Shell中的set--用法-创新互联
set 命令为 shell 设定参数变量。许多命令的输出是以空格分隔的值,如果要使用其中的某个数据域,使用 set 非常有效。
#!/bin/sh
echo the date is $(date)
set $(date)
echo the month is $2
输出:
the date is Wed Apr 23 15:34:16 CST 2014
the month is Apr
将 date 命令的输出设置为参数表,再通过位置参数 $2 取得月份。因为 date 命令受语言和地域的影响比较大,所以如果目的是取出月份的名字,则应该用 date +%B。
还可以利用 set 命令来控制 shell 的执行方式。其中最常用的就是 set +x,使脚本程序跟踪显示当前执行的命令。
再看如下示例:
[root@python shell]# ls /opt
file file3 haha mimei
[root@python shell]# set -- $(ls /opt)
[root@python shell]# echo $1
file
[root@python shell]# echo $2
file3
[root@python shell]# echo $3
haha
[root@python shell]# echo $4
mimei
[root@python shell]# echo $#
4
[root@python shell]# echo $@
file file3 haha mimei
[root@python shell]#
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享标题:Shell中的set--用法-创新互联
当前网址:http://myzitong.com/article/ceppge.html