LinuxQuestions.org
Visit Jeremy's Blog.
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 10-07-2009, 09:45 AM   #1
manya
Member
 
Registered: Apr 2004
Posts: 194

Rep: Reputation: 15
How to add multiple flags in bash script


Hi Guys,

I got a bash script and I need to add two flags for that. I have already achieved the same with one flag using shift command but how do I do the same thing for 2 flags.

my command syntax would be

convertRoute -i <input file> -o <output file>

Thanks,
Manya
 
Old 10-07-2009, 09:49 AM   #2
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by manya View Post
Hi Guys,

I got a bash script and I need to add two flags for that. I have already achieved the same with one flag using shift command but how do I do the same thing for 2 flags.

my command syntax would be

convertRoute -i <input file> -o <output file>

Thanks,
Manya
you could use $1 and $2 in the script?
 
Old 10-07-2009, 09:54 AM   #3
manya
Member
 
Registered: Apr 2004
Posts: 194

Original Poster
Rep: Reputation: 15
I tried that using while and shift but dont know whats going wrong. Do you have syntaxes or template codes?
 
Old 10-07-2009, 10:01 AM   #4
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by manya View Post
I tried that using while and shift but dont know whats going wrong. Do you have syntaxes or template codes?


Code:
#!/bin/bash
result=`echo "blablah;" |nc $1  11111|grep -i $2 |gawk -F, '{print $2}'|tail -n1`
if [ $result -gt "10000" ]; then
echo CRITICAL : $1 : $2 is $result
exit 2
elif [ $result -gt "5000" ]; then
echo WARNING : $1 : $2 is $result
exit 1
else
        echo OK : $1 : $2 Queue is $result
        fi

the above example requires 2 strings ($1 and $2) before it can work


Code:
sh script.sh string1 string2

Last edited by centosboy; 10-07-2009 at 10:03 AM.
 
Old 10-07-2009, 10:16 AM   #5
manya
Member
 
Registered: Apr 2004
Posts: 194

Original Poster
Rep: Reputation: 15
Nope this is not my requirement. I'll forward the test script that I managed to wrote so that you'll get a fair idea about it
 
Old 10-07-2009, 10:27 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Yes, it will help to see your script to better understand what you want to do. Have you considered using getopts? It's intended for parsing command line options. Takes a little understanding but very sweet when you've got it.
 
Old 10-07-2009, 10:41 AM   #7
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
I use a setup like this:
Code:
for opt in $@
  case $opt in
    -i) input_file=$2 ; shift 2 ;;  # reading $2 grabs the *next* fragment
    -o) output_file=$2 ; shift 2 ;; # shift 2 to get past both the -o and the next
  esac
Or, if you want to use syntax like -i=input_file
Code:
for opt in $@
  case $opt in
    -i=) input_file=${opt:3} ; shift 1 ;;
    -o=) output_file=${opt:3} ; shift 1 ;;
  esac
 
Old 10-07-2009, 09:55 PM   #8
manya
Member
 
Registered: Apr 2004
Posts: 194

Original Poster
Rep: Reputation: 15
There you are, this is kinda i wanna achieve it. But what's happening here is while using conventional method I was not able to shift parameter which comes after $4. which should shift to $2.

Here is the thing but its not complete at least you will get a fair idea about it.

if [ "$1" = "-i" ];then
"$3"="-0"
shift 2
fi
if [ "$4" = "-o" ];then
shift 1
fi
RUN #----- RUN is my function
done

I am doing something wrong here but not able to figure that out what that is.
 
Old 10-08-2009, 12:01 AM   #9
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
When you shift, the option locations change. If you have 4 arguments $1, $2, $3 and $4, when you shift 1, then $2 becomes 1$, $3 becomes 2 etc.
And this is correct syntax for anything:
"$3"="-0"
Put some echo statements in there so you can see what is happening:
Code:
#!/bin/bash

for opt in $@
  case $opt in
    -i=) input_file=${opt:3} ; echo $1 ; shift 1 ; echo $1 ;;
    -o=) output_file=${opt:3} ; echo $1 ; shift 1 echo $1 ; ;;
  esac
done
 
Old 10-08-2009, 12:42 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,355

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Don't shift the params; just use them as $1 $2 $3 etc.
If you want to use options with switches eg

blah.sh -i one -o two -t three

the use getopts http://tldp.org/LDP/abs/html/internal.html#EX33
 
Old 10-08-2009, 08:27 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Why roll your own when there's a tool for the job. Something like this should work (not tested)
Code:
    lf=$'\n'
    while getopts i:o: opt 2>/dev/null
    do
        case $opt in
            i)
                in_fn="$OPTARG"
                ;;
            o)
                out_fn="$OPTARG"
                ;;
            * )
                emsg="${lf}Invalid option '$opt'"
        esac
    done

    # Test for extra arguments
    # ~~~~~~~~~~~~~~~~~~~~~~~~
    shift $(( $OPTIND-1 ))
    if [[ $* != '' ]]; then
        emsg="${lf}Invalid extra argument(s) '$*'"
    fi

    # Test for mandatory options not set
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if [[ $in_fn = '' ]]; then
        emsg="${lf}Mandatory option -i not given"
    fi
    if [[ $out_fn = '' ]]; then
        emsg="${lf}Mandatory option -o not given"
    fi

    # Report any errors
    # ~~~~~~~~~~~~~~~~~
    if [[ $emsg != '' ]]; then
        echo "$emsg" >&2
        usage  # a function that prints the usage, can usefully be called with option -h
        \exit 1
    fi
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
add multiple users(Script) amer_58 Programming 22 02-22-2013 09:09 AM
how to add numbers in a formatted file via a bash script? zero79 Linux - General 8 12-24-2010 05:48 PM
how to have bash script use flags neocontrol Programming 7 02-08-2007 09:05 AM
add user bash script noir911 Programming 4 08-13-2005 08:24 AM
Bash script: add all numbers from command output wi-Z-art Programming 2 08-06-2003 09:16 AM

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

All times are GMT -5. The time now is 02:43 AM.

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