Var87
Var87

Reputation: 569

A bash script to repeat the same script action pressing return in terminal over and over again until i press q

I want to make this script repeat the somescript.sh when I click return until I write q. I think I've gotten pretty close, but I can't make it set $actionLoop to 0 if it reads "q" what am I doing wrong here?

#!/bin/bash

    $actionLoop = 1
    while [ ${actionLoop} 1 ]
    do
        echo "do another random review script?"
        sh /somescript.sh
        echo "Done.  Press q to quit."
        read response
        [ $response = "q" ] && $actionLoop = 0
    done

Upvotes: 0

Views: 207

Answers (1)

P.P
P.P

Reputation: 121407

Change

$actionLoop = 1

to

actionLoop=1

Similarly, this line

    [ $response = "q" ] && $actionLoop = 0

to

    [ $response = "q" ] && actionLoop=0

You can't use $ when assigning a variable and you can't have whitespaces around the assignment either.


IMO, you don't need that variable at all. Use an infinite loop and break it when q is given.

while : ;
    do
        echo "do another random review script?"
        sh /somescript.sh
        echo "Done.  Press q to quit."
        read response
        [[ "$response" = "q" ]] && break
    done

I personally prefer bash built-in [[ ]] instead of [ (test) command. Some prefer [ for compatibility with older shells.

Upvotes: 1

Related Questions