bash script - read command

Hi,

I wrote the following bash script which works fine as root:

#!/bin/bash
echo -n "Which user do you want to assess (i.e. user196)? ";
read name1;
ls -l | grep $name1 | gawk ‘{ sum += $5 } END { print (sum/(1024*1024)),“MB” }’

Execution as follow:

eaghp7:/pascal/users/cfd/fred/test # source count-user.sh
Which user do you want to assess (i.e. user196)? user196
0.000159264 MB
eaghp7:/pascal/users/cfd/fred/test #

So far so good.
Now when executed as a user (user196) the following error message appears:

eaghp7 262% ls
total 5
drwxr-xr-x 2 user196 rdduk 80 2009-08-11 15:16 .
drwxr-xr-x 21 user196 rdduk 528 2009-08-11 13:24 …
-rwxrwxrwx 1 user196 rdduk 167 2009-08-11 15:16 count-user.sh
eaghp7 263% source count-user.sh
Which user do you want to assess (i.e. user196)? read: Command not found.
name1: Undefined variable.
eaghp7 264%

The aim is to have the script executed by end users (no root password). How can I make it work for them please?

Any help is appreciated.

Thanks

Fred

Are you using csh as your terminal shell by any chance?

When you do a source, the first line #!/bin/bash does nothing. The contents of the file are executed by the current shell as if you had typed them in. And read is not a built-in command known to csh. To make sure the commands in the script are executed by /bin/bash as you intended, you should execute the script, not source it.

chmod 755 count-user.sh
./count-user.sh

Hi
It works fine if it’s made executable and use


../<nameofscript>


Cheers Malcolm °¿° (Linux Counter #276890)
SUSE Linux Enterprise Desktop 11 (x86_64) Kernel 2.6.27.25-0.1-default
up 6 days 18:09, 2 users, load average: 0.09, 0.09, 0.18
GPU GeForce 8600 GTS Silent - Driver Version: 190.18

Thanks very much it works.

Fred