Solved

need a quick help to fix the script logic

Posted on 2016-09-09
4
69 Views
Last Modified: 2016-09-12
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
0
Comment
Question by:The Rock
  • 2
  • 2
4 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 41791128
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.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 41791149
This should better fit what's claimed in the introduction:

if [ $# -eq 2 ]; then
        if [[ $2 == "yes" || $2 == "no" ]]; then
        echo  Continuing with the main script
        else
        echo "Second parameter must be yes or no"
        echo "Please pass arguments like sample - Sample: $0 abc yes"
        exit 0;
        fi
else
    echo "Invalid argument please pass only 2 arguments"
    echo "Sample: $0 abc yes"
    echo "Sample: $0 abc no"
    exit 0;
fi
echo "Param1 entered: $1"
echo "Param2 entered: $2"
echo Main Script continued here
1
 

Author Comment

by:The Rock
ID: 41793155
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
0
 

Author Closing Comment

by:The Rock
ID: 41795470
Thanks a lot above solution just work fine with me. Thank you!!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Mysql Crashing Intermittently 16 103
Using a variable to name output in powershell script 2 37
Centos 6 User Can't Assign Password 2 41
linux(debian) mouse poor performance 4 39
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question