shell 报错:unary operator expected"

一直学习perl,shell都忘得差不多了,今天写了个小脚本,判断脚本第二个变量不等于某字符串时的操作。

if [ $? -eq 0 ] && [ $2 != "wp" ]

then

: #略

fi

如果不加第二个变量,运行时会出现[: !=: unary operator expected"的报错,最后调试时发现

[ != "wp"]

原来shell的变量不定义时会出现这种情况,那就直接双括号吧。

[[ != "wp"]]

时间: 2024-08-02 12:37:34

shell 报错:unary operator expected"的相关文章

shell脚本中 unary operator expected解决办法

if [ $USER = "oracle" ]; then     if [ $SHELL = "/bin/ksh" ]; then         ulimit -p 16384         ulimit -n 65536     else         ulimit -u 16384 -n 65536     fi fi 解决方案如下: if [ "$USER" = "oracle" ]; then    if [

unary operator expected

问题:写的脚本执行的时候出现报错,如下 分析: (1)出现的脚本所在行为if [ $result -eq 0 ];then (2)报错unary operator expected的中文意思是应该使用一元运算符,也就是说这个$result假如 为空的话就成了 if [ -eq 0] ,系统就会认为是少了一个参数,只有一个 -eq 0,没有左边的参数, 所以就会出现上述报错 解决:可以将脚本改为if [[ $result -eq 0 ]];then,多加一对[]号.至于为什么,我也是百度的

shell脚本报错:"[: =: unary operator expected"

shell脚本报错:"[: =: unary operator expected" md5_109a="81ab961153b62d207f0f517048881b5d" md5_109b=`md5sum install.bin|awk '{print $1}'` if [ $md5_109a != $md5_109b ] 原因,当文件install.bin不存在时, $md5_109b为空这样对比字符串就变成了 if [ 81ab961153b62d207f0f5

[: -ge: unary operator expected 错误

1 #!/bin/bash 2 3 rate=$(df -h |grep /dev/sda3 | awk '{print $5}' | cut -d "%" -f1) 4 5 if [ $rate -ge 10 ] 6 then 7 echo "Warning! /dev/sda3 is full!!" 8 fi 运行报错 : line 5: [: -ge: unary operator expected 错误原因: 由于变量rate初始化赋值为空,那么就成了 [

shell 报错'@'localhost' (using password: YES)'root

shell中写了简单的一段 energy.sh内容: #!/bin/bash host=127.0.0.1 user=****** passwd=****** dbname=****** tablename=device_value_300sct007p01_year MONTH=$(date +%Y-%m) sqlstr="insert into device_value_300sct007p01_year (id,year,month,device_id,device_value_id,va

[: ==: unary operator expected 解决方法

之前在写脚本时遇到了这样的错误 “[: ==: unary operator expected” 这是由于做判断的变量值为空导致的. 谷歌出解决方案: 在变量之后加任意字符.例如,要判断变量un是否为auto又要防止un为空出错则这样写 if [ ${un}x == autox] 当un为auto时,表达式为autox == autox,成立: un为空,表达式为x == autox ,则不成立.

解决:pipenv shell报错:AttributeError: 'module' object has no attribute 'run'

利用pipenv shell切换到虚拟环境时,显示报错:AttributeError: 'module' object has no attribute 'run' 可以看到是d:\program\python34\lib\site-packages\pipenv\shells.py文件的第62行报错了,提示模块没有run的属性,于是就跑到该文件的第62行去看 选中run,CTRL+B发现能看到源码,源码如下: if sys.version_info >= (3, 6): # Nearly sa

exchange shell 报错winrM 无法处理该请求,因为输入 XML 包含语法错误。

网卡1和网卡2 互换了一下IP 后连接exchange shell 报错如下宿主机是HYPER-V平台互换IP没有问题,KVM平台就报错将IP换过去也没能解决这个问题卸载exchange重新安装未能解决最后通过如下方式解决卸载exchange服务器.删除服务器上和exchange相关的注册表删除键"ExchangeServer"在HKEY_LOCAL_MACHINE\Software\Micorosoft删除键"MSExchange"在HKEY_LOCAL_MACH

[Jenkins]运行shell报错:寻找匹配的 `"' 是遇到了未预期的文件结束符

这里有一个坑(至少对于我来说): 报错信息中的出错行,并不代表真实脚本中的出错行. jenkins执行的shell内容如下: 第1行echo 1个字符串,少1个双隐号 echo "233342 echo "12312" echo "34" 执行jenkins报错如下: /usr/local/tomcat/apache-tomcat-8.5.40/temp/jenkins5082891078041588552.sh:行4: 寻找匹配的 `"' 是遇