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
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.
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)