![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| passing runtime arguments to a shell script... | santy | Shell Programming and Scripting | 10 | 01-09-2009 10:47 PM |
| Is there a limit to the no. of arguments to a shell script ? | hidnana | Shell Programming and Scripting | 4 | 03-17-2008 12:19 PM |
| Passing arguments to a shell script from file while scheduling in cron | weblogicsupport | SUN Solaris | 4 | 01-27-2008 11:16 PM |
| To Write a Shell script that takes two arguments. | bobby36 | Shell Programming and Scripting | 3 | 04-05-2007 08:44 PM |
| How to pass arguments to a function in a shell script? | preetikate | Shell Programming and Scripting | 3 | 03-01-2004 04:55 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
taking command line arguments in bash is fairly easy, heres a piece of code i wrote for a small script a few months ago, most of it is from the tldp.org advanced bash shell scripting guide. what i did was put the first thing ran in the code in a function called "main()". you call this function with:
Code:
main "$@" Code:
main ()
{
NO_ARGS=0
E_OPTERROR=65
if [ $# -eq "$NO_ARGS" ] # should check for no arguments
then
echo "Usage: `basename $0` -s<OPTIONS> <HOSTNAME> "
echo "You must specify interactive, or non interactive mode for now"
echo "Try './serverstatus -h' for more information."
exit $E_OPTERROR
fi
while getopts ":sicnvh" Option
do
case $Option in
s )
hostname=`echo $@`
hostname2=`echo $hostname|awk '{print $2}'`
echo $hostname2
a=`nmap $hostname2`
;;
i )
interactivemode_func
;;
c )
add_serv
;;
n )
non_interactivemode_func
;;
v )
version_func
;;
h )
help_func
;;
* )
echo "Unimplemented option chosen"
;;
esac
done
shift $(($OPTIND - 1))
}
|
|
||||
|
as you can see, the -s option gets a hostname from the command line. so the user would run the program like this:
./serverstatus -s hostname then that information is parsed and, (in my example) nmap is run on that host and the output of that scan is stored in a variable. each next command line option invokes a number of different functions which accomplish different tasks. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|