I am taking input from the user, and wants that if user give empty input , it should display the error message , I am trying below code , please correct me where I am wrong .
#!/bin/bash
echo "please enter previous version number"
read prevversion
echo "please enter new version number"
read newversion
echo "please enter environment [DEV2/QAT2/PPE2/PRD]"
read env
if[["$env"=" " &&"$prevversion"=" " &&"$newversion"= " "]]
then
echo "Please enter the details and try again"
else
if [ "$env" = "QAT2" ]
then
prevversion="$prevversion"_QAT2
echo "previous version is $prevversion"
newversion="$newversion"_QAT2
echo "new version is $newversion"
env="$env"
echo "enviornment is $env"
elif [ "$env" = "PPE2" ]
then
prevversion="$prevversion"
echo "previous version is $prevversion"
newversion="$newversion"
echo "new version is $newversion"
env="$env"
echo "enviornment is $env"
elif [ "$env" = "PRD" ]
then
prevversion="$prevversion"
echo "previous version is $prevversion"
newversion="$newversion"
echo "new version is $newversion"
env="$env"
echo "enviornment is $env"
elif [ "$env" = "DEV2" ]
then
prevversion="$prevversion"_DEV2
echo "previous version is $prevversion"
newversion="$newversion"_DEV2
echo "new version is $newversion"
env="$env"
echo "enviornment is $env"
fi
fi
please provide me with correct solution
your are using '&&' in your if-condition which means, that the error-message is only displayed when every(!) input is empty:
if[["$env"=" " &&"$prevversion"=" " &&"$newversion"= " "]]
You should change it like that:
if [ -z "${env}" ] || [ -z "${prevversion}" ] || [ -z "${newversion}" ]; then
echo "either one of the vars is empty"
exit 1
fi
You need to use ||, not &&. And you should be testing against an empty string, not a string with a single space.
if [[ "$env" = "" || "$prevversion" = "" || "$newversion" = "" ]]
Change your code in the following way:
if [ -z "$env" ] || [ -z "$prevversion" ] || [ -z "$newversion" ]; then
echo "either one of the vars is empty"
exit 1
fi
this will check that at least one of the tree var is empty.
If this is the case print your error message and exit the script with return code 1 to show that the script exit in error.
Use -z to check for an empty string, and don't repeat so much code.
#!/bin/bash
# Prompt for what should be command line arguments. If
# the script is going to be used in a batch environment to read
# from a stream, then prompting is unhelpful. If it is used
# in an interactive environment, it is easier to pass this
# data on the command line.
echo "please enter previous version number"
read prevversion
echo "please enter new version number"
read newversion
echo "please enter environment [DEV2/QAT2/PPE2/PRD]"
read env
if test -z "$env" || test -z "$prevversion" || test -z "$newversion"; then
echo "Please enter the details and try again" >&2
exit 1
fi
case "$env" in
QAT2)
prevversion="$prevversion"_QAT2
newversion="$newversion"_QAT2
;;
PPE2)
prevversion="$prevversion"
newversion="$newversion"
;;
PRD)
prevversion="$prevversion"
newversion="$newversion"
;;
DEV2)
prevversion="$prevversion"_DEV2
newversion="$newversion"_DEV2
;;
esac
echo "previous version is $prevversion"
echo "new version is $newversion"
echo "enviornment is $env"
Related
I set a prompt to get user input Yy/Nn. Then I check if the response matches Y/y or N/n. However everytime it evaluates to true on "$prompt_1"="Y"/"y" even when you answer N/n. Now I am sure there is a reason why, however searching around brought me to a few solutions IG: Qoutes around the variables. But nothing has helped to resolve this.
#!/bin/bash
clear;
echo "Would you like to proceed with installation?"
read prompt_1;
echo $prompt_1;
if [ "$prompt_1"="Y" ] || [ "$prompt_1"="y" ]; then
echo "You've accepted installation";
elif [ "$prompt_1”="N"] || [ “$prompt_1"="n" ]; then
exit;
fi
You need to add spaces around the = operator:
if [ "$prompt_1" = "Y" ] || [ "$prompt_1" = "y" ]; then
echo "You've accepted installation";
elif [ "$prompt_1" = "N" ] || [ "$prompt_1" = "n" ]; then
exit;
fi
Or using the pattern-matching operator =~ in a [[...]] expression:
if [[ "$prompt_1" =~ ^[Yy]$ ]]; then
echo "You've accepted installation";
elif [[ "$prompt_1" =~ ^[Nn]$ ]]; then
exit
fi
The case statement solution.
case $prompt_1 in
[Yy]) echo "You've entered $prompt_1 and accepted installation."
##: do something here
;;
[Nn]) echo "You entered $prompt_1 and did not accepted the installation."
exit
;;
*|'') echo "Unknown option ${prompt_1:-empty}." ##: not [Nn] or [Yy] or empty.
exit
;;
esac
That is not restricted to bash anymore, it should work on other POSIX sh shells too.
Another suggestion, using case and lower case substitution:
case ${prompt_1,,} in
y|yes|"just do it")
echo "You've accepted installation"
# perform installation
;;
n|no|forget) exit;;
*) echo "Wrong answer, try again";;
esac
PS: Works in bash4 or later.
Problem:
I need to make this bash script to choose based on the user inputs. Example how can i add choices? such that user just select from 1 to 3 and that is set in variable CLUSTER_NAME.
choices are test.com, try.com and me.com
Script
#!/bin/bash
sops_ops() {
sops --version
if [ "$?" -eq "0" ]; then
echo "proceed sops ops"
else
echo "check sops binary"
fi
read -p 'Enter cluster_NAME: = ' CLUSTER_NAME
test_environment="test.com"
test1_environment="test1.com"
test2_environment="test2.com"
case "${$CLUSTER_NAME}" in
prod.$test_environment) ;;
dev.$test1_environment) ;;
stage.$test2_environment) ;;
test.$test_environment) ;;
*) echo "Invalid option: ${CLUSTER_NAME}" 1>&2 && exit 1 ;;
if [ $CLUSTER_NAME = test.$test_env ];then
printf "got cluster $CLUSTER_NAME"
elif [ $CLUSTER_NAME = "test.test.com" ];then
printf "got dev cluster $CLUSTER_NAME"
echo "not found cluster"
else
echo "Environment not available"
fi
}
sops_ops
Question:
How do I do that?
Any help is appreciated!
So I have this little problem. I'm not sure where it went wrong because I'm pretty sure I got the code right.
Here's the code:
#!/bin/bash
playerHP=100
echo "Hello World"
echo "HP: $playerHP"
echo "Continue? (Y/N):"
read -p $confirm
if [ "$confirm" = "y" ]
then
echo "Yes"
elif [ "$confirm" = "n" ]
then
echo "No"
else
echo "No such command"
fi
Here's the result:
Unrelated: read needs a prompt after -p. Blend the previous echo into it, and while at it, remove the $ from the variable name there.
read -p "Continue? (Y/N):" confirm
The error message is confusing. Don't you have MSWin line ends in the script?
Hi I have modified your script below use it. Working fine for me
#!/bin/bash
playerHP=100
echo "Hello World"
echo "HP: $playerHP"
read -p "Continue? (Y/N): " confirm
echo $confirm
if [ "$confirm" = "y" ]
then
echo "Yes"
elif [ "$confirm" = "n" ]
then
echo "No"
else
echo "No such command"
fi
I'm trying to implement a prev option in my bash script to go back to the previous "menu", as well as a way for the script to ask for user input again if no variable is set for $name.
Heres my bash script:
#!/bin/bash
#Menu() {
for (( ; ; ))
do
beginORload=
echo "Choose option:"
echo "1 - Begin"
echo "2 - Load"
read -p "?" beginORload
#}
#Begin() {
if [ "$beginORload" -eq "1" ]
then
clear
for (( ; ; ))
do
echo "Beginning. What is your name?"
read -p "?" name
#If "prev" specified, go back to #Menu()
if [ "$name" -eq "prev" ]
then
Menu
fi
#If nothing specified, return to name input
if [ -z ${name+x} ]
then
Begin
else
break
fi
echo "Hi $name !"
done
fi
done
In batch, I could simply do:
:menu
echo Choose option:
echo 1 - Begin
echo 2 - Load
[...]
:begin
[...]
if "%name%==prev" goto menu
if "%name%==" goto begin
the issue is I keep running into errors all over the place, and I can't figure out what to type to get it to work
im running Yosemite btw. Thankyou
Something like this is close to what you expect:
while [[ $answer -ne '3' ]];do
echo "Choose option:"
echo "1 - Begin"
echo "2 - Load"
echo "3 - Exit"
read -p "Enter Answer [1-2-3]:" answer
case "$answer" in
1) while [[ "$nm" == '' ]];do read -p "What is your Name:" nm;done # Keep asking for a name if the name is empty == ''
if [[ $nm == "prev" ]];then nm=""; else echo "Hello $nm" && break; fi # break command breaks the while wrapper loop
;;
2) echo 'Load' ;;
3) echo 'exiting...' ;; # Number 3 causes while to quit.
*) echo "invalid selection - try again";; # Selection out of 1-2-3 , menu reloaded
esac # case closing
done # while closing
echo "Bye Bye!"
As a general idea you can wrap up your case selection in a while loop which will break under certain circumstances (i.e If Option 3 is selected or if a valid name is given (not blank - not prev)
PS1: In bash you compare integers with -eq , -ne, etc but you compare strings with == or !=
PS2: Check the above code online here
Why is it the code on "if" statement will keep looping if null is input to "title" variable but for the case statement, my script will have error?
echo -n "Title :"
read title
if [ -z "$title" ]; then
echo "Please input a title"
while [[ -z "$title" ]] ; do
echo -n "Title: "
read title
done
fi
read author
case "$author" in
*[0-9,\""\!##$%\(\)]*) echo "Please enter a name" ;;
while *[1-9,\""\!##$%\(\)]*)"$author" ]] ; do
echo -n "author "
read author
esac
The line
while *[1-9,\""\!##$%\(\)]*)"$author" ]] ; do
wouldn't be allowed there even if there weren't a bunch a syntax errors in it. Try this idiom instead:
title=
while [ -z "$title ]
do
read -p "What is the title? " title
done
You have \"" and should be only \" . That's the syntax error
Also, you can't put a While as a case. Try this and add any cases you want
while [ -z $author ]; do
echo "Please enter an author: "
read author
case "$author" in
*[0-9,\"\!##$%\(\)]*) echo "First case - $author"
;;
esac
done