LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 11-08-2009, 11:39 PM   #1
okos
Member
 
Registered: May 2007
Location: California
Distribution: Slackware/Ubuntu
Posts: 609

Rep: Reputation: 37
cat and grep in a bash script do not show results desired.


Hello
I am writing a script and I ran into a snag.
I want to see if contents in a file exist. Ive been fiddling with this for hours but still no success.

In this example I made, I have a file "doc.txt"
In the file I have:
Quote:
John
Frank
Fred
I wrote this script but there seems to be a problem with the syntax.
Code:
#!/bin/bash
N=1
while test "$N" -le "10" ; do
echo "enter name"; read x
if [ "$(cat doc.txt | grep '$x')" = "" ] ; then
echo "$x does not exist in doc.txt"
else
echo "$x exists in doc.txt"
fi
done
exit
It does not matter what name I enter in I still get the same results.
Quote:
enter name
Joe
Joe does not exist in doc.txt
enter name
John
John does not exist in doc.txt
enter name
There is a problem with
Code:
if [ "$(cat doc.txt | grep '$x')" = "" ] ; then
Any suggestions on how to correct this?
 
Old 11-08-2009, 11:48 PM   #2
wfh
Member
 
Registered: Sep 2009
Location: Northern California
Distribution: Ubuntu Debian CentOS RHEL Suse
Posts: 164

Rep: Reputation: 44
There are many ways to do this other than the way you're doing it, however, try changing your script like so:

Code:
  if [ `grep $x doc.txt` ] ; then
    echo "$x exists in doc.txt"
  else
    echo "$x does not exist in doc.txt"
  fi
Note the back-quotes. You're executing what comes in between.

See here for Command Substitution in LDP abs guide.

Last edited by wfh; 11-08-2009 at 11:54 PM.
 
Old 11-08-2009, 11:57 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
grep text file 1>/dev/null && echo "exists" || echo "no"
 
Old 11-09-2009, 12:35 AM   #4
okos
Member
 
Registered: May 2007
Location: California
Distribution: Slackware/Ubuntu
Posts: 609

Original Poster
Rep: Reputation: 37
wfh,

Quote:
if [ `grep $x doc.txt` ] ; then
is missing a condition and did not work.


Quote:
Originally Posted by ghostdog74 View Post
Code:
grep text file 1>/dev/null && echo "exists" || echo "no"
Thanks a bunch it works.

Could you explain && and ||

Here is my actual script: (This is what did not work)
Quote:
########### Add cifs to fstab
if [ "$(cat /etc/fstab | grep '${NAME[$N]}/$LIST')" = "" ] ; then

echo "//${IP[$N]}/$LIST /home/server/${NAME[$N]}/$LIST cifs credentials=/etc/samba/.smbpass,_netdev,uid=1000,gid=1000 0 0" >> /etc/fstab
fi
Perhaps if I change it to:
Quote:
grep grep '${NAME[$N]}/$LIST' /etc/fstab 1>/dev/null && \
echo "//${IP[$N]}/$LIST /home/server/${NAME[$N]}/$LIST cifs credentials=/etc/samba/.smbpass,_netdev,uid=1000,gid=1000 0 0" >> /etc/fstab
 
Old 11-09-2009, 12:39 AM   #5
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,805

Rep: Reputation: 324Reputation: 324Reputation: 324Reputation: 324
Action && condition 1 || condition 2
translates as
Action AND condition OR condition 2
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] basic bash help (for, cat, grep): cross reference matching between files NoWone Linux - Newbie 3 10-18-2009 10:11 AM
BASH script: can't get a {cat > /dev/...} to work in background 2lunious Programming 16 10-03-2009 07:22 AM
cat in asm/ cat --show-all option Tux Linux - Software 1 09-02-2006 10:31 PM
bash: how to edit cat <filename> | grep <keyword> feature? sirpelidor Linux - Software 2 06-20-2005 03:00 PM
logrotate log files - keeping in order when using grep or cat and bash dmellem Linux - Software 6 02-11-2005 02:05 PM


All times are GMT -5. The time now is 04:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration