Saturday, June 03, 2006

script stdout | shell

when writing a shell script, it is important to make sure it does the correct things, especially when it come to execute the specific commands like rm etcetera. it is advisable to print out to stdout what the commands in the scripts do to make sure no typo/error made.
eg: script.sh
#! /bin/ksh
for file in *.log
do
echo rm $file
done

so when you run the script:
$ ./script.sh
rm 1.log
rm 2.log
rm 3.log

it will not execute the rm command since we only use echo. in order to execute it, no need to delete the echo but simpy pipe ( | ) it to the shell:
$ ./script.sh | sh

will delete those 3 log files.

No comments: