linux bash 取得命令执行的结果,Linux的Bash特性之:命令的执行结果以及状态结果...

linux中的命令执行的状态结果:

bash通过状态返回值来输出此结果:

成功:0

失败:1-255

命令执行完成之后,其状态返回值保存于bash的特殊变量$?中;

命令正常执行时,有命令的返回值:根据命令及其功能不同,结果各不相同;

引用命令的执行结果(命令结果):

$(COMMAND)或COMMAND (反引号)

例如: ll /tmp/ 命令运行后返回的结果为命令的执行结果,命令执行成功后的状态结果返回在特殊变量$?中。

[root@localhost ~]# ll /tmp/

total 8

drwxr-xr-x. 4 root root 24 May 19 17:39 a1

drwxr-xr-x. 2 root root 6 May 19 17:39 a2

drwxr-xr-x. 7 root root 62 May 19 17:51 mysysroot

drwxr-xr-x. 2 root root 98 May 19 11:57 mytest1

drwxr-xr-x. 2 root root 6 May 19 17:40 q_y

drwxr-xr-x. 2 root root 6 May 19 17:40 q_z

-rw-r--r--. 1 root root 465 May 19 16:04 testcp.txt

-rw-r--r--. 1 755 root 26 May 4 2015 test.file

drwxr-xr-x. 2 root root 6 May 19 11:47 tfile-2019-05-19-11-47-16

-rw-rw-r--. 1 user1 user1 0 May 19 16:07 user2.txt

drwx------. 2 root root 6 May 19 11:35 vmware-root

drwxr-xr-x. 2 root root 6 May 19 17:40 x_y

drwxr-xr-x. 2 root root 6 May 19 17:40 x_z

[root@localhost ~]# echo $?

0

[root@localhost ~]#

命令执行的正确和错误的状态结果查看$?

[root@localhost ~]# cat /tmp/test.file

helle word

this is a test

[root@localhost ~]# echo $?

0

[root@localhost ~]# cat /tmp/test.file1

cat: /tmp/test.file1: No such file or directory

[root@localhost ~]# echo $?

1

[root@localhost ~]#

引用命令的执行结果来作为另一个命令的相关输入信息

例如:在/tmp目录下创建以tfile开头,后跟当前日期和时间的文件,文件名形如:tfile-2016-05-27-09-32-22

[root@localhost ~]# touch /tmp/tfile-`date +%Y-%m-%d-%H-%M-%S`

[root@localhost ~]# ll /tmp/

total 8

drwxr-xr-x. 4 root root 24 May 19 17:39 a1

drwxr-xr-x. 2 root root 6 May 19 17:39 a2

drwxr-xr-x. 7 root root 62 May 19 17:51 mysysroot

drwxr-xr-x. 2 root root 98 May 19 11:57 mytest1

drwxr-xr-x. 2 root root 6 May 19 17:40 q_y

drwxr-xr-x. 2 root root 6 May 19 17:40 q_z

-rw-r--r--. 1 root root 465 May 19 16:04 testcp.txt

-rw-r--r--. 1 755 root 26 May 4 2015 test.file

-rw-r--r--. 1 root root 0 May 19 18:14 tfile-2019-05-19-18-14-29

-rw-rw-r--. 1 user1 user1 0 May 19 16:07 user2.txt

drwx------. 2 root root 6 May 19 11:35 vmware-root

drwxr-xr-x. 2 root root 6 May 19 17:40 x_y

drwxr-xr-x. 2 root root 6 May 19 17:40 x_z

[root@localhost ~]# date

Sun May 19 18:14:34 CST 2019

[root@localhost ~]#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值