Link to home
Start Free TrialLog in
Avatar of atom_jelly
atom_jellyFlag 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"
echo "The executable search path PATH does exist"
     if ! grep -q "PATH=$PATH:$HOME/bin' "root/.bashrc"
     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"
Avatar of tsnirone

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

Should work. Perhaps you need the brackets? (if so remember the white spaces)
Avatar of 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.
Avatar of atom_jelly


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.
Just make everything consistent and your code should work, with or without the superfluous inner if condition
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Thanks gerwinjansen!
Did you not understand my comments?