Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

ksh or bash

I have a small script like the below and it doesn't seem to like my synatx I have to change the expression to `cat $file | grep -c test105` -ge 10. But i would like to know why the below syntax doesn't work. Also the && doesn't seem to work

file="/path/to/the/file"
file2="/path/to/the/file2"
if [  `grep -c test105 $file` -ge 10  ]  &&  [  `grep -c test105 $file2` -ge 10 ]; then
echo "blah"
else
blah
fi
0
lolaferrari
Asked:
lolaferrari
  • 2
  • 2
1 Solution
 
woolmilkporcCommented:
Hi,

all your variants should work just fine.

What exactly is the problem? Error message? Wrong output?

By the way, the syntax I'd recommend would be this:


if [[  $(grep -c test105 $file) -ge 10 &&  $(grep -c test105 $file2) -ge 10 ]]; then

wmp
0
 
lolaferrariAuthor Commented:
Thanks for that. Yeah, I thought my variants would work to but it seems to interpret the grep string as a value and the 10 as a value so you end up with

grep: test105: No such file or directory
grep: 10: No such file or directory
grep: test105: No such file or directory
grep: 10: No such file or directory


Would this just be the same ?

if [[  `grep -c test105 $file` -ge 10 &&  `grep -c test105 $file2` -ge 10 ]]; then
0
 
woolmilkporcCommented:
backticks  `  `  and $(   )  are functionally equivalent, but the latter is by far better readable!

Your error messages look just as if you had put the backticks around the whole expression ("grep" up to "10" instead of "grep" up to "$file", and as if you had written "grep c" instead of "grep -c".



0
 
lolaferrariAuthor Commented:
many thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now