CP3 bash shell Standard Input

1.Getting Input from a File

Problem: you want your shell command to read data from a file.

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

2.keeping Your Data with Your Script

Problem: you need input to your script,but don't want a separate file.

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

 

 

 

3.Preventing Weird Behaviour in a Here-Document

Problem: Your here-document is behaving weirdly.You tried to maintain a simple list of donors using the method described previously for phone numbers.

Solution: Turn off the shell scripting features inside the here-document by escaping any or all of the characters in the ending marker.

 

4.Indenting Here-Documents

Problem: The here-document is great,but it's messing up your shell script's formatting.You want to be able to indent for readability.

Solution: Use <<- and then you can use tab character(only!) at the beginning of lines to indent this portion of your shell script.

 

5.Getting User Input

Problem: You need to get input from the user.

Solution: Use the read statement read or read -p "answer me this " ANSWER or read PRE MID POST

 

6.Getting Yes or No Input

Problem: You need to get simple yes or no input from the user,and you want to be as user-friendly as possible.In particular,you do not want to be case sensitive,and you want to provide a useful default if the user presses the Enter key.

Solution: use this self-contained function.

7.Selecting from a List of Options

Problem: You need to provide the user with a list of options to choose from and you don't want to make them type any more than necessary.

Solution: use bash's built-in select construct to generate a menu,then have the user choose by typing the number of the selection

 

 

8.Prompting for a Password

Problem: You need to prompt the user for a password,but you don't want it echoed on the screen.

Solution: read -s -p "password:  " PASSWD

                 printf "%b" "\n"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值