Translations
Info
All page names need to be in English.
en da  de  fr  it  ja  km  nl  ru  zh

Extension Development, bash scripting

From TYPO3Wiki
Jump to: navigation, search

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

[edit]

How does one pass a command-line parameter to a shell script?

The command line parameters are referenced as $1, $2, etc

Mirroring

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

shell script:
 #!/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:

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

Of course you can replace the lines:

shell script:
   target=http://www.foobar.de/typo3conf/ext/
   wget $target$file

with

shell script:
  target=destination
  mv $file $target

or

shell script:
  target=folder
  cp $file $target"/"$file


or

shell script:
  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:

shell script:
 find /tmp | xargs perl -pi -e 's/needle/replace/g'

Without perl, it is a little more complicated:

shell script:
 #!/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:

shell script:
  
 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:

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

Of course you can use perl:

shell script:
 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:

shell script:
#!/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


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