- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Passing parameters to script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2005 09:54 PM
02-23-2005 09:54 PM
I am writing a script in which I have to pass some command line arguments to the script.
For instance, my script's name is script.sh and it should have the following usage
script.sh -h
ALso I should probably give a usage for the script where
script.sh -u
will give me the usage of all the parameters.
I have not done this before.
Can anyone please send me a sample code which has this kind of functionality.
Please help.
Thanks,
Pat
Solved! Go to Solution.
- Tags:
- option
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2005 10:04 PM
02-23-2005 10:04 PM
Re: Passing parameters to script
I don't know what exactly you are looking for but with the given info i would do it this way using korne shell:
script.sh
---------
#!/usr/bin/ksh
list=$*
for i in $list
do
if [ "$1" -eq "-u" ]
then
echo $2 $3 ...
elif [ "$1" -eq "-h" ]
then
echo $2 $3 ...
fi
fi
done
This you can keep on putting if-then-fi as reuired or you can even use CASE statements.
See man sh for more information.
Hope that helps.
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2005 10:37 PM
02-23-2005 10:37 PM
Re: Passing parameters to script
if you have perl:
cat a.pl
#!/usr/contrib/bin/perl -s
print "value of -h: $h\n";
print "value of -d: $d\n";
print "value of -v: $v\n";
./a.pl -h=peter -d=20050224 -v=1.0
value of -h: peter
value of -d: 20050224
value of -v: 1.0
Regards
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2005 10:52 PM
02-23-2005 10:52 PM
Re: Passing parameters to script
Here there is an example:
FILENAME=""
VERBOSE=0
OPTIONS=ahF:V
while getopts "${OPTIONS}" OPTION 2> /dev/null
do
case ${OPTION} in
(a) echo "option 'a' selected"
;;
(F) FILENAME="${OPTARG}"
;;
(v) VERBOSE=1
;;
(h) print "usage: $(basename $0) [-a] [-v] [-F type]"
print "usage: $(basename $0) -h"
exit
;;
(\?) print -u2 "usage: $(basename $0) [-a] [-v] [-F type]"
print -u2 "usage: $(basename $0) -h"
exit 1
;;
esac
done
shift $(( ${OPTIND} - 1 ))
- Tags:
- getopt/getopts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2005 01:27 AM
02-24-2005 01:27 AM
Solutionprogram="${0:##*/}"
function usage {
print -u2 "$program [-d date -h name -v version | -u]"
}
while getopts :d:h:v:u OPTION
do
case "$OPTION" in
d) Date="$OPTARG";;
h) Name="$OPTARG";;
v) Version="$OPTARG";;;;
u) usage;exit;;
:) print -u2 "$program: $OPTARG is missing argument!"
usage
exit 1;;
\?) print -u2 "$program: $OPTARG is an invalid option!"
usage
exit 1;;
esac
done
shift $(( $OPTIND - 1 ))