- 论坛徽章:
- 18
|
本帖最后由 bikkuri 于 2016-11-23 05:15 编辑
大家好!
我有一个问题向大家请教。
请看以下这些命令的执行结果:
[root@hp8:~]# cat t
if [ $(ls -l|grep -c t) -eq 0 ];then echo NO;else echo YES;fi
[root@hp8:~]# if [ $(ls -l|grep -c t) -eq 0 ];then echo NO;else echo YES;fi
YES
[root@hp8:~]# eval $(cat t)
YES
[root@hp8:~]# `cat t`
-bash: if: command not found
[root@hp8:~]#
请问为什么用eval可以正常执行这条命令,用backtick反引号`却无法正确执行?
假如换一条简单的不含有if的命令,比如date,用`是可以正确执行的,比如:
[box@iZ94mso98zxZ:~]$ cat d
date
[box@iZ94mso98zxZ:~]$ `cat d`
Wed Nov 23 04:55:40 CST 2016
[box@iZ94mso98zxZ:~]$
那么为什么用`无法执行if语句呢?
[box@iZ94mso98zxZ:~]$ `$(cat t)`
-bash: if: command not found
[box@iZ94mso98zxZ:~]$ `cat t`
-bash: if: command not found
[box@iZ94mso98zxZ:~]$ cat t
if [ $(ls -l|grep -c t) -eq 0 ];then echo NO;else echo YES;fi
[box@iZ94mso98zxZ:~]$ $(cat t)
-bash: if: command not found
[box@iZ94mso98zxZ:~]$ eval $(cat t)
YES
[box@iZ94mso98zxZ:~]$ eval `cat t`
YES
[box@iZ94mso98zxZ:~]$ ``cat t``
if [ $(ls -l|grep -c t) -eq 0 ];then echo NO;else echo YES;fi
[box@iZ94mso98zxZ:~]$ ```cat t```
-bash: if: command not found
[box@iZ94mso98zxZ:~]$
如果不用eval只用`应该如何正确执行这个命令呢?
[box@iZ94mso98zxZ:~]$ touch $(if [ $(ls -l|grep -c t) -eq 0 ];then echo NO;else echo YES;fi).txt
[box@iZ94mso98zxZ:~]$ ls -l *.txt
-rw-rw-r-- 1 box box 0 Nov 23 05:08 YES.txt
[box@iZ94mso98zxZ:~]$ rm -f YES.txt
[box@iZ94mso98zxZ:~]$ touch `if [ $(ls -l|grep -c t) -eq 0 ];then echo NO;else echo YES;fi`.txt
[box@iZ94mso98zxZ:~]$ ls -l *.txt
-rw-rw-r-- 1 box box 0 Nov 23 05:09 YES.txt
[box@iZ94mso98zxZ:~]$ rm -f YES.txt
[box@iZ94mso98zxZ:~]$ touch $(eval $(cat t)).txt
[box@iZ94mso98zxZ:~]$ ls -l *.txt
-rw-rw-r-- 1 box box 0 Nov 23 05:10 YES.txt
[box@iZ94mso98zxZ:~]$ rm -f YES.txt
[box@iZ94mso98zxZ:~]$ touch `$(cat t)`.txt
-bash: if: command not found
[box@iZ94mso98zxZ:~]$
谢谢大家!
|
|