Extension Development, bash scripting
From TYPO3Wiki
<< 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
[edit] Mirroring
If you need to mirror an extension from another server you can write a little shell-script:
<shell>
#!/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
</shell>
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>
find extension/ | sed -r -e 's/(.*?)/http:\/\/www.foobar.de\/typo3conf\/ext\/\1/g' | wget -vv -i -
</shell>
Of course you can replace the lines:
<shell>
target=http://www.foobar.de/typo3conf/ext/ wget $target$file
</shell>
with
<shell>
target=destination mv $file $target
</shell>
or
<shell>
target=folder cp $file $target"/"$file
</shell>
or
<shell>
target=`echo $file | sed -r -e 's/needle/replace/g' cp $file $target
</shell>
[edit] Search and Replace
If you want to search and replace all files in a directory and replace only the first occurrence:
<shell>
find /tmp | xargs perl -pi -e 's/needle/replace/g'
</shell>
Without perl, it is a little more complicated:
<shell>
#!/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
</shell>
If you already know the filename you can do this:
<shell>
more foobar | sed -r -e 's/needle/replace/g' > foobar
</shell>
Note
Don't you use awk for search and replace!
Again without sed, it is a bit more complicated:
<shell>
more foobar | php -R 'echo preg_replace("/needle/","replace",$argn)."\r\n";' > foobar
</shell>
Of course you can use perl:
<shell>
more foobar | xargs perl -pi -e 's/needle/replace/g'
</shell>
Here is an example taken from my router to show you some other technique how to load a file:
<shell>
- !/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 </shell>
--Chibox 05:41, 28 May 2008 (CEST)
