Check the positional parameter

Check the positional parameter

Post by qazm » Fri, 18 Jan 2002 01:51:08



I have a situation where I need to check the positional parameter, if it is
equal to "-display" then I have to assign the next positional parameter to
my variable DISPLAY. For this I am running the following script but I am
unable to do it. Please help me in solving my problem.

i=0
for i in $*
do
        if [ $i = "-display" ]
        then
                shift 1
                DISPLAY=$i
        fi
        break
done

The script will be run like this:

$sh abcd.sh abc def -display 166.2.3.45:0 xyz

 
 
 

Check the positional parameter

Post by Derek M. Fly » Fri, 18 Jan 2002 02:01:29



> I have a situation where I need to check the positional parameter, if it is
> equal to "-display" then I have to assign the next positional parameter to
> my variable DISPLAY. For this I am running the following script but I am
> unable to do it. Please help me in solving my problem.

> i=0
> for i in $*
> do
>    if [ $i = "-display" ]
>    then
>            shift 1
>            DISPLAY=$i
>    fi
>    break
> done

> The script will be run like this:

> $sh abcd.sh abc def -display 166.2.3.45:0 xyz

I recommend loop logic like the following:

#! /bin/sh

while :
do
  [ $# -eq 0 ] && break
  case "$1"
  in
    -d*) echo DISPLAY=$2; shift ;;
    *) echo $1 ;;
  esac
  shift
done

 
 
 

Check the positional parameter

Post by j.. » Fri, 18 Jan 2002 02:08:19



> I have a situation where I need to check the positional parameter,
> if it is equal to "-display" then I have to assign the next
> positional parameter to my variable DISPLAY. For this I am running
> the following script but I am unable to do it. Please help me in
> solving my problem.

> i=0
> for i in $*
> do
>    if [ $i = "-display" ]
>    then
>            shift 1
>            DISPLAY=$i
>    fi
>    break
> done

> The script will be run like this:

> $sh abcd.sh abc def -display 166.2.3.45:0 xyz

You're trying to use i as an index when it's been loaded with a
string ("-display"). You need to keep track of when you see "-display"
and then use the next thing in the parameter list when the loop runs
again. For example.

#/bin/ksh
flag=0
for i in $*
do
        if [ $flag -eq 1 ];then
                DISPLAY=$i
                break;
        else
          if [ $i = "-display" ]
          then
                flag=1
          fi
        fi
done

Or, better yet, use getopt(1) or the getopts shell builtin.

Joe

 
 
 

Check the positional parameter

Post by Floyd Davidso » Fri, 18 Jan 2002 02:30:14



>I have a situation where I need to check the positional parameter, if it is
>equal to "-display" then I have to assign the next positional parameter to
>my variable DISPLAY. For this I am running the following script but I am
>unable to do it. Please help me in solving my problem.

>i=0
>for i in $*
>do
>    if [ $i = "-display" ]
>    then
>            shift 1
>            DISPLAY=$i
>    fi
>    break
>done

>The script will be run like this:

>$sh abcd.sh abc def -display 166.2.3.45:0 xyz

There are probably a lot of ways to do that.  Here is one.

#!/bin/sh

flag=0
for i in $*
do
    if [ "$i" = "-display" ]
    then
        flag=1
    elif [ $flag -ne 0 ]
    then
        flag=0
        DISPLAY=$i
    fi
done

echo $DISPLAY      

--
Floyd L. Davidson         <http://www.ptialaska.net/~floyd>

 
 
 

Check the positional parameter

Post by Eric Amic » Fri, 18 Jan 2002 02:49:18




> > I have a situation where I need to check the positional parameter, if it is
> > equal to "-display" then I have to assign the next positional parameter to
> > my variable DISPLAY. For this I am running the following script but I am
> > unable to do it. Please help me in solving my problem.

> > i=0
> > for i in $*
> > do
> >       if [ $i = "-display" ]
> >       then
> >               shift 1
> >               DISPLAY=$i
> >       fi
> >       break
> > done

> > The script will be run like this:

> > $sh abcd.sh abc def -display 166.2.3.45:0 xyz

> I recommend loop logic like the following:

> #! /bin/sh

> while :
> do
>   [ $# -eq 0 ] && break
>   case "$1"
>   in
>     -d*) echo DISPLAY=$2; shift ;;

You mean "shift 2", I think.

--
Eric Amick
Columbia, MD

 
 
 

Check the positional parameter

Post by Derek M. Fly » Fri, 18 Jan 2002 03:24:01




...
> > I recommend loop logic like the following:

> > #! /bin/sh

> > while :
> > do
> >   [ $# -eq 0 ] && break
> >   case "$1"
> >   in
> >     -d*) echo DISPLAY=$2; shift ;;

> You mean "shift 2", I think.

Nope.  :)  There is a "default" shift at the bottom of the loop:

        *) echo $1 ;;
      esac
      shift
    done

... which accomplishes shifting the other argument.  But you're right
that another strategy would be to have each case option perform its
own shifting.

 
 
 

Check the positional parameter

Post by qazm » Wed, 23 Jan 2002 10:50:36




> >I have a situation where I need to check the positional parameter, if it is
> >equal to "-display" then I have to assign the next positional parameter to
> >my variable DISPLAY. For this I am running the following script but I am
> >unable to do it. Please help me in solving my problem.

> >i=0
> >for i in $*
> >do
> >       if [ $i = "-display" ]
> >       then
> >               shift 1
> >               DISPLAY=$i
> >       fi
> >       break
> >done

> >The script will be run like this:

> >$sh abcd.sh abc def -display 166.2.3.45:0 xyz

> There are probably a lot of ways to do that.  Here is one.

> #!/bin/sh

> flag=0
> for i in $*
> do
>     if [ "$i" = "-display" ]
>     then
>         flag=1
>     elif [ $flag -ne 0 ]
>     then
>         flag=0
>         DISPLAY=$i
>     fi
> done

> echo $DISPLAY

I corrected my script accordingly, its working fine.
Is there any function/command in shell, similar to the getenv() function in C?
I want to check whether the DISPLAY environment variable is existing or not.
I can check like giving as below. But, is there any other optimal way?

        if [ DISPLAY != "" ]
                echo DISPLAY defined
        else
                echo DISPLAY not defined.
        fi

Thanks.

 
 
 

Check the positional parameter

Post by John DuBo » Wed, 23 Jan 2002 12:41:58




>Is there any function/command in shell, similar to the getenv() function in C?
>I want to check whether the DISPLAY environment variable is existing or not.
>I can check like giving as below. But, is there any other optimal way?

>    if [ DISPLAY != "" ]
>            echo DISPLAY defined
>    else
>            echo DISPLAY not defined.
>    fi

That won't tell you whether DISPLAY is set or not; you're using DISPLAY as a
fixed string, not a variable.  To check whether DISPLAY is set to a non-null
value:

        [ -n "$DISPLAY" ]

To check whether it's set at all (possibly to a null value):

        [ -n "${DISPLAY+x}" ]

These test shell variables, which in the Bourne shell will correspond to
environment variables only if you haven't modified them since your script
started or you've exported them.  In this case it will do what you want since
you're presumably testing whether the variable was set in the environment
passed to your script.

        John
--

 
 
 

Check the positional parameter

Post by David Masterso » Thu, 24 Jan 2002 01:30:09


Quote:>>>>> qazmlp  writes:
> Is there any function/command in shell, similar to the getenv()
> function in C?  I want to check whether the DISPLAY environment
> variable is existing or not.  I can check like giving as below. But,
> is there any other optimal way?
>    if [ DISPLAY != "" ]
>            echo DISPLAY defined
>    else
>            echo DISPLAY not defined.
>    fi

echo DISPLAY=${DISPLAY:=""}

You don't need getenv() in shell -- just put a $ in front of the
variable.  See the man page for your shell.

--
David Masterson                dmaster AT synopsys DOT com
Sr. R&D Engineer               Synopsys, Inc.
Software Engineering           Sunnyvale, CA

 
 
 

1. passing parameter to shell function as in the normal unix commands (not positional parameter)

Hi,

I have a function defined in in .profile which accepts three
parameters.

function myfn
{
var1=$1
var1=$3
var1=$3

I am wondering, whether the function can be invkoded as

"myfun -c var1 -d var2 -e var3"

The reason, I would prefer this method, because I don't have to know
the order of the parameters and optionally I can ignore one or more of
the parameter and set the default value, inside the program if needed.

note: I know, it can be invoked as "myfun var1 var2 var3"
      I am using ksh under AIX 5.2

Thanks,
Prince.

2. DNS Load Balancingg and IP Failover for Linux?

3. Positional Parameters in printf()

4. Problem with color-depth XF86_Mach32

5. Positional Parameters in ksh

6. PPP Internet Connection Problem

7. Problems: source a shell script with positional parameters

8. How do I do NAMED in LInux

9. positional parameter in lex and yacc !!

10. Positional Parameters HELP!!!

11. How to treat positional parameters as an array?

12. redirect output with shell positional parameters

13. question about positional parameters in bash