Chapter 3 Standard Input

1.1 Getting Input from a File

Use input redirection,indicated by the < character, to read data from a file.

[maxwell@MaxwellDBA test]$ cat another.file 
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefghi
abcdefghij
[maxwell@MaxwellDBA test]$ wc < another.file
10 10 65
[maxwell@MaxwellDBA test]$ 

1.2 Keeping Your Data with Your Script

Use a here-document, with the << characters,redirecting the text from the command line rather than from a file.

[maxwell@MaxwellDBA test]$ cat ext.sh
#!/bin/bash
#
# 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
[maxwell@MaxwellDBA test]$ sh -x ext.sh bill
+ grep bill
bill x.919
[maxwell@MaxwellDBA test]$ sh -x ext.sh 555
+ grep 555
sue x.555
[maxwell@MaxwellDBA test]$ 

1.3 Preventing Weird Behavior in a Here-Document

[maxwell@MaxwellDBA test]$ cat donors.sh
#!/bin/bash
#
# simple lookup of our generous donors
#
grep $1 <<EOF
# name amt
pete $100
joe  $200
sam  $ 25
bill $ 9
EOF
[maxwell@MaxwellDBA test]$ ./donors.sh bill
pete bill00
bill $ 9
[maxwell@MaxwellDBA test]$ ./donors.sh pete
pete pete00
[maxwell@MaxwellDBA test]$ 

1.4 Indenting Here-Documents

Use <<- and then you can use tab character  at the begining of lines to indent this portion of your shell script.

[maxwell@MaxwellDBA test]$ cat myscript.sh
#!/bin/bash
...
    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
...
[maxwell@MaxwellDBA test]$ 

1.5 Getting User Input

Use the read statement

[maxwell@MaxwellDBA test]$ read

[maxwell@MaxwellDBA test]$ read -p "answer me this " ANSWER
answer me this
[maxwell@MaxwellDBA test]$ read PRE MID POST

[maxwell@MaxwellDBA test]$

1.6 Getting Yes or No Input

[maxwell@MaxwellDBA test]$ cat func_choose.sh
#!/bin/bash

# cookbook filename: func_choose

# Let the user make a choice about something and execute code based on
# the answer
# Called like: choose <default (y or n)><prompt><yes action> <no action>
# e.g. choose "y" \
#      "Do you want to play a game?" \
#      /usr/games/GlobalThermonucularWar \
#      'printf "%b" "See you later Professor Falkin."' >&2
#Return: nothing
#
function choose {
   
   local default="$1"
   local prompt="$2"
   local choice_yes="$3"
   local choice_no="$4"
   local answer

   read -p "$prompt" answer
   [ -z "$answer" ] && answer="$default"

   case "$answer" in
       [yY1] ) exec "$choice_yes"
          # error check
          ;;
       [nNo] ) exec "$choice_no"
          #error check
          ;;
       *    ) printf "%b" "Unexpected answer '$answer'!" >&2 ;;
   esac
} # end of function choose
[maxwell@MaxwellDBA test]$ 

1.7 Selecting from a List of Options

[maxwell@MaxwellDBA test]$ cat select_dir.sh
#!/bin/bash

#cookbook filename: select_dir

directorylist="Finished $(ls /)"

PS3='Directory to process? ' # set a useful select prompt
util [ "$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

1.8 Prompting for a Password

The -s option tells the read command not to echo the characters typed(s is for slient)

The -p option says that the next argument is the prompt to be displayed prior to reading input.

we follow read with a printf to print out a newline. The printf is necessary beacuse read -s turns off the echoing of characters.

[maxwell@MaxwellDBA test]$ read -s -p "password: " PASSWD ; printf "%b" "\n"
password: 
[maxwell@MaxwellDBA test]$ 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值