Avatar of The Rock
The Rock
Flag for India asked on

need a quick help to fix the script logic

Please help as i want to validate arguments passed while executing the script .

What i want that user can only give values as parameter those are valid as per script.

./script.sh abc xyz                    where abc is a folder location which can be any location and xyz is either yes or no. if user enter any other word apart from yes or no then my message should be displayed and exit there only.

sample given below:

if [ $# -eq 2 ]; then
        if (($test1 == "audit") || ($test1 == "insert")); then
        echo "" or continue with the main script
        else
        echo "please pass arguments like sample - Sample: ./script.sh b1 audit. "
        exit 0;
        fi
else
    echo "invalid argument please pass only 2 argument "
    echo "Sample: ./script.sh b1 audit"
    exit 0;
fi
Linux DistributionsShell ScriptingScripting Languages

Avatar of undefined
Last Comment
The Rock

8/22/2022 - Mon
woolmilkporc

Which is your shell? The script doesn't really look like bash!

Anyway, this should work under bash:

if [ $# -eq 2 ]; then
        if [[ $2 == "audit" || $2 == "insert" ]]; then
        echo "" or continue with the main script
        else
        echo "please pass arguments like sample - Sample: ./script.sh b1 audit."
        exit 0;
        fi
else
    echo "invalid argument please pass only 2 argument "
    echo "Sample: ./script.sh b1 audit"
    exit 0;
fi

Please note that the second  positional command line parameter is "$2", and that the test syntax uses "[..]" or "[[..]]" instead of "(( .. ))" which denotes an arithmetic operation.

By the way, the introduction to your question doesn't really match the sample given.
ASKER CERTIFIED SOLUTION
woolmilkporc

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
The Rock

ASKER
Hi Woolmilkproc ,

Thank you!

Yes might be explanation is not good as per the script given.

But your suggests solution might the same as I need. I will check by tommorrow and revert you .
Thanks
The Rock

ASKER
Thanks a lot above solution just work fine with me. Thank you!!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy