Solved

how to end if statment

Posted on 2013-01-08
8
260 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
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

830 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