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