tcsh: not run script (if: Badly formed number)

Hello
I have Linux Debian & tcsh shell. My mini script not run :(.
Startup script displays a message:

user@host:dir% ./test
if: Badly formed number.

script:

#!/bin/tcsh -f
#script check size files systems
set x = `df -h /usr | tail -n1 | awk '{ print( $5 ); }'`
set y = 60%
if ($x >= $y) then
echo "BAD"
else
echo "GOOD"
endif

df -h /usr | tail -n1 | awk '{ print( $5 ); }' shows 96%

please help!!!

Csh is complaining about the % at the end of the numbers in the expression. Removing them from the df output and from your hard coded assignment will fix.

#!/usr/bin/env tcsh
#script check size files systems
set x = `df -h /usr | tail -n1 | awk '{ print( $5+0 ); }'`  # add +0 to have awk convert to integer
set y = 60

if ($x >= $y) then
     echo "BAD"
else
     echo "GOOD"
endif

2 agama: thanks:b: this solution:cool:

I know in bash to work with digit in % => if ["$ x" == "$ y"] then fi

the C-shell does not work with data in % ???:eek:

You are doing string comparison with this syntax in either bash or Korn shells (and I believe that you need [[ and ]] to use ==). With string comparison you have to be careful as you might not get what you expect. Consider the statement where the variables evaluate to something like this:

if [[ 0040% > 30% ]]

Written this way, the expression will evaluate to false, not true as you might expect.

If you use the (deprecated) form with 'gt' instead of '>' to force a numeric comparison

if [[ 0040% -gt 30% ]]

you will get an error: 40%: more tokens expected

If you wrote the if statement in ksh/bash using the current standard it would also fail if the variables evaluate to a string with a trailing %:

x=60%
y=50%
if (( $x > $y ))
then
    echo "true"
else
    echo "false"
fi

running this in ksh yields: line 6: 60% > 50% : arithmetic syntax error.

$ ./whichsame %100 %90
not same
$ ./whichsame %100 %100
same
$ cat whichsame
#!/bin/tcsh
set a="$1"
set b="$2"
if ( `echo $a | cut -d'%' -f2` == `echo $b | cut -d'%' -f2` ) then
    echo same
  else
    echo not same
endif

it works:

#!/bin/tcsh
set a="$1"
set b="$2"
if ( `echo $a | cut -d'%' -f1` >= `echo $b | cut -d'%' -f1` ) then
    echo same
  else
    echo not same
endif

thanks ygemici