How do i create an interactive shell script?



  • To make shell scripts more operational, you can take input from the user and display information or use the input for conditional logic.

    The read command is the input taken from the user. The default response to read is stored in a variable called $REPLY, but you can create your own variables, to make it a lot more obvious what you are reading in from the user input.

    Here's the script that reads in input from the user.

    ...
    ...
    echo -n "What is your favourite fruit? "
    read
    echo "It's $REPLY"
    ...
    ...
    

    Running this asks the user to enter their favourite fruit, then prints the message.

    $ ./myfruit.sh
    What is your favourite fruit? pineapple
    It's pineapple

    In operational use, the response can be taken and can be put into if/else conditional logic.

    If you want to have your own variable instead of $REPLY, do this

    ...
    ...
    echo -n "What is your favourite fruit? "
    read fruit
    echo "It's $fruit"
    ...
    ...
    

Log in to reply
 

© Lightnetics 2024