Sunday, April 28, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 3034  / 3 Years ago, thu, august 19, 2021, 11:32:44

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.
pgkFetch;
else
echo "You will be prompted for user password. Do you want to temporarily disable this?";
read answer;
fi
if [ $answer !="y" ] || [ $answer !="n" ]; then null
elif [ $answer ="y" ]; then
$passPut|tee -a > /etc/sudoers
elif [ $answer ="n" ]; then
pkgFetch;
fi;
clear;
};
passChk;


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?


More From » command-line

 Answers
0

  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.



[#34676] Friday, August 20, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
naldis

Total Points: 257
Total Questions: 101
Total Answers: 111

Location: Kenya
Member since Sat, Feb 25, 2023
1 Year ago
;