LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-21-2011, 01:38 AM   #1
akelder
Member
 
Registered: Jan 2007
Distribution: debian on servers, ubuntu on desktops/laptops
Posts: 45

Rep: Reputation: 16
shell scripting: value of argument dissapears depending on argument order


Examples:

Code:
$ ./test.sh -a -c 2
operator is -gt
remcount is
            ^ value missing!
Code:
$ ./test.sh -b -c 2
operator is -lt
remcount is
            ^ value missing!
Yet when "-c" is the first argument, its value is present:

Code:
$ ./test.sh -c 2 -b
operator is -lt
remcount is 2
What could I do to ensure the value of "-c" is picked up regardless of the argument order?

Script:

Code:
#!/bin/bash

while [ "$1" ]; do
  case "$1" in
        -a|--above)
        shift
            operator="-gt"
            ;;
        -b|--below)
        shift
            operator="-lt"
            ;;
        -c|--count)
        shift
            remcount="$1"
            ;;
        -h|--help)
        usage
        exit 0
            ;;
    -*)
        echo "$(basename $0): invalid option $1" >&2
        echo "see --help for usage"
        exit 1
        ;;
    *)
        break
        ;;
  esac
  shift
done

echo operator is $operator
echo remcount is $remcount
 
Old 03-21-2011, 02:17 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Have a look at your entries for '-a' and '-b' closely and you may see the error of your ways.

The hint is that neither of these are expecting another argument.
 
Old 03-21-2011, 11:56 AM   #3
akelder
Member
 
Registered: Jan 2007
Distribution: debian on servers, ubuntu on desktops/laptops
Posts: 45

Original Poster
Rep: Reputation: 16
Ok, thanks, using "shift 2" for the argument with a value seems to solve this. Is that the correct solution?

Code:
#!/bin/bash

while [ "$1" ]; do
  case "$1" in
        -a|--above)
            operator="-gt"
            shift
            ;;
        -b|--below)
            operator="-lt"
            shift
            ;;
        -c|--count)
            remcount="$2"
            shift 2
            ;;
        -h|--help)
        usage
        exit 0
            ;;
    -*)
        echo "$(basename $0): invalid option $1" >&2
        echo "see --help for usage"
        exit 1
        ;;
    *)
        break
        ;;
  esac
done

echo operator is $operator
echo remcount is $remcount

Code:
$ ./test.sh -c 2 -b
operator is -lt
remcount is 2
Code:
$ ./test.sh -a -c 2
operator is -gt
remcount is 2
Code:
$ ./test.sh -b -c 2
operator is -lt
remcount is 2
 
Old 03-21-2011, 12:03 PM   #4
akelder
Member
 
Registered: Jan 2007
Distribution: debian on servers, ubuntu on desktops/laptops
Posts: 45

Original Poster
Rep: Reputation: 16
And also getting rid of that last "shift" after "esac"..
 
Old 03-21-2011, 10:11 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Well that does work, but I am usually a proponent for using commands as few times as possible.
So the other way to look at it is that you leave the shift after the esac but remove from all but '-c' option as it
is the only one that requires more than a single shift.

You may also want to have a look at the getopts option, although long command options are not supported.
 
1 members found this post helpful.
Old 03-21-2011, 11:27 PM   #6
akelder
Member
 
Registered: Jan 2007
Distribution: debian on servers, ubuntu on desktops/laptops
Posts: 45

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by grail View Post
leave the shift after the esac but remove from all but '-c' option as it
is the only one that requires more than a single shift.
Oh, yeah, that's definitely better. Thanks!
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] shell: give a range argument to 'for' zedmelon Programming 2 02-20-2010 01:56 PM
execute script shell with regex as argument eguider Linux - Newbie 2 12-15-2009 07:26 PM
Possible to pass an argument from one shell env to another? ne00 Programming 4 04-15-2009 09:19 PM
message sending failed : Error[22 ] invalid argument .....but each and every argument rakeshranjanjha Linux - Software 2 01-07-2008 11:22 PM
Shell Scripting - use the output of a command as an argument for another timgiffney Programming 6 04-17-2006 11:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration