Avatar of atom_jelly
atom_jelly
Flag for United States of America asked on

how to end if statment

i have a nested if statement that will keep inserting the string into the file I want the if statement to see that it is true then do nothing else if string is not there then insert. So when i runs the next time if its true do nothing here is my if statement:

if grep -q "PATH=$PATH:$HOME/bin' "root/.bashrc"
then
echo "The executable search path PATH does exist"
else
     if ! grep -q "PATH=$PATH:$HOME/bin' "root/.bashrc"
     then
     echo "Root search path is missing will be inserted"
sed -i -e '3a\
\#User blah blah blah' "/root/.bashrc"
sed -i -e '4a\
PATH=blah blah blah' "/root/.bashrc"
     fi
fi
Unix OSScripting LanguagesShell Scripting

Avatar of undefined
Last Comment
ozo

8/22/2022 - Mon
tsnirone

if [ condition1 ] ; then
     echo "true1"
else
       if [ condition2 ] ; then
             echo "true2"
       else
             echo "false"
       fi
fi

Should work. Perhaps you need the brackets? (if so remember the white spaces)
ozo

Your quotes are unmatched.
Did you mean "PATH=$PATH:$HOME/bin" or 'PATH=$PATH:$HOME/bin' ?
Either way, it's different from the PATH=blah blah blah that's inserted (assuming that .bashrc has 4 lines) so the condition would not change the next time it runs
Anyway, "root/.bashrc" and "/root/.bashrc" are different files,  unless your working directory happens to be /

The if inside the else appears to be superfluous.
atom_jelly

ASKER
I guess now I just need some help with how to write this out I just want it to stop if the string PATH=$PATH:$HOME/bin is in the file /root/.bashrc. I don't know how to begin putting that together.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ozo

Just make everything consistent and your code should work, with or without the superfluous inner if condition
ASKER CERTIFIED SOLUTION
Gerwin Jansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
atom_jelly

ASKER
Thanks it sure helps when you can see from someone's perspective this helps me very much.

Always learning but its better then being dead. Thanks.
atom_jelly

ASKER
Thanks gerwinjansen!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ozo

Did you not understand my comments?