LinuxQuestions.org
Help answer threads with 0 replies.
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 06-25-2013, 02:01 PM   #1
richa07
LQ Newbie
 
Registered: Jun 2013
Location: India
Posts: 8

Rep: Reputation: Disabled
Read the folder name from file and check a keyword in one of the file inside that


Hi All

I have a file which contain list of folders name. All these folders have one wlserver.txt file.

Now I need to write a script which will read folders name from that file and for each folder it will check a keyword (Enable)is there in wlserver.txt file or not,

if its there that particular folder name should be stored in WithKeyword file else store in WithOutKeyword file

the code which I wrote is :

#!/bin/bash

file=/usr/Richa/study/ModelName.txt
while read line;
do
echo -e "$line\n";
grep "Enable" /usr/Richa/DB_Properties/$line/wlserver.cfg >>Models.txt
if [ $? != 0 ]; then
echo No
exit
else
echo YES
exit
fi
done < $file


In the above code when I delete the below code:
if [ $? != 0 ]; then
echo No
exit
else
echo YES
exit
fi

it shows all the lines from the file
but once I add that code, loop is not going to next line of the file

Can anybody please explain me why
and how can I above script
 
Old 06-27-2013, 03:43 AM   #3
richa07
LQ Newbie
 
Registered: Jun 2013
Location: India
Posts: 8

Original Poster
Rep: Reputation: Disabled
thanks , but there is one more error

Hi there

Thabks for the reply it helps, but now when i am trying to run that program i m getting an error
modelTest.sh: line 15: hgrep_Default_Error_old: command not found

for the below line.
$line >>ModelDont.txt

Note: here hgrep_Default_Error_old is the value of $line

Please let me know how can i save the folder name if above command is not working


Quote:
Originally Posted by unSpawn View Post
Don't use "exit" while inside your "while" loop.


BASH intros:
http://www.gnu.org/software/bash/man...ode/index.html
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html

BASH scripting guides:
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
# these are must reads so do read them!

Common questions / problems:
http://mywiki.wooledge.org/ParsingLs
http://mywiki.wooledge.orgDontReadLinesWithFor
http://mywiki.wooledge.org/UsingFind
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

The Advanced BASH scripting guide:
http://www.tldp.org/LDP/abs/html/

Bourne shell (compatibility):
http://www.grymoire.com/Unix/Sh.html

Also see:
http://www.linuxquestions.org/questi...l-links-35334/
http://www.linuxquestions.org/questi...nd-line-32893/
http://www.linuxquestions.org/questi...ks-4175433508/
 
Old 06-27-2013, 03:42 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
Run your script as '/bin/bash -vx /path/to/script 2>&1 | tee /tmp/output.txt' then post or attach contents of "/tmp/output.txt".
 
Old 06-28-2013, 12:06 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks.

As mentioned, if you use exit, you'll stop the script. In this case you don't need to do anything special. The loop will simply continue until every line in the file is processed.

I'd write it slightly differently though:

Code:
#!/bin/bash

infile=/usr/Richa/study/ModelName.txt
withkeywordfile=Models.txt
withoutkeywordfile=Notthere.txt

serverfile_prefix=/usr/Richa/DB_Properties
serverfile_suffix=wlserver.cfg

while read -r line || [[ -n $line ]]; do

    serverfile="$serverfile_prefix/$line/$serverfile_suffix"

    printf '%s\n\n' "$line"

    if grep "Enable" "$serverfile" >>"$withkeywordfile" ; then
        echo "YES"
    else
        echo "NO"
        echo "$line not found" >>"$withoutkeywordfile"
    fi

done <"$infile"

exit 0
The [[ -n $line ]] addition is there in case the input file doesn't have a final newline.

Finally, you should always take care to keep your code and data as separate as possible. Setting all of your filenames as variables at the top of the script is one recommended suggestion.

Scripting With Style
 
1 members found this post helpful.
  


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
Read a file, and send an email to the users inside the file karthikbhuvanagiri Programming 7 03-19-2013 07:01 PM
How to check if a string inside file is longer than 50 characters. secman110 Linux - Newbie 4 07-01-2012 05:28 PM
[SOLVED] How to read a file inside AWK? NabiVakili Programming 7 04-17-2011 04:44 PM
[SOLVED] dovecot: cannot read/write to folder (file) boxb29 Linux - Server 1 08-15-2009 01:53 AM
how can i default the max folder file size when it create inside a folder antony_csf Linux - Software 1 06-17-2004 03:26 AM


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