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