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:
#!/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:
find extension/ | sed -r -e 's/(.*?)/http:\/\/www.foobar.de\/typo3conf\/ext\/\1/g' | wget -vv -i -
Of course you can replace the lines:
target=http://www.foobar.de/typo3conf/ext/
wget $target$file
with
target=destination
mv $file $target
or
target=folder
cp $file $target"/"$file
or
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:
find /tmp | xargs perl -pi -e 's/needle/replace/g'
Without perl, it is a little more complicated:
#!/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:
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:
more foobar | php -R 'echo preg_replace("/needle/","replace",$argn)."\r\n";' > foobar
Of course you can use perl:
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:
#!/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