LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-10-2014, 07:44 AM   #1
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 258

Rep: Reputation: Disabled
Strange behavior


I have these lines in a file called rogue.txt:
atd
bash
-bash
bdi-default
boa
cleanup

I search through the file one line at a time looking to see if one of the words is therein:
Code:
#!/bin/bash
#
while read line
do
if [ $(grep -c $line rogue.txt) -gt 0 ]
   then
   echo "word in rogue.txt"
   else
   echo "word not in rogue.txt"
fi
done <test.txt
When $line is -bash, the routine passes the echo "word not in rogue.txt" message and also causes the while loop to stop at that point. Any clues as to why this is happening?
Thanks.

Last edited by battles; 07-10-2014 at 08:35 AM.
 
Old 07-10-2014, 08:29 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
This works fine. Firstly please place code within [code][/code] tags.

My contents of rogue.txt are the same as yours.

My contents of test.txt is the word bash.

The script as I've copied it, but note that I've indented as well as put in #!/bin/sh, I called it sear.sh:

Code:
#!/bin/sh

while read line
do
    if [ $(grep -c $line rogue.txt) -gt 0 ]
    then
        echo "word in rogue.txt"
    else
        echo "word not in rogue.txt"
    fi
done <test.txt
The output:

Code:
~/testcode$ ./sear.sh 
word in rogue.txt
~/testcode$
If you're not seeing this, then I wonder if there are other parts of your script which you're not showing; can you elaborate and also check what shell you're running?

Another suggestion is to put the following near the top of your script to enable debugging output:

Code:
set -xv
HOWEVER, this all assumes you're using BASH.
 
Old 07-10-2014, 08:40 AM   #3
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 258

Original Poster
Rep: Reputation: Disabled
I put in the entire code above. Never seen set -xv, pretty nice debugging process.
 
Old 07-10-2014, 08:50 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Quote:
Originally Posted by battles View Post
I put in the entire code above. Never seen set -xv, pretty nice debugging process.
Some other suggestions Blog Entry on BASH Programming.
 
Old 07-10-2014, 08:55 AM   #5
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 258

Original Poster
Rep: Reputation: Disabled
I resemble that remark: "Bash Scripting for Dummies"

Thanks (I think?).
 
Old 07-10-2014, 08:59 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
"AND Geniuses" ...

Just toss yourself in the "other" pool.

Glad you got it working, and yes "set -xv" is extremely useful.
 
  


Reply



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
NTP strange behavior pixellany Linux - Software 4 06-28-2009 06:26 AM
/etc/profile strange behavior ddales SUSE / openSUSE 4 03-27-2006 11:23 PM
Very Strange Behavior raysr Mandriva 4 08-31-2004 02:06 PM
Strange Behavior andrewb758 Linux - Hardware 5 08-31-2003 02:42 PM
strange behavior abhijit Linux - General 3 07-09-2003 11:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:56 PM.

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
Open Source Consulting | Domain Registration