jak@sactoh0.UUCP (Jay A. Konigsberg) (10/11/89)
I have a small shell script that I can't get quite right. What it is supposed to do is list subdirectories or the message "There are no subdirectories". The script is as follows: ls -l | grep "^d" | pg || echo "There are no subdirectories" When the pipe into " pg " is removed it works ok, but I would like to keep the pg in the script. Thanks to all who respond. -- ############################################################# # Jay Konigsberg # (916) 484-6029 # # SAC-UNIX, Sacramento, Ca. # UUCP=...pacbell!sactoh0!jak # #############################################################
ghe@nucthy.physics.orst.edu (Guangliang He) (10/11/89)
Sorry to post it, but the mailer on our machine dosen't know the UUCP domain. In article <1943@sactoh0.UUCP> jak@sactoh0.UUCP (Jay A. Konigsberg) writes: > > [deleted text here] > ls -l | grep "^d" | pg || echo "There are no subdirectories" > > When the pipe into " pg " is removed it works ok, but I would like > to keep the pg in the script. > [deleted text here] > When you have 'pg' in your script, the exit status of left hand side always 0. Without the 'pg', when 'grep' fails, the exit status of left hand side is a non zero value so the script goes to the right hand side of the || op. Then it echos "There ..." Hope it helps. ======================================================================= USMAIL: Guangliang He | INTERNET: ghe@PHYSICS.ORST.EDU Department of Physics | BITNET: hegl@ORSTVM.BITNET Weniger Hall 301 | Oregon State University | Corvallis, OR 97331-6507 | PHONE: (503) 737-4631 =======================================================================
cpcahil@virtech.UUCP (Conor P. Cahill) (10/11/89)
In article <1943@sactoh0.UUCP>, jak@sactoh0.UUCP (Jay A. Konigsberg) writes: > > When the pipe into " pg " is removed it works ok, but I would like > to keep the pg in the script. Your problem is that pg always exits with a 0 (at least I could not get any non-zero exit) and therefore the stuff to the right of the || will never be executed. You could do the following: (ls -l | grep "^d" || echo "There are no subdirectories" ) | pg or you could re-write the script so it used more than 1 line: ls -l | grep "^d" > /tmp/sh$$ if [ $? = 0 ]; then pg /tmp/sh$$ else echo "There are no ..." fi rm /tmp/sh$$ -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+
merlyn@iwarp.intel.com (Randal Schwartz) (10/12/89)
In article <1943@sactoh0.UUCP>, jak@sactoh0 (Jay A. Konigsberg) writes: | | I have a small shell script that I can't get quite right. What it | is supposed to do is list subdirectories or the message "There are | no subdirectories". The script is as follows: | | ls -l | grep "^d" | pg || echo "There are no subdirectories" | | When the pipe into " pg " is removed it works ok, but I would like | to keep the pg in the script. Cheat. Try something like: (ls -l | grep "^d" || echo "There are no subdirectories") | pg 'course, this puts "There are no.." through the pg, but who cares? I said it was cheating. Nope, I can't do it in Perl any shorter. :-) -- /== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\ | on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn | \== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/
davr@hrtix.UUCP (David C. Raines) (10/12/89)
In article <1943@sactoh0.UUCP> jak@sactoh0.UUCP (Jay A. Konigsberg) writes: > >I have a small shell script that I can't get quite right. What it >ls -l | grep "^d" | pg || echo "There are no subdirectories" pg returns 'exit 0' whether there is any input or not, so the second half of the 'or' is never executed. The following should do what you intended, with the minor glitch that the 'no subdirectories' message also ends in a page 'EOF:' prompt. (ls -l | grep "^d" || echo "There are no subdirectories") | pg Or you could write a more complex script that saved $? from the grep command, etc. > >Thanks to all who respond. You are welcome. -- David Raines TCA 5 National Dr., Windsor Locks, CT 06096 UUCP: ...!uunet!hrtix!davr
abrams@dan.ccd.bnl.gov (The Ancient Programmer) (05/09/91)
How does one do a simple computation in a shell script? The c-shell does it very neatly. Running: #!/bin/csh set a = 10 set b = 1 @ c = $a - $b echo "a=$a, b=$b, c=$c" produces: a=10, b=1, c=9 but I've been unable to find out how to do this in the bourne shell. Any help will be greatly appreciated. -- INTERNET: abrams@bnl.gov |/ | ^ BITNET: abrams@bnlux0.BITNET |\ |__ /-\
rearl@gnu.ai.mit.edu (Robert Earl) (05/09/91)
In article <1991May8.192623.24160@bnlux1.bnl.gov> abrams@dan.ccd.bnl.gov (The Ancient Programmer) writes: | How does one do a simple computation in a shell script? | The c-shell does it very neatly. | Running: | #!/bin/csh | set a = 10 | set b = 1 | @ c = $a - $b | echo "a=$a, b=$b, c=$c" | | produces: a=10, b=1, c=9 | | but I've been unable to find out how to do this in the bourne shell. You have to use expr(1) and backquotes: #!/bin/sh a=10 b=1 c=`expr $a - $b` # or perl -e "print $a - $b" :-) echo "a=$a, b=$b, c=$c" --robert
chip@osh3.OSHA.GOV (Chip Yamasaki) (05/10/91)
In <1991May8.192623.24160@bnlux1.bnl.gov> abrams@dan.ccd.bnl.gov (The Ancient Programmer) writes: > How does one do a simple computation in a shell script? >The c-shell does it very neatly. >Running: > #!/bin/csh > set a = 10 > set b = 1 > @ c = $a - $b > echo "a=$a, b=$b, c=$c" >produces: a=10, b=1, c=9 >but I've been unable to find out how to do this in the bourne shell. You don't, exactly. What you do is use the expr program. It is slower because it is external, but it works just fine. Try #!/bin/sh a=10 b=1 c=`expr $a + $b` echo "a=$a b=$b c=$c" -- -- Charles "Chip" Yamasaki chip@oshcomm.osha.gov
dan@systech.bjorn.COM (Dan Gill) (05/12/91)
In article <1991May8.192623.24160@bnlux1.bnl.gov>, abrams@dan.ccd.bnl.gov (The Ancient Programmer) writes: > > How does one do a simple computation in a shell script? > The c-shell does it very neatly. > Running: > #!/bin/csh > set a = 10 > set b = 1 > @ c = $a - $b > echo "a=$a, b=$b, c=$c" > > produces: a=10, b=1, c=9 > you will have to do the math outside of the shell script with expr try this: a=10 b=1 c=`expr $a - $b` echo "a=$a, b=$b, c=$c" unless you don't have expr then you are back where you started... Dg -- ------------------------------------------------------------------------------- "On second thought, let us not go to Camelot. It is a silly place" Dan Gill uunet!systech!dan -------------------------------------------------------------------------------