第2章:标准输入与输出

 

 

第2章:标准输入与输出

 

 

 

2.1echo printf终端打印

echo显示命令默认情况每次调用后会添加一个换行符。

echo 使用带单引号时,Bash不会对单引号中的变量(如$var)求值,只会按照原样显示,如果需要显示变量(如$var)的求值,只需要加上双引号或去掉单引号即可。

范例1:

[root@cloucentos6 home]# cat test.sh

#!/bin/bash

var="hello world!"

echo ‘$var’

echo "$var"

echo $var

[root@cloucentos6 home]# ./test.sh

$var

hello world!

hello world!

 

printf显示命令默认情况不会自动换行,需要后面加\n换行符号,同时加上单引号或双引号。

printf 显示命令可以使用格式字符串,如%s、%c、%d、%f等都是格式替代符,其所对应的参数可以置于带引号的格式字符串之后。

范例2:

[root@cloucentos6 home]# cat ./test.sh

#!/bin/bash

printf "%-10s %-10s\n" Name Mark

printf "%-10s %-4.2f\n" Tom 99.9999

printf "%-10s %-4.2f\n"  Key 77.7777

[root@cloucentos6 home]# ./test.sh

Name       Mark     

Tom        100.00

Key        77.78

 

标注:%s输出一个字符串,-10左对齐且宽度为10的字符串替代(  - 表示左对齐)

%f表示按浮点数格式输出, -4.2分别表示 – 表示左对齐,4表示取前面4位数输出,2表示保留小点两位数,每行格式字符串后都需要加一个换行符\n

 

 

 

2.2read接收键盘输入

read  -n 命令从输入中读取n个字符并存入变量var

范例1:限制输入超过4个字符,如果输入超过4个字符直接显示。

[root@cloucentos6 home]# cat test.sh

#!/bin/bash

echo "请输入4个字符以内:"

read -n 4 var1

echo

echo "打印输出:"

echo "$var1"

[root@cloucentos6 home]# ./test.sh

请输入4个字符以内:

3333

打印输出:

3333

 

read –s 命令键盘输入的字符不回显,只显示输出。

范例2:

[root@cloucentos6 home]# cat test.sh

#!/bin/bash

read -s var1

echo $var1

[root@cloucentos6 home]# ./test.sh

222

 

read  -p 显示提示信息

范例3:

[root@cloucentos6 home]# cat test.sh

#!/bin/bash

read -p "请输入你的拼音名字:" var

[root@cloucentos6 home]# ./test.sh

请输入你的拼音名字:zhangshang

 

Read  -d 用定界符结束输入行

范例4:输入%就结束输入

[root@cloucentos6 home]# cat test.sh

#!/bin/bash

read -d "%" var

[root@cloucentos6 home]# ./test.sh

abc

123

%[root@cloucentos6 home]#

 

Read  -t  特定时间内读取输入

范例5:超过3秒未输入任何字符自动结束脚本

[root@cloucentos6 home]# cat test.sh

#!/bin/bash

if

read -t 3 -p "请输入你的拼音名字:" name

then

echo "hello $name"

else

echo

echo "你已超过3秒未输入,已结束输入"

fi

exit 0

[root@cloucentos6 home]# ./test.sh

请输入你的拼音名字:zhangshang

hello zhangshang

[root@cloucentos6 home]# ./test.sh

请输入你的拼音名字:

你已超过3秒未输入,已结束输入

 

 

 

2.3、读取命令序列输出

Shell脚本特性之一就是可以轻松将多个命令或工具组合起来生成输出,一个命令的输出可以作为一个命令的输入,而这个命令的输出又可以传递给另一个命令当成输入,依次类推,这种命令组合的输出可以被存储在一个变量中。

使用管道命令“|”连接每一个过滤器(命令或工具)。

范例1:ls列出当前目录输出当成cat –n的输入内容加上行号,再输出重写向到文件out.txt

[root@cloucentos6 home]# ls | cat -n > out.txt

[root@cloucentos6 home]# cat out.txt

     1     \

     2     lost+found

     3     out.txt

     4     test.sh

 

使用子shell方法也可以实现,子shell本身是独立进程,可以使用 ( ) 操作符定义子shell。

范例2:将ls | cat –n组合命令定义成一个变量cmd,再直接输出变量cmd,添加\n换行符。

[root@cloucentos6 home]# cat test.sh

#!/bin/bash

cmd=$(ls | cat -n)

echo "$cmd\n"

pwd

(cd /home; ls)

pwd

[root@cloucentos6 home]# ./test.sh

     1     \

     2     lost+found

     3     out.txt

     4     test.sh\n

/home

\  lost+found  out.txt   test.sh

/home

 

范例3:通过引用子shell的方式保留空格和换行符,可以添加双引号,以保留空格和换行符 \n

[root@cloucentos6 home]# cat test.sh

#!/bin/bash

cmd="$(ls | cat -n)"

echo "$cmd"

[root@cloucentos6 home]# ./test.sh

     1     \

     2     lost+found

     3     out.txt

     4     test.sh

 

 

 

范例4:使用反引用方法也可以用于存储命令输出。反引号和单引号不是一回事,它位于键盘的 ~ 键上。

[root@cloucentos6 home]# cat test.sh

#!/bin/bash

cmd=`ls | cat -n`

echo "$cmd\n"

[root@cloucentos6 home]# ./test.sh

     1     \

     2     lost+found

     3     out.txt

     4     test.sh\n

 

 

 

2.4、重定向与管道

<    重定向改变标准输入

[root@localhost home]# cat abc.txt

abcdef

[root@localhost home]# tr -d 'f' < abc.txt  #把tr –d ‘f’删除f字符的命令输入到掉abc.txt文本里

abcde  

 

>    重定向改变标准输出,输出的内容会覆盖原来的文件内容

[root@localhost home]# ifconfig > abc.txt     #把ifconfig命令输出的内容输出到abc.txt文本

 

>>   重定向改变标准输出,输出的内容不会覆盖原来的文件内容,只会追加到原来文件内容的后面

[root@localhost home]# ifconfig >> abc.txt    #把ifconfig命令输出的内容输出到abc.txt文本

 

|   管道命令

可以把< 输入 和 > 输出连接起来,可以把两个命令衔接在一起,把第一个命令的标准输出当作第二个命令的标准输入。

[root@localhost home]# ifconfig | grep "inet addr"      #把ifconfig输出的结果通过管道命令给grep当作输入截取以inet addr开头的信息。

inet addr:192.168.0.102  Bcast:192.168.0.255  Mask:255.255.255.0

inet addr:127.0.0.1  Mask:255.0.0.0

 

 

 

2.5、交互输入自动化

交互式输入是指只有当命令要求获取输入时,才由用户手动输入。

范例1:读取交互输入脚本

[root@cloucentos6 home]# cat test.sh

#!/bin/bash

read -p "Enter number:" no

read -p "Enter name:" name

echo you have entered $no $name

[root@cloucentos6 home]# ./test.sh

Enter number:2

Enter name:zhang

you have entered 2 zhang

[root@cloucentos6 home]# echo -e "2\nzhang\n" | ./test.sh

you have entered 2 zhang

 

 

 

2.6、波浪号展开与通配符

Shell有两种与文件名相关的展开。第一种是波浪号展开,另一种全局展开或者路径展开。

 

2.6.1、波浪号展开

         在命令行字符串的第一个字符为波浪号 ~ 或者变量指定(例如PATH或 CDPATH)的值里任何未被引号括起来的冒号之后第一个字符为波浪号 ~ 时,shell变加执行波浪号展开。波浪号展开的目的是将用户根目录的符号型表示方式,改变实际的目录路径。可以采用直接或间接方式执行此程序的用户,如未明白指定,则为当前的用户:

[root@cloucentos6 ~]# ls $HOME  #显示当前用户HOME目录

[root@cloucentos6 ~]# ls ~        #显示当前用户HOME目录

使用波浪号的好处:

这是一种简介的概念表示方式。

这可以避免在程序里把路径名称直接编码。

 

2.6.2、通配符

         寻找文件名里的特殊字符,也是shell提供的服务之一。当它找到这类字符时,会将它们视为要匹配的模式,也就是,一组文件的规格,它的文件名称都匹配于某个给定的模式。

标注:通配符与正则表达式的特殊字符作用不一样(第3章会介绍正则表达式)

  

使用set结构的通配符如下所示:

 

posted @ 2017-05-22 17:49  邹龙彬  阅读(773)  评论(0编辑  收藏  举报