Results 1 to 5 of 5

Thread: why this error is occuring what's wrong in my script??

  1. #1
    Join Date
    Sep 2012
    Location
    Guntur
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    why this error is occuring what's wrong in my script??

    here is the screen shot of both the script and output i don't get it?? can you tell me

    and the directories mentioned in the script are exist in my system??

    the script is
    Code:
    #!/bin/bash
    #Only Sudo can run the script
    ROOT_UID=0 #Since Only users with $UID 0 have root privileges
    E_NOTROOT=87 #Non-root Exit Error
    if [ "$UID" -ne "$ROOT_UID" ]; then
    echo "You Must be root to run this script"
    exit $E_NOTROOT
    fi
    echo "Enter your Username"
    read uname
    
    if grep -q "^$uname:" /etc/passwd; then
    cd /home/$uname/projects
    echo "the files in this directory are"
    ls
    else
    echo "Incorrect username"
    fi
    echo "Enter File name"
    read fname
    unzip $fname /home/krishna/Desktop/test
    Attached Images Attached Images
    • File Type: jpg S.jpg (82.4 KB, 14 views)

  2. #2
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: why this error is occuring what's wrong in my script??

    You have written a bash script; I know this because on line 1 you have put
    Code:
    #!/bin/bash
    By typing "sh test.sh" you are trying to run it as a sh script. sh is not bash. This may work for some bash scripts because some bash scripts are also sh scripts. However it doesn't work in this case because UID is a variable that is set by the bash shell. It is not set by the sh shell.

    Your unzip command means this: search the zip archive for the file /home/krishna/Desktop/test and extract it. unzip warns you that it cannot find this file in the zip archive.

    If what you intend is to extract the archive into the directory /home/krishna/Desktop/test then you need to use -d as you have already been shown.

  3. #3
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: why this error is occuring what's wrong in my script??

    It should also be mentioned that checking the environment variable UID is inappropriate because it could be set to 0 by a non-root user.

    The preferred way to determine one's user ID is to use /usr/bin/id

    For example:
    Code:
    #!/bin/bash
    
    if [ `/usr/bin/id -u` -eq 0 ]; then
        echo "I'm root"
    else
        echo "I'm NOT root"
    fi
    To follow up on spjackson's comments, on some Linux systems such as Ubuntu, /bin/sh is linked to 'dash', not 'bash'. On RHEL/CentOS/Fedora, /bin/sh is linked to 'bash'. In summary, don't expect /bin/sh to be mapped to bash.

    If you want a script to be executable by the user, then change its mode using chmod; for example:
    Code:
    chmod u+x myscript.sh
    Then you can execute it using:
    Code:
    ./myscript.sh

  4. #4
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: why this error is occuring what's wrong in my script??

    double brackets instead of single brackets.

    [[ ]] instead of [ ]
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  5. #5
    Join Date
    Sep 2012
    Location
    Guntur
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: why this error is occuring what's wrong in my script??

    Quote Originally Posted by spjackson View Post
    You have written a bash script; I know this because on line 1 you have put
    Code:
    #!/bin/bash
    By typing "sh test.sh" you are trying to run it as a sh script. sh is not bash. This may work for some bash scripts because some bash scripts are also sh scripts. However it doesn't work in this case because UID is a variable that is set by the bash shell. It is not set by the sh shell.

    Your unzip command means this: search the zip archive for the file /home/krishna/Desktop/test and extract it. unzip warns you that it cannot find this file in the zip archive.

    If what you intend is to extract the archive into the directory /home/krishna/Desktop/test then you need to use -d as you have already been shown.
    Thank You Jackson it worked

Bookmarks

Posting Permissions

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