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?

vjwilson
  • 754
  • 2
  • 14
  • 30
  • 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. – vjwilson Jun 08 '14 at 17:24
  • was your intention to decide on return value of executing vzctl command? – Deleted User Jun 08 '14 at 17:29
  • Hi, I intended only this which is when the vzctl command is success it should print "Hostname set". – vjwilson Jun 08 '14 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. – Deleted User Jun 08 '14 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? – vjwilson Jun 08 '14 at 17:43
  • There are several ways. return value is, upon command completion, in variable $?, and you can also use || and && for flow control. example: command && echo "no error" – Deleted User Jun 08 '14 at 17:45
  • and yes sure, but Idriss Neumann answered that question already. just look at output of that command, or the contents of your variable, and judge for yourself how much of a number that is. – Deleted User Jun 08 '14 at 17:46
  • for actually testing numbers with bash, also look at the if (( ... )) construct – Deleted User Jun 08 '14 at 17:53
  • @Bushmills yeh I read what Idriss Neumann given on error "[: -eq: unary operator expected" but I am not able to understand it properly as I am a newbie. Could you guide me to correct myself based on my script "vpstest.sh", so I can grab it really well and will be helpful for future purpose. Sorry for being a pain :( – vjwilson Jun 08 '14 at 17:56
  • try: if [ 1 -eq ]; then echo "foo"; fi , and compare it against if [ 1 -eq 1 ]; then echo "foo"; fi – Deleted User Jun 08 '14 at 17:58
  • yeh I get error "[: 1: unary operator expected" for "if [ 1 -eq ]; then echo "foo"; fi" , what does this mean actually? – vjwilson Jun 08 '14 at 18:15
  • it means that your script has the same problem as the first of these two examples. – Deleted User Jun 08 '14 at 18:27
  • @Bushmills yeh it's the same error, I will try some more links and your info to understand it properly. I think now I am wasting your time. Have a great day. – vjwilson Jun 08 '14 at 18:37

1 Answers1

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

Community
  • 1
  • 1
Idriss Neumann
  • 3,760
  • 2
  • 23
  • 32
  • 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? – vjwilson Jun 08 '14 at 17:23
  • @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. – Idriss Neumann Jun 08 '14 at 18:19
  • @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 : http://stackoverflow.com/questions/19597962/bash-illegal-number/19598570?stw=2#19598570 – Idriss Neumann Jun 08 '14 at 18:21
  • @johnwilson Glad to help you, but your problem is solved right now? – Idriss Neumann Jun 08 '14 at 19:05
  • Yes I made some corrections in script according to points mentioned here :) – vjwilson Jun 09 '14 at 04:57
  • I gave the commands in both scripts directly with if statement since those results are not an integer value. like.. ` #/bin/bash if curl -sku root:PASSWORDHERE "https://ip.x.x.x:2087/xml-api/sethostname?hostname=server.domain.com" | awk -F"[<>]" '/status/{print $3}' | head -n1; then echo "Hostname set" else echo "Failed setting hostname" fi .Now this is working. I think this is the only way to get it worked right? – vjwilson Jun 09 '14 at 05:01