Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ksh or bash

Posted on 2011-09-20
4
Medium Priority
?
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

715 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