Solved

how to end if statment

Posted on 2013-01-08
8
262 Views
Last Modified: 2013-01-08
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
0
Comment
Question by:atom_jelly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 3

Expert Comment

by:tsnirone
ID: 38754671
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)
0
 
LVL 84

Expert Comment

by:ozo
ID: 38754680
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.
0
 

Author Comment

by:atom_jelly
ID: 38754809
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.
0
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
LVL 84

Expert Comment

by:ozo
ID: 38754836
Just make everything consistent and your code should work, with or without the superfluous inner if condition
0
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 500 total points
ID: 38754873
Try this:

if grep -q "PATH=\$PATH:\$HOME\/bin" test.txt
then
	echo "The executable search path PATH does exist"
else
	if ! grep -q "PATH=\$PATH:\$HOME/bin" test.txt
	then
		echo "Root search path is missing will be inserted"
		sed -i -e '1i#' test.txt
		sed -i -e '2iPATH=\$PATH:\$HOME/bin' test.txt
	fi
fi 

Open in new window


Note that I use test.txt instead of your .bashrc (for testing purposes).
0
 

Author Comment

by:atom_jelly
ID: 38754912
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.
0
 

Author Closing Comment

by:atom_jelly
ID: 38754914
Thanks gerwinjansen!
0
 
LVL 84

Expert Comment

by:ozo
ID: 38754973
Did you not understand my comments?
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question