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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting tcsh: not run script (if: Badly formed number)
# 1  
Old 09-06-2010
tcsh: not run script (if: Badly formed number)

Hello
I have Linux Debian & tcsh shell. My mini script not run Smilie.
Startup script displays a message:
Code:
user@host:dir% ./test
if: Badly formed number.

script:
Code:
#!/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!!!
moskovets
# 2  
Old 09-06-2010
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.

Code:
#!/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

This User Gave Thanks to agama For This Post:
# 3  
Old 09-06-2010
2 agama: thanksSmilie this solutionSmilie

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

the C-shell does not work with data in % ???Smilie
moskovets
# 4  
Old 09-06-2010
Quote:
Originally Posted by moskovets
I know in bash to work with digit in % => if ["$ x" == "$ y"] then fi

the C-shell does not work with data in % ???Smilie
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:

Code:
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
Code:
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 %:

Code:
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.
# 5  
Old 09-06-2010
MySQL

Code:
$ ./whichsame %100 %90
not same

Code:
$ ./whichsame %100 %100
same

Code:
$ 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

# 6  
Old 09-07-2010
it works:
Code:
#!/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
moskovets
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run a shell script in a loop with max number of threads

hi guys. i have a question for you i have a one file and inside this file there are 1000 lines and each line is a linux command running this commands takes long time so i want to create one bash script and run this lines in a loop with max number of threads for example i want to run... (2 Replies)
Discussion started by: avtaritet
2 Replies

2. Shell Programming and Scripting

Csh/tcsh : Check the file existance and run the script

Hi, I've to wait until a file generated and once its generated, source another script in Linux terminal. Please help me as this is very very urgent. The code should be something like if ( -e "/abc/xyz/a.txt ) source aaa.csh else sleep This should be repeated till the if... (4 Replies)
Discussion started by: kumar_eee
4 Replies

3. Shell Programming and Scripting

Run script every minute and terminate after specified number of executions

when it runs and look at my acron.log file it generates an error as below /tmp/prog.sh: line 4: (12 Replies)
Discussion started by: azherkn3
12 Replies

4. Shell Programming and Scripting

How to use counter to run the script to limit number?

I want to run my shell script to the limit number.Suppose I know in advance that MAX=5 then I want that my script run 5 times only.Something like below$ vi testingMAX=5COMMAND="ssh -l stpuser VHLDVWSAD001 /projects/st/utils/deploy/deployall.sh >/dev/null 2>&1 &" ; sleep 20;count=0while... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

5. Shell Programming and Scripting

TCSH Check if number is even

Hey, I am trying to check if an integer is even in a tcsh script This is what I am running now set lattest = ` echo $latmin "%2" | bc -l ` echo $lattest if ( $lattest == 0 ) then echo "min is already even" else if ( $lattest =! 0 ) then set latmin = ` echo $latmin "+1" |... (2 Replies)
Discussion started by: travish12
2 Replies

6. Shell Programming and Scripting

history: Badly formed number

% history clean history: Badly formed number. :( % echo $0 /usr/local/bin/tcsh % uname SunOS % grep -e "hist" .tcshrc.user set history=90 set savehist=80 Please suggest what could be the problem here. Thanks in advance. (1 Reply)
Discussion started by: mayankmehta_83
1 Replies

7. Shell Programming and Scripting

Badly formed number.

I have the following script running every day numerous times a day and it works fine, but very occasionally I get the following error if: Badly formed number. Anyone know why? Here is the script that runs with the follow parms LCTMDBSE 100000 130000 160000 #!/bin/csh ... (0 Replies)
Discussion started by: Northerner
0 Replies

8. Shell Programming and Scripting

for:badly formed number

Hi, I am doing the following but it complains and says "for:badly formed number" does anyone know why? #!/bin/tcsh foreach(....) ............ set depth=64 set width=23 if ($depth==64) then echo "if" set addr_bits=5 else echo "else" endif echo "addr_bits:$addr_bits" echo... (3 Replies)
Discussion started by: ROOZ
3 Replies

9. Shell Programming and Scripting

How to make a script run for a maximum of "x" number of hours only

How to make a script run for a maximum of "x" number of hours only (7 Replies)
Discussion started by: ScriptDummy
7 Replies

10. Shell Programming and Scripting

New to it all, But i wanna script really badly!!!

Ok, im brand new to this whole thing, well nearly, but all i wanna know and do is scripting, WHAT DO I DO?!:confused: (3 Replies)
Discussion started by: TheNewGuy
3 Replies
Login or Register to Ask a Question