Solved

how to end if statment

Posted on 2013-01-08
8
261 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help Creating PowerShell Script 5 79
Shell script issue 4 63
while read ID; do 4 78
bash script problem with multiple processes 10 18
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
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…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

736 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