As I understand it, you want to execute an infinite loop containing your script, and exit from the script when the answer to the prompt is y
.
The way to create an infinite loop in bash
is using while true
:
while true ; do
# script
done
The way to exit from a script is using the appropriately named exit
.
That would make your script:
#!/bin/sh
while true ; do
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 24 | head -n 4
printf 'do you like this? (y/n)? '
read answer
if [ "$answer" != "${answer#[Yy]}" ] ;then
echo Yes
exit 0
else
echo No
fi
done
There are a number of other remarks for your consideration.
The #!/bin/sh
ensures that the script runs under sh
and not under bash
. If you want to use bash
, change the line to #!/bin/bash
.
read
is able to provide the prompt for you. You do not need a printf
for that.
read -p 'do you like this? (y/n)? ' answer
As if
condition, I would (in bash
, sh
does not support this):
if [[ $answer =~ ^[yY] ]]; then
That is more readable (I think).
while
loop, perhaps? There is no "goto" or "jumpto" in bash.sh
but your question title mentionsbash
. Which do you want?if something is y or Y
condition was doing before.