Results 1 to 4 of 4

Thread: bash script - read command

  1. #1
    fredk009 NNTP User

    Default 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

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: bash script - read command

    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

  3. #3
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    32,352
    Blog Entries
    15

    Default Re: bash script - read command

    Quote Originally Posted by fredk009
    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
    Hi
    It works fine if it's made executable and use
    Code:
    ../<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


  4. #4
    fredk009 NNTP User

    Default Re: bash script - read command

    Thanks very much it works.

    Fred

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •