The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
while loop inside while loop panknil Shell Programming and Scripting 0 01-07-2008 09:49 AM
For loop xramm HP-UX 3 10-10-2007 11:20 AM
While Loop hemangjani Shell Programming and Scripting 2 11-02-2006 08:01 AM
for loop munnabhai1 Shell Programming and Scripting 3 04-06-2006 11:30 AM
how to get the similar function in while loop or for loop trynew Shell Programming and Scripting 3 06-17-2002 08:09 AM

Closed Thread
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 10-16-2001
Registered User
 

Join Date: Oct 2001
Posts: 4
loop

I'm trying to write a script that runs in the background (&) The program names are entered as parameters on the command line. I want to use a loop without hard coding the number of iterations: I want it to execute an arbitary number of programs. Before executing, I need to verify that the program exists, is an ordinary file , and is executable. Could someone please help.....Thanks in advance.
Forum Sponsor
  #2  
Old 10-16-2001
rwb1959's Avatar
Registered User
 

Join Date: Aug 2001
Location: Virginia, USA
Posts: 438
To get the number of positional parameters
given on the command line you can use the
"$#" variable. You could also use a "while"
loop with "$*" (all positional parameters)
as it's input.

You can use the following conditional expressions
for "test" against the files...

-f evaluates true if file exists and is a
regular file.

-x evaluates true if file exists and is an
executable file.

You can "AND" ( && ) these conditions to check
for both in a single "test"
  #3  
Old 10-17-2001
Registered User
 

Join Date: Oct 2001
Posts: 4
Script

Here is the code I came up with. Any suggestions?
Code:
#script is called fickle

while [ "$*" -f -x ]
     do
          "$*"
else

     echo " program doesn't exist"
     echo " not an ordinary file"
     echo " is not executable"
fi

done &&
added code tags for readability --oombera

Last edited by oombera; 02-18-2004 at 08:47 AM.
  #4  
Old 10-17-2001
rwb1959's Avatar
Registered User
 

Join Date: Aug 2001
Location: Virginia, USA
Posts: 438
Well... I know Neo is going to bust my chops for this
but you did try somthing and you need a little
direction so here goes...

OK. The first thing you should do in all shell scripts is to
set up the execution environment. You do this by inserting
the following line as the very first line in your script
(without the comments)...
#!/bin/sh <- Bourne Shell (normally)
...or
#!/bin/ksh <- Korn Shell
...or
#!/bin/csh <- C Shell

...remember that $* expands to ALL the positional parameters.
You need to read each one into a variable...
for prog in $*
do

... now, each time through the loop, $prog contains the next
positional parameter. Next, you want to check $prog for
"ordinary file" AND "executable file"...
if [ -f $prog -a -x $prog ]
then
...
else
...
fi

...then end your "do/done" loop and exit
done
exit 0

...just FYI, it's good programming practice to return some
sort of value (0 = AOK, 255 = Error) although this is a personal
preference.

You can then run this script in the background...
myscript /bin/ls /usr/bin/stat /bin/df ./myprog &

...you get the picture.
Google The UNIX and Linux Forums
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0