Solved

ksh or bash

Posted on 2011-09-20
4
290 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reinstall 1 81
Linux : adding the new user with password option, 11 107
What is the meaning of mount pont? with respect to windows file server. 7 61
Problem logging tar errors 11 58
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

831 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