-1
COUNTER=0    
    let COUNTER=COUNTER+1
    count=`ssh -i /var/www/.ssh/id_rsa_root -o stricthostkeychecking=no $host $cmd`
    count1=`echo $count | awk '{print $4}'`
    printf "count1 : $count1\n"
    result1=${count1/.*}
    if [ "$result1" -ge "0" ]; then
            echo $host 
    else
            echo $host
            exit
    fi

If the value of $result1 is INTEGER and greater than zero, it'll goto IF loop (works fine for me)

But when it is not INTEGER, it is coming to else loop (which it is suppose to do) with the following error in the Output

line 55: [: : integer expression expected

but i dont want the above error in my output. I tried to use 2>/dev/null with this but no luck.

please help!

Jahid
  • 21,542
  • 10
  • 90
  • 108
Pankaj
  • 65
  • 2
  • 9
  • 1
    check your code with http://shellcheck.net – Jahid Jun 15 '15 at 21:17
  • Also, if you use `sh -x yourscript` to run this, you'll see the _actual_ value in use, so you'll be able to see how/why it's not an integer. – Charles Duffy Jun 15 '15 at 21:20
  • 1
    Also, you're using `printf` wrong. Don't substitute into the format string; instead, use the format string to tell the system how to do substitutions: `printf 'count1: %s\n' "$count1"` – Charles Duffy Jun 15 '15 at 21:21
  • 1
    You may want to check the formatting/content of your question ([ask]). – boardrider Jun 15 '15 at 21:22
  • There are other questions directly on the topic of determining whether a value is an integer in bash or not. – Charles Duffy Jun 15 '15 at 21:24
  • Right now, the code provided here doesn't work to reproduce the problem when run by anyone else (we don't have `$host` and `$cmd` set, for instance). If you provided a `count` value that would reproduce the problem 100% of the time, that would be helpful. – Charles Duffy Jun 15 '15 at 21:27
  • By the way -- when you "tried 2>/dev/null", exactly where and how did you try it? – Charles Duffy Jun 15 '15 at 21:30
  • Shouldn't the line `result1=${count1/.*}` be `result1={$count1/.*}` – Rakholiya Jenish Jun 15 '15 at 21:33
  • @CharlesDuffy, i am afraid i can't give the values for $cmd and $host – Pankaj Jun 15 '15 at 21:36
  • I'm not asking you to give the values for `cmd` or `host`. I'm asking you to give the value given as a **result** of running `$cmd` on `$host`. – Charles Duffy Jun 15 '15 at 21:36
  • @RakholiyaJenish, no, it should not; that's clearly an attempt at parameter expansion, and PE operations have the leading `$` _outside_ the curly brackets. Mind you, I'd rather like to know just what the OP means it to do. – Charles Duffy Jun 15 '15 at 21:37
  • @CharlesDuffy : the output is just an integer – Pankaj Jun 15 '15 at 21:38
  • @Pankaj, if it were an integer, you wouldn't need to use `awk` on it, and you wouldn't need error handling for the case where the field you were pulling out with `awk` was actually empty (an empty value is, by definition, not an integer). No, it is *not* in fact an integer at all times, and you're refusing to give us enough information to reproduce the problem -- so how do you expect us to be able to help you? – Charles Duffy Jun 15 '15 at 21:38
  • Given the accepted answer, it sounds like this is actually a dupe of https://stackoverflow.com/questions/806906/how-do-i-test-if-a-variable-is-a-number-in-bash/806923#806923 – Charles Duffy Jun 15 '15 at 21:41
  • @CharlesDuffy : it is not dupe of anything !!! If it was i would have not asked. Secondly i can't really give you the command i tried to be as generic and simple as i can, – Pankaj Jun 15 '15 at 21:44
  • @CharlesDuffy also we should keep focus on solving the problem, it ll help me and anyone looking for it. Thanks – Pankaj Jun 15 '15 at 21:46
  • If you want this to be generic, your question should contain two lines of code and nothing more -- one setting `result1` directly to a value with which it fails, and then the line where the failure actually occurs. Right now, there's a bunch of other content which is nothing but distraction. – Charles Duffy Jun 15 '15 at 21:46
  • See also the MCVE page in the Help Center: http://stackoverflow.com/help/mcve – Charles Duffy Jun 15 '15 at 21:46

3 Answers3

1

If you want to handle an empty result gracefully, check for it explicitly:

if [ -z "$result1" ]; then
        : "ignoring empty string"
elif [ "$result1" -ge 0 ]; then
        printf '%s\n' "$host" 
else
        printf '%s\n' "$host"
        exit
fi
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
1

You could also check if result1 is a valid integer before making arithmetic comparisons:

function isNumber () {
    [[ $1 =~ ^-?[0-9]+$ ]]
}

if ! isNumber "$result1"; then
    echo "not a number"
elif [ "$result1" -ge "0" ]; then
    echo "null or positive"
else
    echo "negative"
fi
Eugeniu Rosca
  • 5,177
  • 16
  • 45
  • 1
    You might consider using POSIX-compliant syntax instead. `isNumber() { case $1 in *[!0-9]*) return 1;; '') return 1;; *) return 0;; esac; }` is a rough draft, though needs a bit more smoothing to handle negative values. – Charles Duffy Jun 15 '15 at 21:47
  • @Charles Duffy: I borrowed this simple function from stackoverflow long time ago. But, since there is a more portable solution, I gladly will use it in my projects from now on. thanks! – Eugeniu Rosca Jun 15 '15 at 21:51
0

Change if [ "$result1" -ge "0" ]; then to

if (( result1 >= 0 )); then

This syntax won't throw any errors if result1 isn't defined (or empty) or happen to be a string somehow.

Jahid
  • 21,542
  • 10
  • 90
  • 108
  • That's better syntax (when targeting bash), to be sure, but does it really solve the problem here? – Charles Duffy Jun 15 '15 at 21:22
  • 2
    I believe `$result1` is simply not an integer. – Eugeniu Rosca Jun 15 '15 at 21:24
  • $result1 is Integer only – Pankaj Jun 15 '15 at 21:24
  • @Pankaj, ...it is? But in your question you explicitly say that the problem happens "when it is not INTEGAR". (It would be helpful if you provided an example of the **exact** value at hand). – Charles Duffy Jun 15 '15 at 21:25
  • it is output of a command, but when you don't get any output it throws [: : integer expression expected . I dont want this error on my terminal display. Is there any way to shadow it ?? – Pankaj Jun 15 '15 at 21:26
  • Well, of course it does. If you want to handle the empty-string case, handle it explicitly. – Charles Duffy Jun 15 '15 at 21:27
  • thanks Charles. It would be nice if you can give me the syntax for empty-string case or what should I exactly change in the code – Pankaj Jun 15 '15 at 21:34
  • @Pankaj did you exactly used `if (( result1 >= 0 )); then` or modified it too: `if (( $result1 >= 0 )); then`, It should work if there aren't any other errors in your program. And this syntax doesn't need `result1` to be defined.. – Jahid Jun 15 '15 at 21:45