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
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.
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.