333 lines
11 KiB
HTML
333 lines
11 KiB
HTML
<!--startcut ======================================================= -->
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
<html>
|
|
<head>
|
|
<META NAME="generator" CONTENT="lgazmail v1.3D.j">
|
|
<TITLE>The Answer Guy 54: "Unary Command Expected" in Shell Script</TITLE>
|
|
</HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000"
|
|
LINK="#3366FF" VLINK="#A000A0">
|
|
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
|
|
<P> <hr>
|
|
<!-- *** BEGIN navbar *** :::::::::::::::::::::::::::::::::::::::::::::::: -->
|
|
<p align="center">
|
|
<A HREF="../lg_bytes54.html"><IMG ALT="[ Prev ]"
|
|
SRC="../../gx/navbar/prev.jpg"
|
|
WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
|
|
<IMG ALT="" SRC="../../gx/navbar/left.jpg"
|
|
WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom" >
|
|
<A HREF="../index.html"><IMG ALT="[ Table of Contents ]"
|
|
SRC="../../gx/navbar/toc.jpg"
|
|
WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A>
|
|
<A HREF="../../index.html"><IMG ALT="[ Front Page ]"
|
|
SRC="../../gx/navbar/frontpage.jpg"
|
|
WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
|
|
<A HREF="../../faq/index.html"><IMG ALT="[ FAQ ]"
|
|
SRC="../../gx/navbar/faq.jpg"
|
|
WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
|
|
<IMG ALT="" SRC="../../gx/navbar/right.jpg"
|
|
WIDTH="15" HEIGHT="45" ALIGN="bottom" >
|
|
<A HREF="../lg_tips54.html"><IMG ALT="[ Next ]"
|
|
SRC="../../gx/navbar/next.jpg"
|
|
WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A>
|
|
<!-- *** END navbar *** :::::::::::::::::::::::::::::::::::::::::::::::::: -->
|
|
</p>
|
|
<P> <hr> <P>
|
|
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
|
|
<center>
|
|
<H1><A NAME="answer">
|
|
<img src="../../gx/dennis/qbubble.gif" alt="(?)"
|
|
border="0" align="middle">
|
|
<font color="#B03060">The Answer Guy</font>
|
|
<img src="../../gx/dennis/bbubble.gif" alt="(!)"
|
|
border="0" align="middle">
|
|
</A></H1>
|
|
<BR>
|
|
<H4>By James T. Dennis,
|
|
<a href="mailto:linux-questions-only@ssc.com">linux-questions-only@ssc.com</a><BR>
|
|
LinuxCare,
|
|
<A HREF="http://www.linuxcare.com/">http://www.linuxcare.com/</A>
|
|
</H4>
|
|
</center>
|
|
|
|
<p><hr><p>
|
|
<!-- endcut ======================================================= -->
|
|
<!-- begin 8 -->
|
|
<H3 align="left"><img src="../../gx/dennis/qbubble.gif"
|
|
height="50" width="60" alt="(?) " border="0"
|
|
>"Unary Command Expected" in Shell Script</H3>
|
|
|
|
|
|
<p><strong>From J.Keo Power on Fri, 05 May 2000
|
|
</strong></p>
|
|
<!-- ::
|
|
"Unary Command Expected" in Shell Script
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
:: -->
|
|
<P><STRONG>
|
|
Hi Jim,
|
|
</STRONG></P>
|
|
<P><STRONG>
|
|
My name is Keo. I have been trying to write a script that provides a
|
|
salutation to the user, though that is different depending on who logs
|
|
in. There are only three of us logging in on the system, and I want to
|
|
have a little fun with them by putting in some cool messages.
|
|
</STRONG></P>
|
|
<P><STRONG>
|
|
So far, I have attempted to write a script in vi named intro and placing
|
|
the file in my home directory. I have "chmod to ugo+x intro". Then going
|
|
to the <TT>/etc/bashrc</TT> file and putting in the path of the executable intro
|
|
file in my home directory.
|
|
</STRONG></P>
|
|
<P><STRONG>
|
|
The bashrc is trying to run the executable, but is returning the message
|
|
"unary command expected". I am not sure what that means!
|
|
</STRONG></P>
|
|
<P><STRONG>
|
|
If you could give me a little guidance on if my methodology is correct
|
|
as far as the files I am manipulating, and possibly an outline of the
|
|
script to write. here is what I have attempted (last time):
|
|
</STRONG></P>
|
|
|
|
<pre><strong> #! intro
|
|
# test of login script
|
|
|
|
name=$LOGIN
|
|
if [ $name = keo ]
|
|
then
|
|
echo "Whats up mano?"
|
|
else
|
|
if [ $name = dan ]
|
|
then
|
|
echo "Lifes a peach, homeboy."
|
|
else
|
|
if [ $name = $other ]
|
|
then
|
|
exit
|
|
fi
|
|
fi
|
|
fi
|
|
exit
|
|
</strong></pre>
|
|
<P><STRONG>
|
|
Thanks for any help.
|
|
Keo
|
|
</STRONG></P>
|
|
<BLOCKQUOTE><IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
|
|
HEIGHT="28" WIDTH="50" BORDER="0"
|
|
>
|
|
I've been trying to clean out my inbox of the 500 messages
|
|
that have been sitting unanswer and unsorted for months.
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
This is one of them that I just couldn't pass up.
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
First problem with this script is right at the first
|
|
line. That should be a "she-bang" line --- like:
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE><BLOCKQUOTE><CODE>
|
|
#!/bin/sh
|
|
</CODE></BLOCKQUOTE></BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
... which is normally found at the beginning of all scripts.
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
The "she-bang" line is sometimes called "hash bang" -- so-called
|
|
because the "#" is called a "hash" in some parts, and the "!"
|
|
is often called a "bang" among hackers, it's also short for
|
|
"shell-bang" according to some. It looks like a comment line
|
|
--- but it is used by the system to determine where to find an
|
|
interpreter that can handle the text of any script. Thus you
|
|
might see 'awk' programs start with a line like:
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE><BLOCKQUOTE><CODE>
|
|
#!/usr/bin/gawk -f
|
|
</CODE></BLOCKQUOTE></BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
... or PERL programs with a she-bang like:
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE><BLOCKQUOTE><CODE>
|
|
#!/usr/local/bin/perl
|
|
</CODE></BLOCKQUOTE></BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
... and programs written using the 'expect' language (a
|
|
derivative of TCL) would naturally start with something like:
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE><BLOCKQUOTE><CODE>
|
|
#!/usr/local/bin/expect -f
|
|
</CODE></BLOCKQUOTE></BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
After you fix that here are some other comments that I've
|
|
inserted into your code (my comments start with the ##
|
|
-- double hash):
|
|
</BLOCKQUOTE>
|
|
|
|
<blockquote><pre> #! intro
|
|
# test of login script
|
|
|
|
name=$LOGIN
|
|
## should be quoted: name="$LOGIN" in case $LOGIN had
|
|
## any embedded whitespace. It shouldn't, but your scripts
|
|
## will be more robust if you code them to accept the most
|
|
## likely forms of bad input.
|
|
|
|
## Also I don't think $LOGIN is defined on most forms of
|
|
## UNIX. I know of $LOGNAME and $USER, but no $LOGIN
|
|
|
|
## Finally, why assign this to some local shell variable?
|
|
## Why not just use the environment variable directly
|
|
## since you're not modifying it?
|
|
|
|
if [ $name = keo ]
|
|
then
|
|
echo "Whats up mano?"
|
|
## That can be replaced with:
|
|
## [ "$name" = "keo" ] && echo "What's up mano?"
|
|
## Note the quotations, and the use of the && conditional
|
|
## execution operator
|
|
else
|
|
## don't need an else, just let this test drop through to here
|
|
## (the else would be useful for cases where the tests were expensive
|
|
## or they had "side effects."
|
|
if [ $name = dan ]
|
|
then
|
|
echo "Lifes a peach, homeboy."
|
|
## [ "$name" = "dan" ] && echo "Lifes a peach, homeboy."
|
|
else
|
|
if [ $name = $other ]
|
|
then
|
|
exit
|
|
fi
|
|
fi
|
|
fi
|
|
exit
|
|
## $other is undefined. Thus the [ ('test') here will give
|
|
## you a complaint. If it was written as: [ "$name" = "$other" ]
|
|
## then the null expansion of the $other (undefined) variable
|
|
## would not be a problem for the 'test' command. The argument
|
|
## would be there, albeit empty. Otherwise the = operation
|
|
## to the 'test' command will not have its requisite TWO operands.
|
|
|
|
## All eight of these trailing lines are useless. You can just
|
|
## drop out of the nested tests with just the two 'fi' delimiters
|
|
## (technically 'fi' is not a command, it's a delimiter).
|
|
</pre></blockquote>
|
|
<BLOCKQUOTE>
|
|
Here's a more effective version of the script:
|
|
</BLOCKQUOTE>
|
|
|
|
<blockquote><pre>#!/bin/sh
|
|
case "$LOGNAME" in
|
|
jon)
|
|
echo "Whats up mano?" ;;
|
|
dan)
|
|
echo "Lifes a peach, homeboy."
|
|
*)
|
|
# Do whatever here for any other cases
|
|
;;
|
|
esac
|
|
</pre></blockquote>
|
|
<BLOCKQUOTE>
|
|
This is pretty flexible. You can easily extend it for
|
|
additional cases by insering new "foo)" clauses with
|
|
their own ";;" terminators. It also allows you to
|
|
use shell globbing and some other pattern matching like:
|
|
</BLOCKQUOTE>
|
|
|
|
<blockquote><pre>#!/bin/sh
|
|
case "$LOGNAME" in
|
|
jon|mary)
|
|
echo "Whats up mano?" ;;
|
|
dan)
|
|
echo "Lifes a peach, homeboy."
|
|
b*)
|
|
echo "You bad, man!"
|
|
;;
|
|
esac
|
|
</pre></blockquote>
|
|
<BLOCKQUOTE>
|
|
Note that this sees "Jon" or "Mary" in the first clause,
|
|
Dan in the second and anyone whose login name starts with a
|
|
"b" in the last case.
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
Any good book on shell scripting will help you with this.
|
|
</BLOCKQUOTE>
|
|
|
|
<!-- sig -->
|
|
|
|
|
|
<!-- end 8 -->
|
|
<!--startcut ======================================================= -->
|
|
<P> <hr> </p>
|
|
<H5 align="center"><a href="http://www.linuxgazette.com/copying.html"
|
|
>Copyright ©</a> 2000, James T. Dennis
|
|
<BR>Published in <I>The Linux Gazette</I> Issue 54 June 2000</H5>
|
|
<H6 ALIGN="center">HTML transformation by
|
|
<A HREF="mailto:star@tuxtops.com">Heather Stern</a> of
|
|
Tuxtops, Inc.,
|
|
<A HREF="http://www.tuxtops.com/">http://www.tuxtops.com/</A>
|
|
</H6>
|
|
<P> <hr>
|
|
<!-- begin tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::-->
|
|
<p align="center">
|
|
<IMG ALT="" SRC="../../gx/navbar/left.jpg"
|
|
WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="middle" border="0">
|
|
<A HREF="../lg_answer54.html"
|
|
><IMG SRC="../../gx/dennis/answertoc.jpg" align="middle"
|
|
ALT="[ Answer Guy Current Index ]" border="0"></A>
|
|
<A HREF="../lg_answer54.html#greeting"><img align="middle"
|
|
src="../../gx/dennis/smily.gif" alt="greetings" border="0"></A>
|
|
<A HREF="1.html">1</A>
|
|
<A HREF="2.html">2</A>
|
|
<A HREF="3.html">3</A>
|
|
<A HREF="4.html">4</A>
|
|
<A HREF="5.html">5</A>
|
|
<A HREF="6.html">6</A>
|
|
<A HREF="7.html">7</A>
|
|
<A HREF="8.html">8</A>
|
|
<A HREF="9.html">9</A>
|
|
<A HREF="10.html">10</A>
|
|
<A HREF="11.html">11</A>
|
|
<A HREF="12.html">12</A>
|
|
<A HREF="13.html">13</A>
|
|
<A HREF="14.html">14</A>
|
|
<A HREF="15.html">15</A>
|
|
<A HREF="16.html">16</A>
|
|
<A HREF="17.html">17</A>
|
|
<A HREF="18.html">18</A>
|
|
<A HREF="../../tag/kb.html"
|
|
><IMG SRC="../../gx/dennis/answerpast.jpg" align="middle"
|
|
ALT="[ Index of Past Answers ]" border="0"></A></td>
|
|
<IMG ALT="" SRC="../../gx/navbar/right.jpg" align="middle"
|
|
WIDTH="14" HEIGHT="45" BORDER="0">
|
|
</p>
|
|
<!-- end tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::::-->
|
|
<P> <hr>
|
|
<!-- *** BEGIN navbar *** :::::::::::::::::::::::::::::::::::::::::::::::: -->
|
|
<p align="center">
|
|
<A HREF="../lg_bytes54.html"><IMG ALT="[ Prev ]"
|
|
SRC="../../gx/navbar/prev.jpg"
|
|
WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
|
|
<IMG ALT="" SRC="../../gx/navbar/left.jpg"
|
|
WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom" >
|
|
<A HREF="../index.html"><IMG ALT="[ Table of Contents ]"
|
|
SRC="../../gx/navbar/toc.jpg"
|
|
WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A>
|
|
<A HREF="../../index.html"><IMG ALT="[ Front Page ]"
|
|
SRC="../../gx/navbar/frontpage.jpg"
|
|
WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
|
|
<A HREF="../../faq/index.html"><IMG ALT="[ FAQ ]"
|
|
SRC="../../gx/navbar/faq.jpg"
|
|
WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
|
|
<IMG ALT="" SRC="../../gx/navbar/right.jpg"
|
|
WIDTH="15" HEIGHT="45" ALIGN="bottom" >
|
|
<A HREF="../lg_tips54.html"><IMG ALT="[ Next ]"
|
|
SRC="../../gx/navbar/next.jpg"
|
|
WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A>
|
|
<!-- *** END navbar *** :::::::::::::::::::::::::::::::::::::::::::::::::: -->
|
|
</p>
|
|
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
|
|
</BODY></HTML>
|
|
<!--endcut ========================================================= -->
|