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?
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.