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]#



本文转自 wangergui 51CTO博客,原文链接:http://blog.51cto.com/wangergui/2070443,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值