The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-17-2003
sankar6254 sankar6254 is offline
Registered User
  
 

Join Date: Dec 2003
Posts: 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.
  #2 (permalink)  
Old 12-17-2003
norsk hedensk norsk hedensk is offline Forum Advisor  
Registered User
  
 

Join Date: Jul 2002
Location: new york
Posts: 1,025
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 "$@"
main looks like this:
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))

}
hope that helps!
  #3 (permalink)  
Old 12-17-2003
norsk hedensk norsk hedensk is offline Forum Advisor  
Registered User
  
 

Join Date: Jul 2002
Location: new york
Posts: 1,025
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.
  #4 (permalink)  
Old 12-22-2003
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Hello.

In this situation, I probably wouldn't even bother with getopts.

If you only want to install 1, 2, or 3 packages (thus assuming that the ONLY action that the script is to perform is to install packages) you could do the following:

Code:
#!/bin/sh

if [ "$#" -gt 3 ]; then
  echo "More than 3 arguments entered" && exit 1
fi

pkgs=""

if [ "$#" -eq 0 ]; then
  echo "Please enter a space seperated list of packages"
  read pkgs
fi

if [ -z "$pkgs" ]; then
  # user must have supplied some arguments
  for file in "$@"
  do
    # then install $file as you want
  done
else
  for file in "$pkgs"
  do
   # then install $file as you want
  done
fi

exit 0
Now... I haven't checked this out - just wrote it on the fly. But you get the idea......
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0