shell scripting while if string length != 0
Steve Coile
scoile at nandomedia.com
Fri Aug 1 06:25:03 PDT 2003
Dave:
>From your description of the script, and from the script itself,
it appears that the reason you're checking for a blank command is to
determine when you've reached the end of commands. Presumably, that
would only occur at the end of the file. Is my interpretation correct?
If the reason you're looking for that blank line is to find the end of
commands, you could approach the problem this way:
- move the original file containing commands into a temporary file;
- create an empty file in the original location;
- execute the commands from the temporary file;
- delete the temporary file.
In this way, you avoid having the file of commands updated while you're
reading through it and executing commands. You also avoid a number
of potential programatic pitfalls that are present with your current
approach, pitfalls that may result in the loss of changes to the file
containing commands.
So:
#/bin/sh
CMDFILE=/path/to/file_o_commands
# move current file to temporary location
mv -f "${CMDFILE}" "${CMDFILE}.tmp"
# create an empty command file
touch "${CMDFILE}"
# commands are coming from the temporary file
eval < "${CMDFILE}.tmp"
# read each command
while read command
do
# execute each command via SSH
eval ssh ${command}
done
# delete the temporary file
rm -f "${CMDFILE}.tmp"
Note the use of "eval". It's necessary because the example you included
in your original posting included commands that include shell special
characters, such as the pipe ("|"). Without the "eval" command, those
characters would not be treated the way you appear to want them treated
(as special characters).
--
Steve Coile
Systems Administrator
Nando Media
ph: 919-861-1200
fax: 919-861-1300
e-mail: sysadmins at nandomedia.com
http://www.nandomedia.com
More information about the freebsd-questions
mailing list