LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-06-2019, 05:51 AM   #1
OnionBoy
LQ Newbie
 
Registered: Mar 2019
Posts: 6

Rep: Reputation: Disabled
Post bash script help needed


Hi everyone, This is the first time I posting question here. Please be nice to me. I'm not a very experience Linux user so I really need some help here because of my limited knowledge to Linux.

I want to write a script to change few setting in few configuration files in RHEL 6 and 7. Below are some of the thing i want to archive:

1. Make a copy for all the files that will be modify by the script with format file.DDMMYYYY. If found a backup exist then ignore. Cause I done some testing and found out that if the scripts executed more than once in the same date, then the backup file will be the one that have made changes of.
2. The script will find certain parameter in a file and change the value if found. If not found then add the parameter in.
3. If the file is not found, then create the file and put the parameters in the file. Set permission for it as well.

Below is the noob script I have done, I know there are a lot of improvement with the script. The script is just an example of what needed to archive. Hope there will be nice guys willing to share their knowledge with me.

Code:
#!/bin/bash

###backup file###Please guide me how to ignore if the backup file already exist.###
cp -Rp /home/admin/Documents/1_file /home/admin/Documents/1_file.$(date +"%d%h%Y")
cp -Rp /home/admin/Documents/CIS.conf /home/admin/Documents/CIS.conf.$(date +"%d%h%Y")
cp -Rp /home/admin/Documents/2_file.conf /home/admin/Documents/2_file.conf.$(date +"%d%h%Y")

###Add line to the file###
echo "i successful add line to the file" > /home/admin/Documents/1_file

###Append line to the file###
echo "find and replace=not success" >> /home/admin/Documents/1_file

###Find word and replace###
sed -i 's/i successful add line to the file/admin successful add line to the file/' /home/admin/Documents/1_file

###Find line and replace###Problem facing here is the "=" might have space or non space, how do i find the setting and replace whatever after with a value?###
sed -i 's/find and replace=not success/find and replace=success/' /home/admin/Documents/1_file

###Create file and add text to it###How to check if the file not exist, then only create?###
touch notfound.conf
cat<<END>notfound.conf
Line1
Line2
Line3
Line4
Line5
END


###Find parameter in a file and replace the value###
sed -i "s/Line4/Line5/" notfound.conf
sed -i "s/Line2/Line4/" notfound.conf

###Change permission and owner###
chmod 644 notfound.conf
chown admin:admin notfound.conf

###Find parameter with hashtag and unhastag them/vice versa, change the value also###Same issue with previous one, the output is the word will be insert in between which have two = ####
sed -i "r/#Try unhashtag me*/Try unhashtag me = success/" /home/admin/Documents/2_file.conf
sed -i 's/Try hashtag/#Try hashtag* = sucess/' /home/admin/Documents/2_file.conf

Last edited by OnionBoy; 03-06-2019 at 06:22 AM. Reason: Add Code Tags
 
Old 03-06-2019, 06:08 AM   #2
dc.901
Senior Member
 
Registered: Aug 2018
Location: Atlanta, GA - USA
Distribution: CentOS/RHEL, openSuSE/SLES, Ubuntu
Posts: 1,005

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Quote:
Originally Posted by OnionBoy View Post

###backup file###Please guide me how to ignore if the backup file already exist.###
Here is an example for how to check if file exists: https://stackoverflow.com/questions/...-exist-in-bash

Also, if you use code tags, it is lot easier to read...
 
1 members found this post helpful.
Old 03-06-2019, 06:16 AM   #3
OnionBoy
LQ Newbie
 
Registered: Mar 2019
Posts: 6

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by dc.901 View Post
Here is an example for how to check if file exists: https://stackoverflow.com/questions/...-exist-in-bash

Also, if you use code tags, it is lot easier to read...
Thanks, will try the code tags next time.
 
Old 03-06-2019, 07:08 AM   #4
OnionBoy
LQ Newbie
 
Registered: Mar 2019
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dc.901 View Post
Here is an example for how to check if file exists: https://stackoverflow.com/questions/...-exist-in-bash

Also, if you use code tags, it is lot easier to read...
Really thanks for the guide...So I tested with the recommended step:
Code:
#!/bin/bash
if [ ! -e /home/admin/Documents/new.conf ]; then
echo 'create new.conf'
touch new.conf
cat<<END>> new.conf
Setting_A=A
Setting_B=2
Setting_C=F
Setting_D = 0
END
        else
if [ ! -e /home/admin/Documents/new.conf.$(date +"%d%h%Y") ]; then
cp -Rp /home/admin/Documents/new.conf /home/admin/Documents/new_conf.$(date +"%d%h%Y")
sed -i 's/Setting_C=*/Setting_C=C/' /home/admin/Documents/new.conf
sed -i 's/Setting_D*/Seeting_D=4/' /home/admin/Documents/new.conf
fi
fi
The problem i have is with the changes on the text inside file....the output i getting is below if I execute more than once:
Quote:
root@ubuntu00:/home/admin/Documents# cat new.conf
Seeting_D=4A=A
Seeting_D=4B=2
Seeting_D=4C=CF
Seeting_D=4 = 0
anyone can help on that part....
 
Old 03-06-2019, 09:36 AM   #5
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,950

Rep: Reputation: Disabled
You need a leading '.' when using globs with sed.

Consider this example.

Code:
$ foo='Setting_C=foo'
$ echo $foo | sed 's/Setting_C=*/Setting_C=C/'
Setting_C=Cfoo
$ echo $foo | sed 's/Setting_C=.*/Setting_C=C/'
Setting_C=C
 
1 members found this post helpful.
Old 03-06-2019, 11:37 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
your two timing cat fix.
Code:
#!/bin/bash

cat<<END> new.conf
Setting_A=A
Setting_B=2
Setting_C=F
Setting_D = 0
END

cat ~/new.conf

[[ ~/new.conf ]] && \
{ sed -i 's#.*Setting_D.*#Setting_D=4#' ~/new.conf  ; }

cat ~/new.conf

Last edited by BW-userx; 03-06-2019 at 11:42 AM.
 
Old 03-06-2019, 02:46 PM   #7
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,950

Rep: Reputation: Disabled
You can drop the back slashes after '&&' or '||'.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed for using awk to parse a file to make array for bash script tallmtt Programming 12 04-14-2012 01:16 PM
[SOLVED] Help needed with bash script and zenity. Wizard^^ Programming 4 10-29-2010 10:01 PM
Bash Script - Help needed manya Programming 14 07-22-2010 03:38 AM
Bash script. help needed tazthecat Linux - General 2 09-30-2005 01:54 AM
bash script help needed Henster Linux - General 4 08-21-2005 09:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration