错误两个脚本:"一元运算符预期"和"整前pression预期" [英] Error in two scripts: "unary operator expected" and "integer expression expected"

查看:178
本文介绍了错误两个脚本:"一元运算符预期"和"整前pression预期"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了两个脚本,试图做同样的动作两种不同的方式,但我得到的错误我每次运行这些时间。请请求您的帮助纠正我的脚本和提高我的知识和。所有我试图做VPS建立在一个脚本。下面的两个脚本将在其中得到的错误,它部分各一次。

1)脚本来设置通过的cPanel XML的API主机名在OpenVZ的节点的VPS

猫vpstest.sh

 #/斌/庆典
hostname_status =`卷曲-sku根:PASSWORDHEREHTTPS://ip.x.x.x:2087 / XML的API / sethostname主机= server.domain.com| awk的-F[<>]'/状态/ {打印$ 3}'|头-n1`
    如果[$ hostname_status -eq 1];然后
      回声主机名集
    其他
      回声失败设置主机名
    科幻

输出:

 #./vpstest.sh
./vpstest.sh:3号线:[:当量:一元运算符预期
失败的主机名设置


2)脚本设置通过命令行的主机名在OpenVZ的节点

猫vpstest1.sh

 #!/斌/庆典
hostname_status =`vzctl设置containerID --hostname server.domain.com --save`
    如果[$ hostname_status-eq 1];然后
      回声主机名集
    其他
      回声失败设置主机名
    科幻

输出:

 #./vpstest1.sh
./vpstest1.sh:3号线:[:保存到/etc/vz/conf/containerID.conf CT配置:整数EX pression预期
失败的主机名设置


有人能帮助清除这些错误?


解决方案

首先,的输出vzctl设置containerID --hostname server.domain.com --save 似乎而 -eq 只提供给做整数值之间的比较不是一个整数值。

这可以解释以下错误:


 整数EX pression预期


然后,您应该了解的必要性(或没有)读取此提醒保护您的变量用双引号的。

这可以解释下面的错误,你可以使用类似:


  ./ vpstest.sh:3号线:[:当量:一元运算符预期


如果您要检查命令的状态:

 命令>的/ dev / null的2  - ;&放大器; 1安培;&安培;回声成功||回声不及格
# 要么
如果命令>的/ dev / null的2 - ;&放大器; 1;然后
    回声成功
其他
    回声不及格
科幻

您也可以查看对应于previous指令( 0 $? >当该命令是成功还是另一整数值是 1 在大多数情况下,或更多)。

I wrote two scripts which try to do the same action in two different ways, but I get errors each time I run those. Kindly requesting your help to correct my scripts and to improve my knowledge as well. All I am trying to do the vps setup in a single script. Following two scripts are just a portion of it which get errors each time.

1) Script to set hostname through cpanel xml-api for a vps in openvz node

cat vpstest.sh

#/bin/bash
hostname_status=`curl -sku root:PASSWORDHERE "https://ip.x.x.x:2087/xml-api/sethostname?hostname=server.domain.com" | awk -F"[<>]" '/status/{print $3}' | head -n1`
    if [ $hostname_status -eq 1 ]; then
      echo "Hostname set"
    else
      echo "Failed setting hostname"
    fi

Output:

# ./vpstest.sh
./vpstest.sh: line 3: [: -eq: unary operator expected
Failed setting hostname


2) Script to set hostname via command line in an openvz node

cat vpstest1.sh

#!/bin/bash
hostname_status=`vzctl set containerID --hostname server.domain.com --save`
    if [ "$hostname_status" -eq 1 ] ; then
      echo "Hostname set"
    else
      echo "Failed setting hostname"
    fi

Output:

# ./vpstest1.sh
./vpstest1.sh: line 3: [: CT configuration saved to /etc/vz/conf/containerID.conf: integer expression expected
Failed setting hostname


Can someone help to clear these errors?

解决方案

First, the output of vzctl set containerID --hostname server.domain.com --save seems not be an integer value whereas -eq is only provided to do comparisons between integers values.

This would explain the following error :

integer expression expected

Then, you should read this reminder about the necessity (or not) to protect your variables with double quotes.

This would explain the following error, you could use something like :

./vpstest.sh: line 3: [: -eq: unary operator expected

If you want to check the status of a command :

command >/dev/null 2>&1 && echo "success" || echo "fail"
# or
if command >/dev/null 2>&1; then
    echo "success"
else
    echo "fail"
fi

You could also check the variable $? which correspond to the status of the previous command (0 when that command success or another integer value which is 1 in most of cases, or more).

这篇关于错误两个脚本:&QUOT;一元运算符预期&QUOT;和&QUOT;整前pression预期&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆