I am fresh into Ubuntu and one of my goals is shell scripting for personal (and maybe public) use.
I'm a novice, though I do understand some of the basics (e.g. what a variable, string, loop, etc... is) but to get the most of scripting I need to learn in-depth. I figure the best way to do that is to jump right into scripting and ask questions only pertinent to the stage I am at in my attempted script.
Scenario: I have edited my sudoers file to allow my non-root user to run sudo commands without being prompted for a password.
Question: In vim, what would be the best code to use for 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]?
Layout - 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.
Any help with this would be greatly appreciated and assist me in my journey to writing shell scripts.
- UPDATED
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?