当前位置:网站首页>Chapter 3 Standard Input
Chapter 3 Standard Input
2022-07-23 04:11:00 【梦想家DBA】
1.1 Getting Input from a File
Use input redirection,indicated by the < character, to read data from a file.
[[email protected] test]$ cat another.file
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefghi
abcdefghij
[[email protected] test]$ wc < another.file
10 10 65
[[email protected] 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.
[[email protected] 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
[[email protected] test]$ sh -x ext.sh bill
+ grep bill
bill x.919
[[email protected] test]$ sh -x ext.sh 555
+ grep 555
sue x.555
[[email protected] test]$ 1.3 Preventing Weird Behavior in a Here-Document
[[email protected] 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
[[email protected] test]$ ./donors.sh bill
pete bill00
bill $ 9
[[email protected] test]$ ./donors.sh pete
pete pete00
[[email protected] 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.
[[email protected] 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
...
[[email protected] test]$ 1.5 Getting User Input
Use the read statement
[[email protected] test]$ read
[[email protected] test]$ read -p "answer me this " ANSWER
answer me this
[[email protected] test]$ read PRE MID POST
[[email protected] test]$1.6 Getting Yes or No Input
[[email protected] 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
[[email protected] test]$ 1.7 Selecting from a List of Options
[[email protected] 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.
[[email protected] test]$ read -s -p "password: " PASSWD ; printf "%b" "\n"
password:
[[email protected] test]$
边栏推荐
- After 100 billion of revenue, Alibaba cloud ecosystem has a new way to play
- ANTLR4 入门学习(一):下载和测试
- Flask learning notes
- 客户至上 | 国产BI领跑者,思迈特软件完成C轮融资
- Reverse theoretical knowledge 1
- How to query data differences between two isomorphic tables of massive data
- 科技赋能新保险:中华财险的数字化转型
- What is the difference between College coder and 985 programmer?
- 禅道的甘特图功能是什么
- 配饰器模式
猜你喜欢

Online English learning system based on s2sh+mysql

What is the difference between College coder and 985 programmer?
![[circular statement]](/img/4a/a27beafcbb07394a8192f6ff0a500d.png)
[circular statement]

Sequence model (III) - sequence model and attention mechanism

什么是文件管理软件?你为什么需要它?

Customer first | domestic Bi leader, smart software completes round C financing

【Delphi】制作控件面板安装图标的简单方法(译)

Practice of RTC performance automation tool in memory optimization scenario

SeekTiger的Okaleido有大动作,生态通证STI会借此爆发?

22、wpf之Combobox使用小记
随机推荐
Accessory mode
RTC 性能自动化工具在内存优化场景下的实践
网线水晶头接法图解8根顺序
"Lost wake up problem" in multithreading | why do wait() and notify() need to be used with the synchronized keyword?
Unity Image中Sprite和overrideSprite区别(转载)
SSH supermarket inventory management system
vs中新建文件/筛选器/文件夹
【Qt5.12】Qt5.12安装教程
信息安全危机四伏,企业数据资产泄露管控刻不容缓
Redis replication cluster setup
[Internet of vehicles prototype system II] database + application layer protocol design
Kingbasees SQL language reference manual of Jincang database (8. Function (2))
Idea integrated sonar complete process
腾讯云客户端命令行工具tccli主流程解析
Richview textbox items textbox
[circular statement]
kex_exchange_identification: read: Connection reset by peer 不完美解决办法(之一)
Underlying mechanism of pointer
LeetCode每日一题(1946. Largest Number After Mutating Substring)
How does VirtualBox set up port forwarding?