0

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?

13
  • Guys, I am only a beginner to scripting. Can someone help explaining these errors and corrections in a simplest way? If so, that would be really helpful to me. Commented Jun 8, 2014 at 17:24
  • was your intention to decide on return value of executing vzctl command? Commented Jun 8, 2014 at 17:29
  • Hi, I intended only this which is when the vzctl command is success it should print "Hostname set". Commented Jun 8, 2014 at 17:32
  • try this: if vzctl set containerID --hostname server.domain.com --save; then echo "success"; else echo "failure"; fi # what you are doing in your script is writing the command output to variable, instead of the return value - and as you act upon it right away, there is no need to explicitely store it anywhere. Commented Jun 8, 2014 at 17:35
  • @Bushmills ok brother thanks, I know this is the simple way. I want to know if this is the only way to check ""if a command is success then proceed to next line"" in bash scripting. Are we following only this method everytime? Also do you have any clue about what happened in my first script "vpstest.sh" and the error "[: -eq: unary operator expected" with that? Commented Jun 8, 2014 at 17:43

1 Answer 1

0

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).

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your time, but I did not get a full idea of it. Please note that I am beginner to bash scripting. Can you explain these two errors in a simple way?
@johnwilson In both cases you try to compare a command output with the value "1" and according to these errors, in both cases your both commands don't return an integer value.
@johnwilson -eq must be use only with integer expressions and you must protect your variables with doubles quotes when you use test or [ (unlike [[). You should read this link : stackoverflow.com/questions/19597962/bash-illegal-number/…
@johnwilson Glad to help you, but your problem is solved right now?
Yes I made some corrections in script according to points mentioned here :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.