13

I've been working with bash for not more than 6 hours, and now I'm trying to create a menu that allows you to do some "fun" stuff :D.

My problem is with the if statement that check if you're in sudo mode. I want to have 3 conditions:

  1. If I execute the script with sudo mode, I'll be able to enter the path of the folder to be copied.
  2. If I execute the script without sudo mode, it'll ask me to insert the password, if I do that correctly the script will show me the echo and read op that allows me to write the path of the folder to be copied.
  3. The same as the point 2, but if I fail the authentication the application will be closed automatically.

Create a copy

2)
    if [ "$EUID" -ne 1 ]
      then 
            echo "Checking if you are in sudo mode..."
            echo "Error, please insert your password:"
            sudo ls /root
            if [ "$EUID" -ne 1 ]
                then
                    echo -e "\nCould not authenticate the user."
                    echo -e "For security reasons the application will be closed."
                    exit    
            else
                echo "==============================================================="
                echo -e "ALL COPIES HAVE A DEFAULT ROUTE: /home/patryk/Desktop/a/"
                echo "==============================================================="
                echo -e "Enter the path of the folder to be copied: "
                read origin
                rsync -avzh $origin /home/patryk/Desktop/a/`date-I`
            fi
    else
        echo "==============================================================="
        echo -e "ALL COPIES HAVE A DEFAULT ROUTE: /home/patryk/Desktop/a/"
        echo "==============================================================="
        echo -e "Enter the path of the folder to be copied: "
        read origin
        rsync -avzh $origin /home/patryk/Desktop/a/`date -I`    
    fi;;    
Patryk
  • 133
  • 1
  • 5
  • 1
    Welcome to SO, please show your coding efforts. – Cyrus Mar 18 '17 at 14:51
  • Yeah, sorry :D. – Patryk Mar 18 '17 at 14:56
  • did you try elif statement? – darvark Mar 18 '17 at 15:01
  • I was about to put it, but I didn't. I'm with this for about 5 hours... – Patryk Mar 18 '17 at 15:03
  • As an aside, your prolific use of `echo -e` seems misdirected. You should probably consider switching to `printf` instead. – tripleee Mar 18 '17 at 15:12
  • Your `EUID` will be switched back to your own after `sudo` finishes. It affects only the command you run under `sudo`, not the remainder of your script. See also http://stackoverflow.com/questions/37586811/pass-commands-as-input-to-another-command-su-ssh-sh-etc – tripleee Mar 18 '17 at 15:17
  • You should use double quotes around `"$origin"` unless you are completely sure that its value cannot ever contain any shell metacharacters. See http://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable – tripleee Mar 18 '17 at 15:19
  • Thanks for the help :) – Patryk Mar 18 '17 at 16:45

1 Answers1

15

If you have sudo credentials caching enabled (that is, after a successful sudo, you don't have to enter the password again for subsequent sudos) you could use the following trick:

Execute sudo true and check the return status. If the correct password was entered, the exit code will always be 0. Otherwise the exit code will be different.

if [[ "$EUID" = 0 ]]; then
    echo "(1) already root"
else
    sudo -k # make sure to ask for password on next sudo
    if sudo true; then
        echo "(2) correct password"
    else
        echo "(3) wrong password"
        exit 1
    fi
fi
# Do your sudo stuff here. Password will not be asked again due to caching.

At least on my system, root has the UID 0, not 1, so I adapted the if.

Socowi
  • 22,529
  • 3
  • 25
  • 46