I have a very simple BASH script that asks the user if they would like to enter some input. The response is y or n
If they enter n the script exits.
If they enter y then they are asked some questions. At the end I want to ask them to confirm the input.
If it's correct we'll carry on, if not I want to return them to the questions to re enter the information.
So far I've got:
while true; do
echo -e "Enter details ? "
read yn
case $yn in
[Nn]* ) exit;;
[Yy]* )
echo -e "description"
read desc
echo -e "Address"
read address
* ) echo "Please answer yes or no.";;
esac
done
This seems to work fine, so I've tried to add the confirmation.. but thats not working.
while true; do
echo -e "Enter details ? "
read yn
case $yn in
[Nn]* ) exit;;
[Yy]* )
echo -e "description"
read desc
echo -e "Address"
read address
echo -e "$desc - $address"
while true; do
echo -e "\nAre these details correct ? "
read conf
case $conf in
[Nn]* ) // return to enter details //
[Yy]* ) // carry on with the details entered // break;;
esac
done
* ) echo "Please answer yes or no.";;
esac
done
The aim is as follows
The user selects y and enters description and address. The page shows them a summary of what they've entered and asks them are these details correct ? if they are we move on and the script progresses. The the input is wrong then the script returns them to enter description followed by enter address.
Can someone advise how to do this.
Thanks
You could add one more loop level and use the optional level indicator of break [n]:
while true; do
echo -e "Enter details ? "
read yn
case $yn in
[Nn]*) exit;;
[Yy]*)
while true; do
echo -e "description"
read desc
echo -e "Address"
read address
echo -e "$desc - $address"
while true; do
echo -e "\nAre these details correct ? "
read conf
case $conf in
[Nn]* ) break 1;;
[Yy]* ) break 3;;
esac
done
done;;
*) echo "Please answer yes or no.";;
esac
done
Don't use case for this.
Script for example for you:
#!/bin/bash
while true; do
echo -e "Enter details ?(y/n)"
read yn
if [[ $yn == "y" ]];then
echo -e "description"
read desc
echo -e "Address"
read address
echo -e "$desc - $address"
while true; do
echo -e "\nAre these details correct ? (y/n)"
read conf
if [[ $conf == "y" ]];then
echo "All done"
break
elif [[ $conf == "n" ]];then
break
else
continue
fi
done
elif [[ $yn == "n" ]];then
break
else
continue
fi
There you go:
DONE=""
while [ "$DONE" != "true" ]; do
echo -e "Enter details ? "
read yn
case $yn in
[Nn]* ) exit;;
[Yy]* )
echo -e "description"
read desc
echo -e "Address"
read address
echo -e "$desc - $address"
while true; do
echo -e "\nAre these details correct ? "
read conf
case $conf in
[Nn]* )
break;;
[Yy]* )
echo "doing something with your values"
DONE="true"
break;;
esac
done
;;
* ) echo "Please answer yes or no.";;
esac
done
Related
I have a script that substitutes a variable with another value, depending on the input:
#!/bin/bash
prompt()
{
while true; do
read -p "Do you wish to install this program? " "ANSWER"
case "$ANSWER" in
[Yy]* ) printf -v "$1" %s "true"; break;;
[Nn]* ) printf -v "$1" %s "false"; break;;
* ) echo "Please answer yes or no.";;
esac
done
}
prompt "QUESTION"
if [ "$QUESTION" = "true" ]; then
echo "SUCCESS"
elif [ "$QUESTION" = "false" ]; then
echo "FAILURE"
fi
This works fine, though i want the script to be POSIX compliant. I use #!/bin/sh for all my scripts, though printf -v is bashism. How can i modify this program? Is there an equivalent function i could use? Thanks!
read itself can set the variable whose name is in $1. However, you still need read ANSWER first, so that you can examine the response. Once it's done, you can use read and a here-document to transfer the value of $ANSWER to whatever variable prompt requests.
prompt () {
while :; do
printf "Do you wish to install this program? " >&2
read ANSWER
case $ANSWER in
[Yy]* ) ANSWER=true ; break ;;
[Nn]* ) ANSWER=false; break ;;
* ) printf 'Please answer yes or no.\n' >&2 ;;
esac
done
read "$1" <<EOF
$ANSWER
EOF
}
You can use a command substitution to ensure the ANSWER is not set in the global environment.
prompt () {
read "$1" <<EOF
$(while :; do
printf "Do you wish to install this program? " >&2
read ANSWER
case $ANSWER in
[Yy]* ) printf true; break ;;
[Nn]* ) printf false; break ;;
* ) printf 'Please answer yes or no.\n' >&2 ;;
esac
done
)
EOF
}
I am writing a korn script to process some fixed/user provided input. I am not able to run this script as I am getting syntax error `)' unexpected. I am thinking that it is because I am using a break inside the if statement, where the if is inside a switch case. I have just picked up scripting last week and would really appreciate any help on this.
P.S There is a reason for using korn script.
#!/usr/bin/ksh93
typeset -A fileset_list #fileset_list is associative
fileset_list=([All filesets]="A B C D E"
[A]=AA
[B]=BB
[C]="CC CCC CCCC CCCC"
[D]=DD
[E]="EE EEE EEEE EEEE EEEEE"
)
fileset="All filesets"
echo "Recent update has found following unsupported filesets on the system:\n${fileset_list["All filesets"]}"
echo "Do you want to delete all the listed filesets along with their dependencies Y/N"
while true; do
read yn
case $yn in
[Yy]* )
set -A delete_list ${fileset_list["All filesets"]}
uninstall_fun
break;;
[Nn]* )
echo "Do you want to delete the partial list Y/N"
while true; do
read y_n
case $y_n in
[Yy]* ) echo "Enter the space separated filesets from the above list for deletion"
read user_list
echo "You have entered $user_list\nIs the list correct Y/N"
read selection
if [[ $selection == [Yy]* ]]
then
set -A delete_list $user_list
uninstall_fun
break;; #<<<<ISSUE
else
echo "Do you want to re-enter list Y/N" #<<<<<need this to go back and read y_n
fi
# break;;
[Nn]* )
break;;
* ) echo "Please answer yes(y) or no(n).";;
esac
done
break;;
* ) echo "Please answer yes(y) or no(n).";;
esac
done
uninstall_fun(){
echo "In uninstall_fun"
}
The ;; is used at the end of each case block. You had one in the middle of the if statement, but the case block is not finished yet.
The line with break;; # <<<<ISSUE should become break # without ;;,
and the line # break;; should be ;;.
[Yy]* ) echo "Enter the space separated filesets from the above list for deletion"
read user_list
echo "You have entered $user_list\nIs the list correct Y/N"
read selection
if [[ $selection == [Yy]* ]]
then
set -A delete_list $user_list
uninstall_fun
break # Without ;;
else
echo "Do you want to re-enter list Y/N" #<<<<<need this to go back and read y_n
fi
;; # Needed here
[Nn]* )
Want to get user's input from a function. However, the prompt ("Please answer y or n." in this case) is also included in the return value.
#!/bin/bash
input() {
while true; do
read -p "input y/n: " yn
case $yn in
[Yy]* ) yn="y"; break;;
[Nn]* ) yn="n"; break;;
* ) echo "Please answer y or n.";;
esac
done
echo $yn
}
val=$(input)
echo "val is: $val"
If input an error value first and here is the results:
input y/n: other
input y/n: y
val is: Please answer y or n.
y
Thanks.
Echo your error to stderr (FD 2), not stdout (the default, FD 1):
echo "Please answer y or n." >&2
Better use a common global variable to pass values between the function and the caller. It's more efficient than summoning subshells with command substitution.
#!/bin/bash
input() {
while true; do
read -p "input y/n: " __
case "$__" in
[Yy]* ) __="y"; break;;
[Nn]* ) __="n"; break;;
* ) echo "Please answer y or n.";;
esac
done
}
input; val=$__
echo "val is: $val"
This is the script and I keep getting an unexpected fi error. What am I missing? .. (I started using [] for the if statement but since I'm using this command I deleted the [] .. Is it ok this way?)
if type "java" 2>&1;
then
echo "All ok . . . ";
exit
else
read -n1 -r -p "Yo need to install"
while true; do
echo "Want to install??"
select yn in "y" "n"; do
case $yn in
y ) echo "Installing here..."; break;;
n ) echo "Ok... stopping..."; exit;;
esac
done
exit
fi
thanks!
while ends with done, not exit. Try this:
if type "java" 2>&1;
then
echo "All ok . . . ";
exit
else
read -n1 -r -p "Yo need to install"
while true; do
echo "Want to install??"
select yn in "y" "n"; do
case $yn in
y ) echo "Installing here..."; break;;
n ) echo "Ok... stopping..."; exit;;
esac # <-- ending `case`
done # <-- ending `select`
done # <-- while ends with `done`, not `exit`!
fi # <-- ending `if`
You have an exit before the last fi; I suppose that should be a done.
I'm following some tutorials here and whenever I execute this, I get
Do you want to create a bukkit server on this computer? (Hint: answer YES or NO) > no
answered no
./test.sh: line 44: syntax error near unexpected token `else'
./test.sh: line 44: 'else'
here's the script:
while true; do
read -p "Do you want to create a bukkit server on this computer? (Hint: answer YES or NO) > " yn
case $yn in
[Yy]* ) echo answered yes; INSTALL="Y"; break;;
[Nn]* ) echo answered no; break;;
* ) echo "Please answer yes or no.";;
esac
done
if [ -z "$INSTALL" ];
echo "Yay!"
else
echo "Sadface!"
fi
i'm a bash newbie :/
You're missing a then keyword after the condition:
if [ -z "$INSTALL" ]; then
echo "Yay!"
else
echo "Sadface!"
fi
You need a then after the if:
while true; do
read -p "Do you want to create a bukkit server on this computer? (Hint: answer YES or NO) > " yn
case $yn in
[Yy]* ) echo answered yes; INSTALL="Y"; break;;
[Nn]* ) echo answered no; break;;
* ) echo "Please answer yes or no.";;
esac
done
if [ -z "$INSTALL" ]; then
echo "Yay!"
else
echo "Sadface!"
fi