There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Tag Cloud
access acer asus bios bsod computer crash drive driver drivers error ethernet excel freeze games gaming hard drive hardware hdmi internet java laptop malware memory monitor motherboard music network obp printer problem ram random registry router slow software sound trojan usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > Linux and Unix >
Shell script with arguments

Reply  
Thread Tools
sankar6254's Avatar
Junior Member with 6 posts.
 
Join Date: Nov 2003
17-Dec-2003, 03: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, 01: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.
Reply

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.

Search Tech Support Guy

Find the solution to your
computer problem!




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



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
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 07:38 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.