Jump to content

bash shell script doesn't work?


Recommended Posts

I have a small script:

 

# !/bin/sh

# this script reads all files in the current directory and
# prints out the file containing the string 'main'

for file in *
do
if grep -q main $file
then
	echo $file
fi
done

exit 0

 

Here the result in terminal:

> pete cmd: ls -asl
total 92
4 drwxrwxr-x 2 xcross87 xcross87 4096 2009-02-13 19:57 ./
4 drwxrwxr-x 4 xcross87 xcross87 4096 2009-02-13 19:14 ../
8 -rw-rw-r-- 1 xcross87 xcross87   86 2009-02-10 21:18 bill.c
0 -rw-rw-r-- 1 xcross87 xcross87	0 2009-02-10 21:17 bill.c~
4 -rw-r--r-- 1 root	 root	  836 2009-02-10 21:20 bill.o
4 -rwxrwxr-x 1 xcross87 xcross87  199 2009-02-13 19:57 first*
4 -rw-rw-r-- 1 xcross87 xcross87   88 2009-02-13 19:46 first~
8 -rw-rw-r-- 1 xcross87 xcross87   81 2009-02-10 21:19 fred.c
0 -rw-rw-r-- 1 xcross87 xcross87	0 2009-02-10 21:17 fred.c~
4 -rw-r--r-- 1 root	 root	  836 2009-02-10 21:20 fred.o
8 -rwxr-xr-x 1 root	 root	 6147 2009-02-10 21:08 hello*
4 -rw-r--r-- 1 root	 root	 1882 2009-02-10 21:24 libfoo.a
8 -rw-rw-r-- 1 xcross87 xcross87   36 2009-02-10 21:21 lib.h
0 -rw-rw-r-- 1 xcross87 xcross87	0 2009-02-10 21:21 lib.h~
8 -rwxr-xr-x 1 root	 root	 6257 2009-02-10 21:26 program*
8 -rw-rw-r-- 1 xcross87 xcross87   86 2009-02-10 21:22 program.c
4 -rw-rw-r-- 1 xcross87 xcross87   76 2009-02-10 21:22 program.c~
4 -rw-r--r-- 1 root	 root	  876 2009-02-10 21:22 program.o
> pete cmd: ./first
grep: 1: No such file or directory
^Z
[11]+  Stopped				 ./first
> pete cmd: /bin/sh first
grep: 1: No such file or directory
^Z
[12]+  Stopped				 /bin/sh first
> pete cmd:

 

But if I put this script in an empty folder:

> pete cmd: ls -asl
total 12
4 drwxr-xr-x 2 xcross87 xcross87 4096 2009-02-13 20:02 ./
4 drwxrwxr-x 4 xcross87 xcross87 4096 2009-02-13 19:14 ../
4 -rwxrwxr-x 1 xcross87 xcross87  199 2009-02-13 19:57 first*
> pete cmd: ./first
first
> pete cmd:

 

It totally works !

 

 

Can anyone explain this for me?

 

I appreciate help.

 

Sincere,

Link to comment
Share on other sites

I'm not sure what you're trying to achieve? Please explain what result your trying to obtain. Anyway, from my system:

 

ian@esprit:~/test$ ls -l
total 4
-rw-r--r-- 1 ian ian   0 2009-02-13 13:37 bill
-rwxr-xr-x 1 ian ian 214 2009-02-13 13:33 first
-rw-r--r-- 1 ian ian   0 2009-02-13 13:37 fred
-rw-r--r-- 1 ian ian   0 2009-02-13 13:37 hello
-rw-r--r-- 1 ian ian   0 2009-02-13 13:37 libfoo
-rw-r--r-- 1 ian ian   0 2009-02-13 13:37 lib.h
-rw-r--r-- 1 ian ian   0 2009-02-13 13:37 program

ian@esprit:~/test$ ./first 
first

 

seems to work for me but I don't know what you're trying to achieve.

Link to comment
Share on other sites

Have you tried this?

grep -l main *

Is that what you're trying to do? I am guessing that bash is failing to expand your filenames properly, possibly trying to expand the ~ signs? Just a guess.

 

And yes, I'm guessing that the echo is correct, just to list the files which contain the characters "main". But that's what the above grep command should do too. Even better, you can make it recursive to search a whole directory tree with "grep -Rl main *" if you want.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...