Shell 中的set --用法

发布时间:2020-06-18 11:51:07 作者:Abigale2018
来源:网络 阅读:7666

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

推荐阅读:
  1. 打印九九乘法表
  2. 打开python shell的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

shell set 用法

上一篇:java中方法的定义

下一篇:golang怎么判断文件是否存在

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》