shell scripting while if string length != 0

Steve Coile scoile at
Fri Aug 1 06:25:03 PDT 2003


>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.




	# 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
		# execute each command via SSH
		eval ssh ${command}

	# 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

More information about the freebsd-questions mailing list