PDA

View Full Version : Problem with if and integer



ryanrio95
March 3rd, 2012, 05:40 PM
Code:

if [ du -s resources | sed 's/\tresources//g' < "720000" ]; then
echo "Directory is smaller then 720 MB "
fi

Output:

bash: [: missing `]'
bash: 720000: No such file or directory

This isn't working, who can help me?

Regards

MadCow108
March 3rd, 2012, 05:43 PM
you must put the commands into backticks so they are executed and the output inserted in this spot:
if
[ `du -s resources | sed 's/\tresources//g'` -lt 720000 ]; then
echo "Directory is smaller then 720 MB "
fi
for integer comparison you should use -eq -lt -gt -le -ge etc, see man test

Vaphell
March 3rd, 2012, 05:55 PM
if you use bash you can use (( )) to switch to integer arithmetic mode


if (( $(du -s resources | cut -f1) < 720000 )); then ...; fi

backticks are deprecated and $( ) should be used instead.

Khayyam
March 3rd, 2012, 10:50 PM
el al ...


du -s resources | awk '{if($1<720000) {print "Directory is smaller than 720MB"}}'


Vaphell's suggestion of using bash to evaluate the expression is probably more what your looking for ...

HTH ... khay

MadCow108
March 3rd, 2012, 11:00 PM
backticks are deprecated and $( ) should be used instead.

citation needed.

to my knowledge backticks are more portable.

matt_symes
March 3rd, 2012, 11:12 PM
Hi


citation needed.

to my knowledge backticks are more portable.

I think command substution using $( ) is part of the POSIX standard, whereas using backticks was the previous standard.

manuals.ts.fujitsu.com/file/8867/posix_k.pdf

Backticks are more portable but trying to read them... :)

Kind regards

Khayyam
March 4th, 2012, 01:16 AM
citation needed. to my knowledge backticks are more portable.

I think the only instance in which backticks will prove more 'portable' is if the target is an old bourne shell, most machines 'sh' will either be bash or dash. I don't think there will be any calculable chance of running into trouble with $().

Anyhow, the Bash FAQ has this to say about backticks (http://mywiki.wooledge.org/BashFAQ/082).

best ... khay

MadCow108
March 4th, 2012, 01:36 AM
I don't think there will be any calculable chance of running into trouble with $().

famous last words.

but I agree $() are preferable to the ugly backticks if you don't care about legacy systems.

Khayyam
March 4th, 2012, 02:59 AM
famous last words.

Ya well, better than, "kiss me Hardy" :)


but I agree $() are preferable to the ugly backticks if you don't care about legacy systems.

Those things exist (ummm ... like =< Solaris 10), but we should remember that bourne shell had the "^" (caret) for "|" adopted from the Thompson shell, and that too was depreciated. So "legacy" has some timelimit, besides the target here is not legacy but a modern posix complient OS.

best ... khay

ryanrio95
March 4th, 2012, 05:50 PM
i go for the way madcow108 said.

thanks for helping me.

regarrds

Khayyam
March 4th, 2012, 08:45 PM
i go for the way madcow108 said. thanks for helping me.

I dunno .. even if you must use backticks rather than brace expansion vaphell's solution does seem to me the most proper ..


if (( $(du -s resources | cut -f1) < 720000 )); then
echo "Directory is smaller than 720MB"
fi

Anyhow, each to their own I guess.

best ... khay

matt_symes
March 4th, 2012, 09:43 PM
Hi

Backticks ? Honesty, i don't use them. What shell and version are you targeting ?

Kind regards