If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > parameter processing

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-31-03, 01:53
msetjadi msetjadi is offline
Registered User
 
Join Date: Jun 2003
Posts: 35
parameter processing

Hi Anyone,

if i have a script that expecting a parameter such as:

> ./myscript.sh -Uuser -Ppassword -Ffilename


How do i process those parameter to get the user,password and filename?

if i use $1 or $2, i will get -Uuser and -Ppassword not user / password.

Are there any unix command that might be usefull to do this?


Thanks.
Rookie
Reply With Quote
  #2 (permalink)  
Old 07-31-03, 06:15
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,543
Re: parameter processing

Search for
ksh getopt
in google ...

From a search result,

The following code fragment shows how one might process the arguments for
a command that can take the options a or b, as well as the option o,
which requires an argument:

set -- `getopt abo: $*`
if [ $? != 0 ]
then
echo $USAGE
exit 2
fi
for i in $*
do
case $i in
-a | -b) FLAG=$i; shift;;
-o) OARG=$2; shift 2;;
--) shift; break;;
esac
done

This code accepts any of the following as equivalent:

cmd -aoarg file file
cmd -a -o arg file file
cmd -oarg -a file file
cmd -a -oarg -- file file
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 07-31-03, 06:35
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Depends on the shell. You can always do something messy and check for -P/U/F and then 'cut -c2-'.

If you have bash or ksh (and probably some others but I'm ignorant), you can use 'getopts'.

An example below:

------------------------------------------------------
# getoptsScript

while getopts ":abc:d:" opt
do
case $opt in
a) echo "Option a has been passed"
optionA=true
;;
b) echo "Option b has been passed"
optionB=true
;;
c) echo "Option c has been passed"
optargC=$OPTARG
echo "Option c has argument $optargC"
;;
d) echo "Option d has been passed"
optargD=$OPTARG
echo "Option d has argument $optargD"
;;
*) echo "Invalid option - $opt"
;;
esac
done
shift $((OPTIND - 1))

-----------------------------------------------------------------

This script takes options a, b, c and d.

getoptScript -a -b OR getoptScript -ab is exactly the same

The options list (":abc:d:") holds the valid options. Options c and d are suffixed with a ":" which indicates that they require an option argument.

getoptScript -abc "An argument for c" -d "An argument for d"

The $OPTIND variable is incremented when options are processed. If you shift your input parameters by $OPTIND after processing your options, any additional parameters would be set to $1, $2...

e.g.

getoptScript -c "An argument for c" some other values

Would hold some, other and values in $1, $2 and $3 respectively.

HTH

Last edited by Damian Ibbotson; 07-31-03 at 06:38.
Reply With Quote
  #4 (permalink)  
Old 08-29-03, 10:31
gurey gurey is offline
Registered User
 
Join Date: Aug 2003
Location: Argentina
Posts: 780
Re: parameter processing

Quote:
Originally posted by msetjadi
Hi Anyone,

if i have a script that expecting a parameter such as:

> ./myscript.sh -Uuser -Ppassword -Ffilename


How do i process those parameter to get the user,password and filename?

if i use $1 or $2, i will get -Uuser and -Ppassword not user / password.

Are there any unix command that might be usefull to do this?


Thanks.
Rookie
Hi Rookie,
I do not have left very clear.
To see if this helps you.
For example:

usu=$1
pass=$2
file=$3
...............

or

Using ksh

echo "Username: \c"
read usr
echo off
echo "Password: \c"
read pass
echo on
echo "File name: \c"
read file
.......................

Gustavo.
Reply With Quote
  #5 (permalink)  
Old 08-29-03, 10:38
gurey gurey is offline
Registered User
 
Join Date: Aug 2003
Location: Argentina
Posts: 780
Re: parameter processing

Quote:
Originally posted by gurey
Hi Rookie,
I do not have left very clear.
To see if this helps you.
For example:

usu=$1
pass=$2
file=$3
...............

or

Using ksh

echo "Username: \c"
read usr
echo off
echo "Password: \c"
read pass
echo on
echo "File name: \c"
read file
.......................

Gustavo.
Sorry Rookie

The correct form is

Using ksh

echo "Username: \c"
read usr
echo "Password: \c"
echo off
read pass
echo on
echo "File name: \c"
read file
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On