how to use if condition with sed command in BASH

 
Thread Tools Search this Thread
Operating Systems Linux Red Hat how to use if condition with sed command in BASH
# 1  
Old 04-30-2012
how to use if condition with sed command in BASH

Urgent help with bash scripting

1- i am using grep to find a string called: tinker panic 0 in a file /etc/ntp.conf
if the string is not there, i want to add the strings in /etc/ntp.conf file in the first line of the file. if not do nothing or exit.

2- also i want to add # in front of the following lines in /etc/ntp.conf only if # is not there.


Code:
#server 127.127.1.0 # local clock#fudge 127.127.1.0 stratum 10

Here what i have so far, but not working properly:


Code:
#!/bin/bash#const='tinker panic 0';if [ -e /etc/ntp.conf ] ; thenfound = 'grep "$const" /etc/ntp.conf' >/dev/null 2>&1 if [ "$found" -eq 0 ] ; then echo "not found" ; sed -i.bak '1 i\tinker panic 0' /etc/ntp.conf fifi
Thank you in advance.
# 2  
Old 04-30-2012
This is a simple script that should do it for you:
Code:
#!/bin/bash
const='tinker panic 0'
if [ -e /etc/ntp.conf ]; then
  grep -q -e "$const" /etc/ntp.conf
  if [ $? = 1 ]; then
    echo "$const  ; not found but will be inserted as first line!"
    sed -i.bak '1 i\tinker panic 0' /etc/ntp.conf
  fi
fi
# Add '#' to beginning of line if it is not already there
sed -i.bak \
    -e 's/^server 127.127.1.0$/#server 127.127.1.0/' \
    -e 's/^local clock$/#local clock/' \
    -e 's/^fudge 127.127.1.0 stratum 10$/#fudge 127.127.1.0 stratum 10/' /etc/ntp.conf

hth
This User Gave Thanks to spacebar For This Post:
# 3  
Old 05-01-2012
Thank you very Much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed, awk or another bash command to modify string with the content of another file

Hello everybody, I would like modify some strings using sed or another command line with the content file. For example: - {fqdn: "server-01" , ip: "server-01"} - {fqdn: "server-02" , ip: "server-02"} - {fqdn: "server-03" , ip: "server-03"} - {fqdn: "server-04" , ip: "server-04"} My... (4 Replies)
Discussion started by: dco
4 Replies

2. UNIX for Beginners Questions & Answers

Condition in bash script

I want get from user and pass these parameters to bash script. script should copy files in user home directory. FYI: each file might be exist or not, might be one of them exist or four of them. Here is my script, it always copy file1 and seems only one of them execute! #!/bin/bash for... (6 Replies)
Discussion started by: indeed_1
6 Replies

3. Shell Programming and Scripting

Need bash script to use a sed command as a variable

I need to be able to use a sed command as a variable in a bash script. I have the sed command that almost works the way I want it. the command is sed -n '/inet/,/}/p' config.boot This gets me this result: inet 192.168.1.245 } I need to get the IP address into a variable so I... (9 Replies)
Discussion started by: edlentz
9 Replies

4. Shell Programming and Scripting

Executing sed command inside a bash script

I want to run commands inside a bash script. An example is I want to pass the command in a string as regexp as an argument to the script, then run sed on the bash variable sed.sh regexp sed.sh "-i \"s/<p>//g\"" then call sed "$regexp" $fl (3 Replies)
Discussion started by: Kangol
3 Replies

5. Shell Programming and Scripting

Creating a condition on a bash script

I wrote a code to find codons in a DNA string. The only problem I have is how do I make the code only work for a file with DNA. This means the file only has the characters a,c,g,t and no white space characters. (3 Replies)
Discussion started by: germany1517
3 Replies

6. Shell Programming and Scripting

Add another condition to bash for when not met

In the below I can not seem to add a line that will add Not low if the statement in bold is not true or meet. I guess when the first if statement is true/meet then print low, otherwise print Not low in $(NF + 1). I am not sure how to correctly add this. Thank you :). if(low <= $2 && $2 <=... (5 Replies)
Discussion started by: cmccabe
5 Replies

7. Shell Programming and Scripting

[Solved] SED - Bash - Inserting multi Tab character in the command

Hello. I am using : sed -i -e '/§name_script§/a#'"${MY_TAB11}"'# \ #'"${MY_TAB1}"'The Standard way'"${MY_TAB7}"'# \ #'"${MY_TAB1}"'==============='"${MY_TAB7}"'# \ ' "$CUR_FILE" Is there a better way to define "MY_TAB7","MY_TAB11" in other way than : MY_TAB1=$'\t' MY_TAB2=${MY_TAB1}$'\t'... (2 Replies)
Discussion started by: jcdole
2 Replies

8. Programming

if condition in bash

Hi, I have meaning to include an if condition statement in my code to check the directory for existing output files and if its existing i want the program to delete it before doing the succeeding command. i just dont know the correct syntax for it. thanks much guys, this forum has indeed been very... (4 Replies)
Discussion started by: ida1215
4 Replies

9. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

10. Shell Programming and Scripting

Bash: if condition as variable

How can I use a variable that has the conditions for the if statement stored in it? my test script condition=" || || " if "$condition" then echo "true" else echo "false" fi output $ ./test2.sh ./test2.sh: line 3: || || : command not found false (2 Replies)
Discussion started by: curlee2002
2 Replies
Login or Register to Ask a Question