LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to add multiple flags in bash script (https://www.linuxquestions.org/questions/programming-9/how-to-add-multiple-flags-in-bash-script-760323/)

manya 10-07-2009 09:45 AM

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

centosboy 10-07-2009 09:49 AM

Quote:

Originally Posted by manya (Post 3711035)
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?

manya 10-07-2009 09:54 AM

I tried that using while and shift but dont know whats going wrong. Do you have syntaxes or template codes?

centosboy 10-07-2009 10:01 AM

Quote:

Originally Posted by manya (Post 3711052)
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

manya 10-07-2009 10:16 AM

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

catkin 10-07-2009 10:27 AM

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.

gnashley 10-07-2009 10:41 AM

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


manya 10-07-2009 09:55 PM

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.

gnashley 10-08-2009 12:01 AM

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


chrism01 10-08-2009 12:42 AM

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

catkin 10-08-2009 08:27 AM

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



All times are GMT -5. The time now is 11:08 PM.