two bash scripting questions
Rick Stevens
rstevens at vitalstream.com
Mon Feb 14 19:53:19 UTC 2005
Mark Knecht wrote:
> Hi,
> I've built a little bash script that does some music stuff for me
> in Gnome. I've set a mime type in Gnome so that I can click on certain
> files, a terminal opens, the script runs, I answer some questions and
> the script makes the music server do some things. It works well, as
> far as it goes, but there are a couple of things I couldn't figure out
> on Sunday.
>
> 1) If I run a generic command that talks to a music server in a
> terminal I get a response of OK. I'd like to capture this OK, vs. the
> other possibilities or WARN or ERR, and take action based on that
> response. How can I do this in a script?
It rather depends. In bash, the "stdout" of a program can be captured
as you show below (using "VARIABLE=`command`", "$VARIABLE" will contain
the text returned by "command"). Most command-line programs also return
a numeric return code which can be examined by looking at the "$?"
built-in variable. Most commands return zero on success and non-zero
on failure (and the non-zero failure MAY be related to valid error
numbers in /usr/include/errno.h or /usr/include/sysexit.h). Here's a
quickie script that demonstrates this:
#!/bin/bash
CMD=/usr/bin/true
VAL=`$CMD`
if [ $? -ne 0 ]; then
echo $CMD returned TRUE, value is $VAL
else
echo $CMD returned FALSE, value is $VAL
fi
Change "CMD=/usr/bin/true" to "CMD=/usr/bin/false" to see the FALSE
response. Also remember that $? is the return code of the LAST COMMAND,
so I don't echo it above as it's value would be replaced by the result
of the "if" test. If you want to examine it later, assign it to a
variable and use the variable for testing:
RC=$?
if [ $RC -eq 0 ]; then
> 2) In the case of the command that adds channels to my server I get a
> response that has the channel number buried in the OK, such as OK[3]
> telling me the ADD CHANNEL command completed and the new channel is
> number 3. How can I capture this number for use in the script? The
> normal command sequence here might look like
>
> echo "ADD CHANNEL" | nc localhost 8888
>
> and I receive
>
> OK[3] back in the terminal. I tried some things like
>
> OUTPUT=`echo "ADD CHANNEL" | nc localhost 8888`
>
> and
>
> OUTPUT=$(echo "ADD CHANNEL" | nc localhost 8888)
>
> but they didn't work.
In what way didn't they work? Remember that "nc" may return stuff to
stderr, and your "OK[3]" message may be on stderr rather than stdin. In
that case, I'd change the commands to:
echo "ADD CHANNEL" | nc localhost 8888 >/tmp/retstr 2>&1
RC=$?
OUTPUT=`cat /tmp/retstr`
rm -f /tmp/retstr
You can then test $RC for the return code of the "echo" pipeline and see
both stdout and stderr of the "nc" command in $OUTPUT. Clunky, but it
works.
> The script is small. It's attached in case there are better ways to
> do this stuff that people want to share.
Uh, it wasn't attached.
----------------------------------------------------------------------
- Rick Stevens, Senior Systems Engineer rstevens at vitalstream.com -
- VitalStream, Inc. http://www.vitalstream.com -
- -
- "You think that's tough? Try herding cats!" -
----------------------------------------------------------------------
More information about the Redhat-install-list
mailing list