LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Read the folder name from file and check a keyword in one of the file inside that (https://www.linuxquestions.org/questions/linux-newbie-8/read-the-folder-name-from-file-and-check-a-keyword-in-one-of-the-file-inside-that-4175467351/)

richa07 06-25-2013 01:01 PM

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

unSpawn 06-25-2013 02:03 PM

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/

richa07 06-27-2013 02:43 AM

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 (Post 4978505)
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/


unSpawn 06-27-2013 02:42 PM

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".

David the H. 06-28-2013 11:06 AM

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


All times are GMT -5. The time now is 03:07 PM.