![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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 &&
Last edited by oombera; 02-18-2004 at 08:47 AM. |
|
#4
|
||||
|
||||
|
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 |