Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 1 Aug 2003 09:20:15 -0400 (EDT)
From:      Steve Coile <scoile@nandomedia.com>
To:        "Dave [Hawk-Systems]" <dave@hawk-systems.com>
Cc:        FreeBSD Questions <freebsd-questions@FreeBSD.ORG>
Subject:   RE: shell scripting while if string length != 0
Message-ID:  <Pine.LNX.4.44.0308010910290.2187-100000@localhost.localdomain>
In-Reply-To: <DBEIKNMKGOBGNDHAAKGNAEKFCPAC.dave@hawk-systems.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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@nandomedia.com
http://www.nandomedia.com



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.LNX.4.44.0308010910290.2187-100000>