A Study in Bash Shell : Input

A Study in Bash Shell : Input

Getting Input from a File

$ wc < my.file

Keeping Your Data with Your Script

$ cat ext
#
# here is a "here" document
#
grep $1 <<EOF
mike x.123
joe x.234
sue x.555
pete x.818
sara x.822
bill x.919
EOF
$
It can be used as a shell script for simple phone number lookups:
$ ext bill
bill x.919
$
or:
$ ext 555
sue x.555
$
# solution
grep $1 <<\EOF
pete $100
joe $200
sam $ 25
bill $ 9
EOF
$ cat myscript.sh
...
grep $1 <<-'EOF'
    lots of data
    can go here
    it's indented with tabs
    to match the script's indenting
    but the leading tabs are
    discarded when read
    EOF
ls

Getting User Input

read

Selecting from a List of Options

# cookbook filename: select_dir
directorylist="Finished $(ls /)"

PS3='Directory to process? ' # Set a useful select prompt
until [ "$directory" == "Finished" ]; do
    printf "%b" "\a\n\nSelect a directory to process:\n" >&2
    select directory in $directorylist; do
        # User types a number which is stored in $REPLY, but select
        # returns the value of the entry
        if [ "$directory" = "Finished" ]; then
            echo "Finished processing directories."
            break
        elif [ -n "$directory" ]; then
            echo "You chose number $REPLY, processing $directory ..."
            # Do something here
            break
        else
            echo "Invalid selection!"
        fi # end of handle user's selection
    done # end of select a directory
done # end of while not finished

Prompting for a Password

read -s -p "password: " PASSWD
printf "%b" "\n"