Tag: while

  • Read a text file with bash line by line

    Read a text file in bash line by line with while. It will read till the end of line. You can modify the end of line setting a new value to EOF variable.

    while read line; do echo "${line}"; done < <(cat file.lst)

  • Shell script menu

    This is an example of how to create a bash menu using while and read.

    #!/bin/sh
    echo "Use one of the following options"
    echo " d : run date command"
    echo " : "
    echo " q: quit this menu"
    echo "enter your choice or type "q" to quit : \c"
    read option
    while [ "$option" != "q" ]
    do
    case $option in
    d ) date
    shift
    echo "enter your choice or type "q" to quit : \c"
    read option
    ;;
    )
    shift
    echo "enter your choice or type "q" to quit : \c"
    read option
    ;;

    # ...

    * )
    echo "invalid choice!"
    shift
    echo "enter your choice or type "q" to quit : \c"
    read option
    ;;
    q ) exit 0
    ;;
    esac
    done
    exit 0