• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

Using Bash I want to look for a string within a specific file and if it isn't there, I want to add it.

Using Bash I want to look for a string within a specific file and if it isn't there, I want to add it.

I want to check the file /etc/my.cnf and if the string skip-innodb is not there, I want to add it. Here's the catch, I want to add it directly after the line socket=/var/lib/mysql/mysql.sock

How do you do that?
0
heavenlydishes
Asked:
heavenlydishes
  • 3
1 Solution
 
NovaDenizenCommented:
/etc/my.cnf is usually a configuration file for the MySQL database.  It is actually a composite configuration file for several different classes of programs, with several different sections labeled like [client], [mysqld], [mysqldump], etc.  The socket= line can appear multiple times in the file, appearing at least in the [client] and [mysqld] sections.

Which sections do you want to modify, and how?
0
 
koppchaCommented:
#!/bin/bash

x=$(egrep -c "skip-innodb" text.txt)
if ((x == 0))
then
perl -i -pe 's/socket=(.*)$/socket=$1\nskip-innodb/' text.txt
fi
0
 
koppchaCommented:
If you have multiple lines in the file with socket=....
change the reg ex i mentioned above accoringly so that you select the correct line you want
0
 
ahoffmannCommented:
grep skip-innodb my.cnf && perl -i.bak -pe 's/(socket=.*)$/$1\nskip-innodb/' my.cnf
0
 
koppchaCommented:
Thanks for the grade :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now