Solved

ksh or bash

Posted on 2011-09-20
4
287 Views
Last Modified: 2012-05-12
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
Comment
Question by:lolaferrari
  • 2
  • 2
4 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 36569048
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
 

Author Comment

by:lolaferrari
ID: 36569242
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
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 36569410
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
 

Author Closing Comment

by:lolaferrari
ID: 36569498
many thanks!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now