There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen blue screen boot bsod computer connection crash css dell display driver drivers email error excel explorer firefox firefox 3 game hard drive internet internet explorer itunes laptop linux malware network networking outlook outlook 2003 outlook express partition password printer problem ram router security slow software sound trojan usb video virus vista windows windows xp wireless
UNIX/Linux
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Operating Systems > UNIX/Linux >
Shell script with arguments


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
sankar6254's Avatar
Junior Member with 6 posts.
 
Join Date: Nov 2003
17-Dec-2003, 02:22 PM #1
Shell script with arguments
Hi All,

I need some help/ideas in coming up with a shell script.

Basically, the script should install 1 or 2 or 3 packages based on the input arguments.

For example, if I type in pkgscript.sh a1 a2 a3, it should install all the 3 scripts and pkgscript.sh a1 should install only a1.

If a user enters only pkgscript.sh, it should ask for arguments and then proceed accordingly.

Any help would be greatly appreciated by this shell script novice.

Thank you,
Sankar.
codejockey's Avatar
Senior Member with 1,410 posts.
 
Join Date: Feb 2002
18-Dec-2003, 12:55 AM #2
Probably the simplest approach is to use positional arguments. The shell arranges for each argument passed to your script to appear in a variable, beginning with $1. So, for example, if your script was invoked as:

pkgscript.sh a1 a3

then $1 would have the value "a1" and $2 would have the value "a3". You can iterate over the arguments using a construct such as the following:

while [ ! -z "$1" ]
do
PACKAGE=$1
# perform install command(s) for a single package
shift
done

The key bits here are the test command (abbreviated with the square brackets), the -z option (tests for zero-length string -- i.e., a NULL argument), the shift operator (discards current $1, moves all arguments forward one position), and the while do/done loop. Each of these are discussed in the bash/Bourne shell manual page.

The shell also sets a number of other variables automatically when your script is invoked, including the variable $# (the number of arguments passed to your script). So, for example, you could use this value to determine if your script was invoked with no arguments and print a usage message to inform the user.

You might also want to look at capturing the return code from any installation commands that your script performs, since the user will undoubtedly want to know if the installation was successful.

Hope this helps.
__________________
The slowest component still sits at the keyboard.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 12:21 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.