-2

little script

#!/bin/sh

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
else
    echo No
    goto?!? jumpto?!? urandom line
fi

it reads from urandom and ask if this ok, if yes the script end.

if not yes, i will read and ask again - how to do this?

do i need goto? or jumpto or anything other?

5
  • 1
    I'm sorry, but this is impossible to understand. Please edit your question and explain what you need. In full sentences. Are you looking for a while loop, perhaps? There is no "goto" or "jumpto" in bash.
    – terdon
    Commented Jul 10 at 8:45
  • 2
    Your script explicitly invokes sh but your question title mentions bash. Which do you want?
    – DopeGhoti
    Commented Jul 10 at 8:56
  • @terdon There is the hack at bobcopeland.com/blog/2012/10/goto-in-bash . Commented Jul 10 at 9:07
  • 6
    @LjmDullaart let's pretend that doesn't exist. Please! :)
    – terdon
    Commented Jul 10 at 9:25
  • 1
    I don't think I've ever had to think so hard to understand what an if something is y or Y condition was doing before.
    – bxm
    Commented Jul 10 at 13:00

1 Answer 1

3

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

4
  • The builtin read in POSIX sh does not support the -p, flag/option so there's that. Also some system has /bin/sh pointing to /bin/bash or the likes.
    – Jetchisel
    Commented Jul 10 at 10:30
  • 2
    @Jetchisel: If bash is invoked with the name sh, it will try to mimic the startup behavior of historical versions of sh and will be more strictly POSIX Commented Jul 10 at 10:44
  • 2
    or break to break out of the loop, without exiting the whole script
    – ilkkachu
    Commented Jul 10 at 10:59
  • ...or return to exit a function withing a script, or put the whole thing in while ! [[ "$answer" =~ ^(y|Y) ]; do....done or ....
    – symcbean
    Commented Jul 10 at 11:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .