Solved

need a quick help to fix the script logic

Posted on 2016-09-09
4
63 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

1. Introduction As many people are interested in Linux but not as many are interested or knowledgeable (enough) to install Linux on their system, here is a safe way to try out Linux on your existing (Windows) system. The idea is that you insta…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now