I have edited my sudoers file to allow my non-root user to run sudo commands without being prompted for a password. In vim, how do I create a function that checks whether this condition is true, if not, prompt the user if they want the script to edit and save the sudoers file to make this condition true?

If condition is true, carry-on with rest of script. If condition is not true, the script silently edits/adds the line: %sudo ALL=(ALL:ALL) NOPASSWD: ALL in the sudoers file, saves and then continues on with the next part of the script.

So I've started writing the function using the code:

... #lines 1-5
passChk() {
passPut="%sudo ALL=(ALL:ALL) NOPASSWD:ALL";
passRd=sudo grep -is "$passPut" --file=/etc/sudoers;
if [ $passRd == $passPut ]; then #if sudoers already contains line from $passPut,     
                                 #exit passChk and proceed to pkgFetch.
   echo "You will be prompted for user password. Do you want to temporarily disable this?"; 
   read answer;
if [ $answer !="y" ] || [ $answer !="n" ]; then null
elif [ $answer ="y" ]; then
   $passPut|tee -a > /etc/sudoers
elif [ $answer ="n" ]; then

I am recieving an error on line 9 if [ $passRd = $passPut ]; then pkgFetch; [: too many arguements. I don't get it. I'm trying to test if directory in $passRd contains string from $passPut. What am I doing wrong?

  1. "The bash cookbook" is an excellent resource, you can get the basics online but this will save your butt when you're pressed for time.

  2. Never use double quotes on things you intend to be bare strings e.g. not subject to bash substitution, so that passput line should be single quoted.

  3. Always double quote variable references in a test construct [], [[ ]], (()), etc. Bad things can happen if you don't, read up on it. The preferred construct is:

[ "${passRd}" = "${passPut}" ]
  1. The semi-colons after the function calls are unnecessary and clutter the code.

  2. man test, there must be spaces between every argument in a test call.

  3. set -x on the CLI or pass -x to #!/bin/bash to assist in debugging, see help set for details.

  • As was mentioned in the beginning, I am a novice. This is an example of a good clear-cut answer that will assist me in my learning process. I have added "set -x" in my code and ran the script with the debugging, even Googled error messages I encountered but resulted with nothing that was specific for the circumstance. Very much appreciate you taking the time to assist me with this. +1 – Kevin Wyman Oct 31 '12 at 7:49
  • When I first started system scripting, I found bash very difficult to pick up, the space sensitivity, the shell expansion, the arsenal of options to test, and how you can use it depending on the braces involved. It is complex especially when you create larger programs. I found that I really needed to invest in bash as a programming language to "get it right". Looking at it as a shell first and independent language second messed up my expectations, creating more frustration than necessary. Python is the same way, if you commit to it's way of working, it's easy, otherwise... – ppetraki Oct 31 '12 at 13:50

This book may be best resource for learning Bash Scripting in Ubuntu.

  • Thanks for the share. I have been reading as I realize this will be a necessary step in learning bash scripting. However, more-so what I'm looking for is anyone willing to post a code block of a function that can do this. It speeds up the learning if there's something to look at and analyze. – Kevin Wyman Oct 27 '12 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.