Running a Script to change a wp-config file DB Name - linux

I teach students how to fix wordpress sites and I would like to write a script that looks at what information they have in their wp-config.php and add a single letter or number to the database name.
For example the line is such
define('DB_NAME', 'cpanelUser_NameofDB');
I would like to add a number or a line to the end of NameofDB
I can use this to isolate the cpanelUser_NameofDB
grep -i 'DB_NAME' wp-config.php | cut -d"'" -f4
but I'm not sure how to add information, nor if this is the correct script I should run to get there. I would also like it to not matter what the name of the database is since it will be ran on multiple sites. I'm sure I could use regex but I'm not too versed in that and would not know where to start. Help please!

You can use WP CLI on the server:
Go to the website root:
cd /var/www/mysite.com/htdocs
...and list all the wp-config values:
wp config list
...or set the value you need to change:
sudo wp config set DB_NAME put_my_custom_db_name_here --allow-root
See more WP CONFIG features here:
https://developer.wordpress.org/cli/commands/config/

You can use sed for this purpose to fix this line:
#!/bin/bash
id=34
sed -i "s/^.*DB_NAME.*$/define('DB_NAME', 'cpanelUser_NameofDB${id}');/" wp-config.php
flag -i means that we do change in file directly.
Alternatively you can make template file and generate new file while sed works on stdin and stdout
#!/bin/bash
id=34
cat wp-config.php-template| sed "s/^.*DB_NAME.*$/define('DB_NAME', 'cpanelUser_NameofDB${id}');/" > wp-config.php.$id

Related

Where is the config file store for the xfce4-plugin "notification area" alias "systray"

I am on Qubes OS. That means fedora-25 as dom0. I would like to change the configs for "notification area" alias "systray" plugin of xfce. How can I do it. I would like to delete/add one item.
The Gui only gives me the option to hide with ugly arrow on the side or to "clear all known applications". However, regarding the last option I am afraid to lose the notification area as it is and never get it back.
I looked with the "find" command for "xfce4" and "xfce4-plugins" and so on. All the files I could find, e.g. in ~/.config/xfce4, could not help me. I can nowhere find a config file for the plugin.
Thanks in advance :)
Known applications is stored as an array in xfconf, in the xfce4-panel channel and under the property /plugins/plugin-[id]/known-items, where the plugin id is dynamic and depends on the order plugins were added to panel.
You could hack your way messing with ~/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-panel.xml but I strongly advise you not to, instead use xfconf-query to read and set values.
I'm going to write down some snippets below so you can use them to craft a script that suits your needs:
# Find the plugin id, can be empty if systray is not present
xfconf-query -c xfce4-panel -p /plugins -l -v | grep systray | grep -Po "plugin-\\d+" | head -n1
# Get array of current known apps
xfconf-query -c xfce4-panel -p /plugins/$PLUGIN_ID/known-items | tail -n +3
# Set array of known apps
xfconf-query -c xfce4-panel -p /plugins/$PLUGIN_ID/known-items -t string -s value1 -t string -s value2 ...

Replace a single placeholder in a .conf file using bash

Hi everyone I am still learning bash at the moment but I am writing
a script for work that is being used to install Docker and CNTLM because we are running behind some proxies.
I have the installations working but am struggling to change 2 variable placeholders in the cntlm.conf file below.
cnlm.conf
#
# Cntlm Authentication Proxy Configuration
#
# NOTE: all values are parsed literally, do NOT escape spaces,
# do not quote. Use 0600 perms if you use plaintext password.
#
Username $MyUserName
Domain NTADMIN
Password $MyPassWord
# NOTE: Use plaintext password only at your own risk
# Use hashes instead. You can use a "cntlm -M" and "cntlm -H"
# command sequence to get the right config for your environment.
# See cntlm man page
# Example secure config shown below.
PassLM 93409221097752460192457192457999
PassNT 08992693829837928372938729387229
### Only for user 'testuser', domain 'corp-uk'
PassNTLMv2 02793865976487363876348763873467
# Specify the netbios hostname cntlm will send to the parent
# proxies. Normally the value is auto-guessed.
#
# Workstation netbios_hostname
I have been able to change the
PassLM
PassNT
PassNTLMv2
by using replace line with 'sed' but I am unable to change the $MyUserName and $MyPassWord from the variables being used in the bashscript.
Any ideas on how I can do this?
There are various alternatives:
To replace them using sed on a "template" and creating a new file, you can do it like this:
sed 's/\$MyPassword/MySuperPassword/' cnlm.conf > cnlm.new.conf
Now, if you will replace into the same file and you don't know the last value of the password, you can do:
sed -ri 's/^(Password *).*$/\1MySuperPassword/' cnlm.conf
If your new password is in a shell variable, then you can execute the last command like this:
newPasswd="abcde"
sed -ri "s/^(Password *).*$/\1${newPasswd}/" cnlm.conf
Finally, if you want to change the username and the password in the same command:
newUser="user123"
newPasswd="abcde"
sed -ri "s/^(Username *).*$/\1${newUser}/; s/^(Password *).*$/\1${newPasswd}/" cnlm.conf

Use shell to load in variables to replace placeholders

I have a problem where my config files contents are placed within my deployment script because they get their settings from my setting.sh file. This causes my deployment script to be very large a bloated.
I was wondering if it would be possible in bash to do something like this
setting.sh
USER="Tom"
log.conf
log=/$PLACEHOLDER_USER/full.log
deployment.sh
#!/bin/bash
# Pull in settings file
. ./settings.sh
# Link config to right location
ln -s /home/log.conf /home/logging/log.conf
# Write variables on top of placeholder variables in the file
for $PLACEHOLDER_* in /home/logging/log.conf
do
(Replace $PLACEHOLDER_<VARAIBLE> with $VARIABLE)
done
I want this to work for any variable found in the config file which starts with $placeholder_
This process would allow me to move a generic config file from my repository and then add the proper variables from my setting file on top of the placeholder variables in the config.
I'm stuck on how I can get this to actually work using my deployment.sh.
This small script will read all variable lines from settings.sh and replace the PLACEHOLDER_xxx in file for each. Does this help you?
while IFS== read variable value
do
sed -i "s/\$PLACEHOLDER_$variable/$value/g" file
done < settings.sh
#!/usr/local/env bash
set -x
ln -s /home/log.conf /home/logging/log.conf
while read user
do
usertmp=$(echo "${user}" | sed s'#USER=\"##' \
sed s'#"$##')
user="${usertemp}"
log="${user}"/full.log
done < setting.sh
I don't really understand the rest of what you're trying to do, I will confess, but this will hopefully give you the idea. Use read.

replace a line in linux file containing special characters

Here is an extract from a script showing the variables for the script
PathToPiconPNG="/var/OscamSrvidPicon/picon/19.2E/"
PathToOscamSrvid="/var/OscamSrvidPicon/picon/19.2E/oscam.srvid"
PathToPiconTPL="/var/OscamSrvidPicon/oscam_picons/"
PathToTmp="/tmp/"
I want to run this script numerous times replacing (for example) this line:
PathToPiconPNG="/var/OscamSrvidPicon/picon/19.2E/"
with this lines
PathToPiconPNG="/var/OscamSrvidPicon/picon/28.2E/"
I have tried using sed (I know this example is wrong but you might get what im trying to achieve)
sed 's/{PathToPiconPNG="/var/OscamSrvidPicon/picon/19.2E/"}/{PathToPiconPNG="/var/OscamSrvidPicon/picon/28.2E/"}/g' filename.txt > newfilenam.txt
If that is not possible, is there any way that I can set the variable externally from another script
sed -E 's/picon\/.+\//picon\/28.2E\//' filename.txt > newfilenam.txt

Bind/Named named.conf deleted/replaced accidently.. recovery help

Apologies my bad english.
Well, I accidentally replace named.conf ..
is there any way to see the configuration again ...
named continues running without restart
show me something like configurations running ....
Thanks to all
PS. i tried to recover the file but it was not erased.
Unless you have a backup of some kind you're going to be out of luck. Although you might get some vague details from rndc you'll never recover the actual file...
You may grep your disk for it.
I usually add the same comment to all the configuration files i touch. Something like
## (pablo) /etc/inputrc
...
## eof
If i delete /etc/inputrc i can grep for it with:
fgrep -a -A 50 '## (pablo) /etc/inputrc' /dev/sda >/some/file/outside/dev/sda
You may use the same technique looking for something you know is in the file, like some domain name you were serving.
-a force text search
-A 50 display 50 lines after the pattern matched
-B 50 display 50 lines before the pattern matched
-B is usefull if you don't know exactly where the pattern will match.

Resources