当前位置:网站首页>Chapter 3 Standard Input
Chapter 3 Standard Input
2022-07-23 10:37:00 【Dreamer 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]$
边栏推荐
- UE5 官方案例Lyra全特性详解 6.生成防御塔
- MySQL query optimization - detailed explanation
- 2. Judgment statement
- 2022/7/21
- 序列模型(二)- 自然语言处理与词嵌套
- Data warehouse: workflow design and Optimization Practice
- [c#] IEnumerable可枚举类型接口分析yield
- PowerBI入门指南
- [pytorch] the difference between cuda() and to (device)
- Redis transaction - detailed implementation process of seckill case simulation
猜你喜欢

元宇宙浪潮震撼来袭,抓住时机,齐心协力

22. Notes on the use of combobox in WPF

CS5266+MA8621做TYPEC转HDMI+PD+U3+2U+SD/TF七合一拓展坞方案设计|CS5266多口拓展坞PCB+原理图参考

Unityc realizes the conversion of Chinese characters to Pinyin - using Microsoft chspinyinconv Library

Information security is in danger, and it is urgent to control the leakage of enterprise data assets

UnityC#实现中文汉字转拼音-使用微软CHSPinYinConv库

Introduction to partition operators, broadcast variables and accumulators of 32 spark

赛尔运维:高校IT运维服务新样本

数据湖:Apache Iceberg介绍

阿里云如何将一个域名解析到另一个域名上
随机推荐
PyQt5_QListWidget分页多选控件
CV (3)- CNNs
CS5266+MA8621做TYPEC转HDMI+PD+U3+2U+SD/TF七合一拓展坞方案设计|CS5266多口拓展坞PCB+原理图参考
openvino_ datawhale
Unity Image中Sprite和overrideSprite区别(转载)
CLion + MinGW64配置C语言开发环境 Visual Studio安装
MySQL queries all table names and column information of the database through SQL
SeekTiger的Okaleido有大动作,生态通证STI会借此爆发?
网络数据泄露事件频发,个人隐私信息如何保护?
Accessory mode
Kingbasees SQL language reference manual of Jincang database (8. Function (6))
LeetCode每日一题(1946. Largest Number After Mutating Substring)
CV (3)- CNNs
Cs5266+ma8621 do the scheme design of typec to hdmi+pd+u3+2u+sd/tf seven in one expansion dock | cs5266 multi port expansion dock pcb+ schematic diagram reference
Network data leakage events occur frequently, how to protect personal privacy information?
行业洞察|如何更好地建设数据中台?IT和业务要“齐步走”
缓存穿透、缓存击穿、缓存雪崩
8.< tag-动态规划和LCS问题>lt.300. 最长递增子序列 + lt.674. 最长连续递增序列
Chapter 4 Executing Commands
chrome selenium 用默认profile 不必每次清空