Solved

need a quick help to fix the script logic

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

16 Experts available now in Live!

Get 1:1 Help Now