LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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, 08:44 AM   #1
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 236

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 09:35 AM.
 
Old 07-10-2014, 09:29 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,986
Blog Entries: 12

Rep: Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044
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, 09:40 AM   #3
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 236

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

Rep: Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044
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, 09:55 AM   #5
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 236

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

Thanks (I think?).
 
Old 07-10-2014, 09:59 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,986
Blog Entries: 12

Rep: Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044
"AND Geniuses" ...

Just toss yourself in the "other" pool.

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


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

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

All times are GMT -5. The time now is 07:07 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration