Mourning the loss of our friend, WhitPhil.
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Linux and Unix
Tag Cloud
access audio black screen blue screen boot bsod connection crash dell desktop driver dvd email error excel excel 2003 firefox hard drive hardware hijackthis internet keyboard laptop malware monitor network networking outlook problem processor ram recovery router safe mode screen slow sound spyware tdlwsp.dll trojan vba video virus vista vundo windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > Linux and Unix >
Shell script with arguments

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

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 Bookmark and Share

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.

Smart Search

Find your solution!



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 want to help you solve your 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 -5. The time now is 12:24 PM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.