having trouble with a while loop in bash

Status
Not open for further replies.

joethesinner

Ars Centurion
246
Subscriptor
So I'm writing some scripts to automate pushing keys for SSH to ease deployment of new linux boxes (these are modems running an embedded linux). So far I have a database that stores the info about the server, a script that will push the keys and report if they are successful (which works well), and an expect script that runs the first script using the IP address that gets passed to it as an argument. What I have halfway done is a script that can run as a cron job and will get the IP address of a box that hasn't had keys pushed to it and pass that as an argument to the expect script, the problem is it doesn't stop when all records in the DB have been hit and had the key_status updated, and I get the following error: line 26: [: =: unary operator expected

A sample of the code is below:

Code:
 1 #/bin/bash
  2 
  3 
  4 function get_ip {
  5 ipaddr=$(mysql pkg_deploy -B -N -e "select addr from modem_status where key_status != 'downloaded' and pkg_status != 'downloaded' limit 1;")
  6 }
  7 
  8 function update_key_status_success {
  9 mysql pkg_deploy -e "UPDATE modem_status SET key_status = 'downloaded' where addr = '$ipaddr';"
 10 }
 11 
 12 function push_keys {
 13 result=$(/root/deploy_keys.sh $ipaddr|grep successfully|awk '{printf $6}')
 14 }
 15 function update_key_status_failed {
 16 mysql pkg_deploy -e "UPDATE modem_status SET key_status = 'failed' where addr = '$ipaddr';"
 17 }
 18 function get_next_ip {
 19 ipaddr=$(mysql pkg_deploy -B -N -e "select addr from modem_status where key_status = '' and pkg_status != 'downloaded' LIMIT 1;")
 20 }
 21 
 22 while [ -n $ipaddr ]
 23 do
 24 get_ip
 25 push_keys
 26 if [ $result = "successfully" ]
 27 then update_key_status_success
 28 get_next_ip
 29 else
 30 update_key_status_failed
 31 get_next_ip
 32 fi
 33 continue
 34 done

Any help or tips you could point me to would be appreciated.

edit: fixed the "line 26: [: =: unary operator expected", was missing set of quotes around $result on line 26.
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
You do of course realize that your 'get_ip' and 'get_next_ip' functions use different queries, right? And that you check pkg_status but never set it on success? Your real problem though, is that you do not initialize ipaddr before you begin, nor do you put quotes around it. What happens is that '[' is a shell alias to test, but the ']' is nothing - a required operand to call test ('[ -n $str' does not call test) but otherwise treated as real input. Unless you do not put quotes around a var that is not used, in which case ']' is tested by '-n'. Put quotes around it now and your script quickly exits.
Code:
$ set -xv
$ if [ -n $test ]; then echo 1; fi
if [ -n $test ]; then echo 1; fi
+ '[' -n ']'                          <--- see what happens here? ']' is a non-zero string.
+ echo 1
1
$ if [ -n "$test" ]; then echo 1; fi
if [ -n "$test" ]; then echo 1; fi
+ '[' -n '' ']'                       <--- here '' is substituted for the unset var $test
I do have some other tips, though.

If you are going to do this in bash, then learn to use return codes. Instead of grepping the output of deploy_keys.sh, have *it* use exit with an argument. Then return that value ($?) and check it. The standard is that 0 is success and anything else is failure; some programs return different failure values depending on the cause (1 for something that completed but was not successful, -1 when it errors out on bad arguments, etc.).
Code:
$ grep localhost /etc/hosts
::1     localhost
127.0.0.1       localhost
$ echo $?
0
$ grep lacolhost /etc/hosts
$ echo $?
1
So, change it so that $result is $? and test that (side note, a function called push_keys that only does one line should be inline to assist with flow, OR test $? without the function and have it do the updates).

You are also not checking $? in "update_key_status_success", which could fail as well.

You are calling get_next_ip within the if-else loop, move it out of there, after the 'fi' and before 'continue'. It has to run regardless of success or failure, right?

For debugging purposes, have each external call write to a temp log file. Then check $? and if it is non-zero (if [ "$?" != "0" ]), echo the contents of that file. Then delete the file and continue on. You could also have the program log the output of a query similar to get_ip or get_next_ip without the limit 1 at the end of every iteration of the while loop, so you can see how the output changes (or does not change). Pretty sure this will not be necessary if you just call 'get_ip' before the loop, get_next_ip before the 'continue', and put quotes around the $ipaddr in the while conditional.
 
Status
Not open for further replies.