Reputation: 159
I am making a simple shell script which will minimize the time I spend in searching all directories under a parent directory and grep some things inside some files. Here's my script.
#!/bin/sh
MainDir=/var/opt/database/1227-1239/
cd "$MainDir"
for dir in $(ls); do
grep -i "STAGE,te_start_seq Starting" "$dir"/his_file | tail -1 >> /home/xtee/sst-logs.out
if [ -f "$dir"/sysconfig.out];
then
grep -A 1 "Drive Model" "$dir"/sysconfig.out | tail -1 >> /home/xtee/sst-logs.out
else
grep -m 1 "Physical memory size" "$dir"/node0/setupsys.out | tail -1 >> /home/xtee/sst-logs.out
fi
done
The script is supposed to grep the string STAGE,te_start_seq Starting
under the file his_file
then dump it sst-logs.out
which it does. My problem though is the part in the if
statement. The script should check the current directory for sysconfig.out
, grep drive model
and dump it to sst-logs.out
if it exists, otherwise, change directory to node0
then grep physical memory size
from setupsys.out
and dump it to sst-logs.out
. My problem is, it seems the if then else
statement seems not to work as it doesn`t dump any data at all but if i execute grep manually, i do have data.
What is wrong with my shell script? Is there any more efficient way in doing this?
Upvotes: 0
Views: 548
Reputation: 346
First, when you say "change directory to node0 then grep" you're not changing dirs here you're just using a different path. Do you really want to change directories? Is "$dir/node0/" or is it outside of the directory "$dir"?
Second, does /var/opt/database/1227-1239/ have any files with spaces? Can you show the output of ls?
I wouldn't use 'for dir in $(ls); do'
You'll start to run into a lot of pitfalls when you attempt to parse ls
for dir in * ; do
if [[ -d "$dir" ]] ; then
grep -i "STAGE,te_start_seq Starting" "$dir"/his_file | tail -1 >> /home/xtee/sst-logs.out
if [ -f "$dir"/sysconfig.out]; then
grep -A 1 "Drive Model" "$dir"/sysconfig.out | tail -1 >> /home/xtee/sst-logs.out
else
grep -m 1 "Physical memory size" "$dir"/node0/setupsys.out | tail -1 >> /home/xtee/sst-logs.out
fi
if
done
You can also use find -maxdepth if you got it, otherwise it turns into a .. -prune -o ..
My only guess is that $(ls) has some files with bad characters or spaces so when you manually process the files everything works because you're escaping these characters?
Upvotes: 2
Reputation: 7853
Why don't you use something like
find . -name "*" -print | xargs grep -i <text>
To search text
in all files under current directory
Upvotes: 0