getopts problem


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users getopts problem
# 1  
Old 11-11-2008
getopts problem

i was going through the man page of getopts

this particular section is not clear to me can anyone please
clarify in a little detail so that i can understand the concept

MANPAGE::

Since getopts affects the current shell execution environ-
ment, it is generally provided as a shell regular built-in.
If it is called in a subshell or separate utility execution
environment, such as one of the following:

(getopts abc value "$@")
nohup getopts ...
find . -exec getopts ... \;

it does not affect the shell variables in the caller's
environment.

Notice that shell functions share OPTIND with the calling
shell even though the positional parameters are changed.
# 2  
Old 11-11-2008
I am not sure which point you have troubles to understand, so maybe my explanation is superficial - i'll just give it a try:

when you call a script with some parameters these parameters are part of the scripts environment - variables $1, $2, $3, .... - and this environment does not change under normal circumstances (save for explicit changes to it using the "shift" keyword, etc.).

This now changes when you use "getopts" to parse the commandline. "getopts" will do the parsing but after calling it the environment (more precisely: part of it - the parameters special variables) will have no more meaningful content. Example:

Code:
echo "$1"      # some content
/some/command
echo "$1"      # same !! content as before

whereas:

Code:
echo "$1"      # some content
getopts [.....] "$@"
echo "$1"      # NOT the same content as before

To prevent this it is possible to call getopts in a different (child) environment by several means listed in the manpage. When you call a child process the environment of the father process is copied and given to the child (the child "inherits" from the father) - this is why the Nr. 1 reason for cron jobs is the wrong environment: the job is tested in a login shell, which has some environment the inheriting job relies implicitly upon. When it is called not by the login shell but by cron (which has next to no environment, not even a PATH) it fails. This only as n aside.

When getopts is - by the mentioned means - called in a child process it gets a copy of the environment and works on this, therefore not destroying the father process' environment.

Was this your problem or is there more to it?

I hope this helps.

bakunin
# 3  
Old 11-12-2008
getopts querry ..in continuation

thanks, that pretty much explained and cleared my confusion ...

as it seems to me you can help can you tell me please in the below code what this set lines are doing


set -- -1 -----------> ??
while getopts 1 opt; do
case "${opt}" in
1) echo "Worked!";;
*) exit 1;
esac
done

OPTIND=1
set -- -2-------------??
while getopts 2 opt; do
case "${opt}" in
2) echo "Worked!";;
*) exit 1;
esac
done
# 4  
Old 11-12-2008
hello bakunin,
i tried something after reading your post ...there is a problem..
see below please

*************************CODE**********************

echo $1 $2 $3


while getopts ":ab:" var
do
case $var in

a) echo "option a"
;;

b) echo "option b argument $OPTARG "
;;

esac
done


echo " $1 $2 $3"

**********************o/p*******************
csbu061 [mhalder] 216: bash sample.sh -b malay milan manab
-b malay milan manab
option b argument malay
-b malay milan


----according to yoy the envirinment [$1..$2..] will change if i call from the same shell but here it is printing the same values.
# 5  
Old 11-12-2008
getopts problem

in the below code what this set lines are doing ???
and what does this -- variable means


set -- -1 -----------> ??
while getopts 1 opt; do
case "${opt}" in
1) echo "Worked!";;
*) exit 1;
esac
done

OPTIND=1
set -- -2-------------??
while getopts 2 opt; do
case "${opt}" in
2) echo "Worked!";;
*) exit 1;
esac
done
Edit/Delete Message
# 6  
Old 11-12-2008
getopts issue

in the below script i am expecting the variable surname to be changed but that is not happening can anyone help and explain why and how to do that


surname=halder

echo $surname

while getopts "Smilie" OPTION
do
case $OPTION in

b) surname=bose
;;

esac
done

echo $surname
# 7  
Old 11-12-2008
Quote:
Originally Posted by mobydick
----according to yoy the envirinment [$1..$2..] will change if i call from the same shell but here it is printing the same values.
The point is not that it has to change, but it might change. You simply cannot rely on the fact that "$1" remains the same before and after getopts - whereas you can rely on, say, "$charley" being the same before and after executing a command.

The meaning of the set-command is simple: "set --" clears the special variables "$1", "$2", etc.:

Code:
# cat settest.sh
#!/bin/ksh

echo "first parm is: $1"
echo "second parm is: $2"
echo "third parm is: $3"

set --

echo "first parm is: $1"
echo "second parm is: $2"
echo "third parm is: $3"
exit 0

# ./settest.sh a b c
first parm is: a
second parm is: b
third parm is: c
first parm is:
second parm is:
third parm is:

Therefore the "set -- -2-------" means: first make sure no other commandline parameter is set, regardless of what you enter on the commandline, then set exactly one parameter ("-2") with some argument ("------").

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Identify problem with while getopts

can anyone spot a problem with the below: $ $ cat getopts.sh #!/bin/sh usage() { echo "myscript.sh local /tmp data.txt 600s -query" 1>&2; exit 1; } while... (4 Replies)
Discussion started by: SkySmart
4 Replies

2. Shell Programming and Scripting

problem with getopts

Hi, I have written a script to take command line arguments using geopts.This is the code. #!/bin/sh # The usage of this script. usage="Usage is $0" usage="$usage " usage="$usage " usage="$usage " # Use the getopt utility to set up the command line flags. set -- `/usr/bin/getopt... (4 Replies)
Discussion started by: arijitsaha
4 Replies

3. Shell Programming and Scripting

getopts - optional and problem to display help

In the below code while getopts :rfw:d:s:a: options do case "$options" in r) echo reverse;; f) echo forward;; w) window=$OPTARG;; d) duration=$OPTARG;; s) search=$OPTARG;; a) value=$OPTARG;; *) help; exit;; esac done ... (2 Replies)
Discussion started by: Amutha
2 Replies

4. Shell Programming and Scripting

getopts problem

Hi everyone I want to know how can we pass multiple argument in getopts suppose PARAMS="abcd" while getopts ${PARMS} FLAG do case ${FLAG} in (a) (b) (c) (d) esac (6 Replies)
Discussion started by: aishsimplesweet
6 Replies

5. Shell Programming and Scripting

getopts problem

How do I get the getopts command to display whats written at my help option if no option is types in? For example, myscript.sh -h will bring up my help option, however, I also want myscript.sh to do the same! #!/bin/bash while getopts :abh opt do case "$opt" in... (2 Replies)
Discussion started by: linuxkid
2 Replies

6. Shell Programming and Scripting

problem with getopts

Hi, I am a new member to unix.com. Actually I am facing a problem with getopts. In my script i have used getopts to parse the parameters. when i use the script as shown below its working fine: find_status -p all ### where find_status is a script name. But even if I pass more than one... (3 Replies)
Discussion started by: pvamsikr
3 Replies

7. Shell Programming and Scripting

Problem in getopts

while getopts l:f:s:o:h: c do case $c in l) tail -${OPTARG} /etc/passwd exit 2;; f) head -${OPTARG} /etc/passwd exit 3;; s) grep ${OPTARG} /etc/passwd | cut -d: -f7 exit 4;; o) OARG=$OPTARG exit 5;; h) ... (3 Replies)
Discussion started by: nadman123
3 Replies

8. Shell Programming and Scripting

getopts help

Hi i have part of the scripts below ,getopt for -h or ? not working for me. can anybody tell me if this sytax right or wrong. #!/usr/bin/ksh program=$(basename $0) ##################################################################################### function usageerr { RC=1 ... (3 Replies)
Discussion started by: GrepMe
3 Replies

9. Shell Programming and Scripting

Problem with getopts

I need to parse parameters but the arguments could be NULL,example: > cat getopts.sh while getopts "a:b:" opt 2>/dev/null do case "${opt}" in a) echo "A:${OPTARG}" ;; b) echo "B:${OPTARG}" ;; *) exit 1 ;; esac done > getopts.sh -a TEST1 -b TEST2... (5 Replies)
Discussion started by: Klashxx
5 Replies

10. Shell Programming and Scripting

getopts

I have a script that facillitates NDM (Connect::\Direct) transfer to remote hosts. This script uses getopts to parse through the parameters passed to it and to set appropriate variables based upon what was passed in. Kickoff="mv $PATH/$FILE1 $PATH/$FILE2" ndm_shell.ksh -p $Node -s $Source -d... (3 Replies)
Discussion started by: google
3 Replies
Login or Register to Ask a Question