源代码:
1 #!/bin/bash
2
3 if [ $# -ne 1 ];then
4 echo "wrong "
5 exit 1
6 fi
7
8 if [ id $1 ];then
9 echo "user infomation output"
10 sed '/$1/w A.txt' /etc/passwd
11
12 else
13 echo "no such user $1"
14 fi
脚本目的是 判断用户是否存在,存在就提取用户信息,然后添加到其他文件中。
运行后第8行报错 ./Info.sh: line 8: test: id: unary operator expected
百度一下,在8行if后面加一个[] if [[ id $1 ]];then.
但还是报错:
./Info.sh: line 8: conditional binary operator expected
./Info.sh: line 8: syntax error near `"$1"'
./Info.sh: line 8: `if [[ id "$1" ]];then'
本人新手。写脚本不久。电脑win10,wmware12,opensuse42.1 ,xshell5
[[ -z $(id fengxsong) ]] && echo 'yes' || echo 'no'
Similar to this, you did not save the output of
id
to the variable[]
is actually test. You can view the test documentation throughman test
. Generally, if is used in combination with test, but this is not always the case. The mechanism of if is to determine the return value of the command. If the return value is 0, it is true, if it is not 0, it is false.It is recommended to read ABS, which has such an example:
So it should be changed like this:
What is
id
? It’s not a built-in variable either! ?