?
Solved

how to end if statment

Posted on 2013-01-08
8
Medium Priority
?
263 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 2000 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

Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

777 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