Extension Development, bash scripting

From TYPO3Wiki

Jump to: navigation, search

<< Back to Extension Development << Back to Developer manuals page

[edit]

Contents

Introduction

For use with caution.

Mirroring

If you need to mirror an extension from another server you can write a little shell-script:

<shellScript> :
 
 #!/bin/bash
 
 find extension/ > mirror
 exec 3<mirror
 
 cd folder
 
 while read file <&3
 do
 
  target=http://www.foobar.de/typo3conf/ext/
  wget $target$file
 
 done
 

Description: With exec 3<mirror you open a new Linux-File-Descriptor. There are unlimited Linux-File-Descriptors. Just don't use 0,1,2 because they are reserved for Linux. In the while-loop there is the condition read file from that descriptor, while file is just a variable which stores each line of mirror.

1) Here is an "one-liner" which does the same thing:

<shellScript> :
 
 find extension/ | sed -r -e 's/(.*?)/http:\/\/www.foobar.de\/typo3conf\/ext\/\1/g' | wget -vv -i - 
 

Of course you can replace the lines:

<shellScript> :
 
   target=http://www.foobar.de/typo3conf/ext/
   wget $target$file
 

with

<shellScript> :
 
  target=destination
  mv $file $target
 

or

<shellScript> :
 
  target=folder
  cp $file $target"/"$file
 


or

<shellScript> :
 
  target=`echo $file | sed -r -e 's/needle/replace/g'
  cp $file $target
 

Search and Replace

If you want to search and replace all files in a directory and replace only the first occurrence:

<shellScript> :
 
 find /tmp | xargs perl -pi -e 's/needle/replace/g'
 

Without perl, it is a little more complicated:

<shellScript> :
 
 #!/bin/bash
 
 find extension/ > mirror
 exec 3<mirror
 
 while read file <&3
 do
  replace=`more $file | sed -r -e 's/needle/replace/g'` 
  cat $replace > $file
 done
 


If you already know the filename you can do this:

<shellScript> :
 
 more foobar | sed -r -e 's/needle/replace/g' > foobar
 

Note

Don't you use awk for search and replace!

Again without sed, it is a bit more complicated:

<shellScript> :
 
 more foobar | php -R 'echo preg_replace("/needle/","replace",$argn)."\r\n";' > foobar
 

Of course you can use perl:

<shellScript> :
 
 more foobar | xargs perl -pi -e 's/needle/replace/g'
 


Here is an example taken from my router to show you some other technique how to load a file:

<shellScript> :
 
#!/bin/sh
 
INTERVAL=120
 
while sleep $INTERVAL
do
 
  // Replace "file" with the file name that has the list of IPs
 iplist=/foobar/whitelist
 upcount=0
 downhosts=""
 
 for i in $(cat ${iplist})
 do
        ping -c 1 $i > /dev/null 2>&1
 
        if [[ $? -eq 0 ]]
        then
                let upcount+=1
        else
                downhosts="${downhosts} ${i}"
        fi
 done
 
 if [ $upcount -eq 0 ]
 then
      echo "The following host are down: ${downhosts}"
 else
      echo "ping: ${upcount} hosts are up"
 fi
 
done
 

Links


--Chibox 05:41, 28 May 2008 (CEST)

Personal tools