Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


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];
    grep -A 1 "Drive Model" "$dir"/sysconfig.out | tail -1 >> /home/xtee/sst-logs.out
    grep -m 1 "Physical memory size" "$dir"/node0/setupsys.out | tail -1 >> /home/xtee/sst-logs.out


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?

share|improve this question

closed as too localized by Ignacio Vazquez-Abrams, warl0ck, Michael Mrozek Sep 28 '12 at 13:58

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

STOP CROSSPOSTING THIS QUESTION. WE ARE NOT AMUSED. – Ignacio Vazquez-Abrams Sep 28 '12 at 3:11
You need to be a little more specific – warl0ck Sep 28 '12 at 9:54
Please don't cross-post all over the place. Closing here – Michael Mrozek Sep 28 '12 at 13:58
up vote 1 down vote accepted
if [ -r "$dir"/sysconfig.out ];

Notice the whitespace between t and ].

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.