Solved

how to end if statment

Posted on 2013-01-08
8
257 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Just make everything consistent and your code should work, with or without the superfluous inner if condition
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks gerwinjansen!
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Did you not understand my comments?
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now