Solved

need a quick help to fix the script logic

Posted on 2016-09-09
4
94 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Utilizing an array to gracefully append to a list of EmailAddresses
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

717 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