2012-11-22 69 views
3

我想测试两个变量是否存在,之后我使用“read”创建它。如果用户只输入我想要的两个变量中的一个,则会显示错误。bash中的错误 - 一元运算符预期存在-e

有我的代码:

while true; 
do 
    echo "Saisissez deux variables x et y sous la forme [x y]" 
    read x y 

    if [ !-e $x ] || [ !-e $y ] <<<<<< problem ligne 
    then 
    echo "Vous devez renseigner deux nombres x et y" 
    elif [ $x = "." ] 
    then 
    exit 0 
    else 
    calcul $x $y 
    fi 
done 

并没有当我刚进入一个参数错误:

[: !-e: unary operator expected 

感谢您的帮助:)

+1

如果你把什么空间了''之间和'-e '? –

+1

'-e'运算符检查是否存在具有该名称的文件。 '!后面需要一个空格!' - shell要求每个命令和参数都是一个单独的标记(除括号,重定向和其他一些语法细节外)。你也应该大致双引号的用户输入,所以' “$ X”''亲$ x'和' “$ Y”''亲$ y'。 – tripleee

+0

感谢您的回复:)我试图检测用户是否按下了没有任何价值的回车键。我试图用-e来防止这一点,但它不工作:/ – toshiro92

回答

7

将其更改为:

if [ -z "$x" ] || [ -z "$y" ] 

说明

  • [实际上是一个shell内置(尝试which [help [您提示);它是test的同义词。
  • -z[的一个参数。它的意思是“测试如果下一个字符串的长度为0;返回true,如果是这样;返回false,否则
  • 始终包你用双引号测试变量

这里是有用的选名单! [,因为我认为你会感兴趣:

-b file = True if the file exists and is block special file. 
-c file = True if the file exists and is character special file. 
-d file = True if the file exists and is a directory. 
-e file = True if the file exists. 
-f file = True if the file exists and is a regular file 
-g file = True if the file exists and the set-group-id bit is set. 
-k file = True if the files "sticky" bit is set. 
-L file = True if the file exists and is a symbolic link. 
-p file = True if the file exists and is a named pipe. 
-r file = True if the file exists and is readable. 
-s file = True if the file exists and its size is greater than zero. 
-s file = True if the file exists and is a socket. 
-t fd = True if the file descriptor is opened on a terminal. 
-u file = True if the file exists and its set-user-id bit is set. 
-w file = True if the file exists and is writable. 
-x file = True if the file exists and is executable. 
-O file = True if the file exists and is owned by the effective user id. 
-G file = True if the file exists and is owned by the effective group id. 
file1 –nt file2 = True if file1 is newer, by modification date, than file2. 
file1 ot file2 = True if file1 is older than file2. 
file1 ef file2 = True if file1 and file2 have the same device and inode numbers. 
-z string = True if the length of the string is 0. 
-n string = True if the length of the string is non-zero. 
string1 = string2 = True if the strings are equal. 
string1 != string2 = True if the strings are not equal. 
!expr = True if the expr evaluates to false. 
expr1 –a expr2 = True if both expr1 and expr2 are true. 
expr1 –o expr2 = True is either expr1 or expr2 is true. 
+0

是的:)在bash:如果您要检查它们实际上的数字,也许像“总是给当你正在测试变量” –

+0

''$'in''| * [!0-9] *)用法语死亡;; esac'和同上为'$ y'。 – tripleee

+0

感谢补:)(死在法国 - >“mourir”) 和它的作品,但如果我只是按ENTER键,没有任何价值,这是行不通的。我试图用-e来防止这一点,但它不工作:/ – toshiro92

1

运营商-e是不是在这种情况下,使用权操作员操作-z是右操作它检查是否一个字符串是空的;在。你的情况xy

所以更改此设置:

if [ !-e $x ] || [ !-e $y ] 

这样:

if [ ! -z $x ] || [ ! -z $y ] 

操作-e是用来检查文件是否存在。

+0

呀它的工作原理,但如果我只是按ENTER键,没有任何价值,这是行不通的。我试图用-e来防止这种情况,但它不起作用:/ – toshiro92

0

在bash中,你可以使用:

if [[ -z $x || -z $y ]]; then 

[[具有多种功能,这使得它更容易使用比test/[使用,其中包括把||&&直接进入表达的能力,和事实上你不需要引用参数扩展,因为[[会自动执行。

help [[ 

会给你一些更多的信息,但它忽略了这个有用的段落,你可以在man bash发现:

Word splitting and pathname expansion are not performed on the 
    words between the [[ and ]]; tilde expansion, parameter and 
    variable expansion, arithmetic expansion, command substitution, 
    process substitution, and quote removal are performed. Conditional 
    operators such as -f must be unquoted to be recognized as primaries. 
相关问题