Labels

Android (1) bash (2) boost (2) C (34) C++ (2) cheatsheet (2) CLion (6) css (3) Debian (33) DL (17) Docker (1) Dreamweaver (2) Eclipse (3) fail2ban (4) git (5) GitHub (4) Hacking (3) html (8) http (1) iOS (1) iPad (1) IRC (1) Java (30) javascript (3) Linux (164) Mac (19) Machine Learning (1) mySQL (47) Netbeans (4) Networking (1) Nexus (1) OpenVMS (6) Oracle (1) Pandas (3) php (16) Postgresql (8) Python (9) raid (1) RedHat (14) Samba (2) Slackware (45) SQL (14) svn (1) tar (1) ThinkPad (1) Virtualbox (3) Visual Basic (1) Visual Studio (1) Windows (2)

Tuesday 20 July 2021

Brief note on sed, its use with variables and also changing variables inside the file which is being run.

Generally to use sed, we would do;

sed -i 's/old-text/new-text/g' file.txt

where file.txt is the file containing old-text and new-text.

If we want to ensure that our search term is at the beginning of the line, we add the ^ as follows;

sed -i 's/^old-text/new-text/g' file.txt

A problem I found though was that I wanted to use this inside of a bash file and use variables but found that the variable, example $TEMP_VARIABLE would not be expanded to what the actual variable was and to get around this, I had to use double quotes and escape any double quote that I wanted to be part of the search or replace term, so;


sed -i "s/^MASTER_IP=\".*/MASTER_IP=\"$TEMP_VARIABLE\"/g" $SQL_PATH$APP_NAME


where the search term was MASTER_IP="some text", here we used .* as a wild card so the search term was everything after the MASTER_IP="

so  MASTER_IP=\".*

the replacement term was "MASTER_IP="new text" and for this we used the $TEMP_VARIABLE and also escaped the first and last double quote.

so MASTER_IP=\"$TEMP_VARIABLE\"

The $SQL_PATH$APP_NAME are variables for the path/file which in this case was actually the same file containing the sed command. So we were essentially running a bash script to change it's own variables. This is particularly useful if we have a script that has custom variables for each server etc and yet we want to allow updating of the file with the most recent versions, as is the case with my script-buddy. Until now, I would have to download the latest version of the file and then modify any variables specific to the target server or whatever customisation was required. An extract of the functions from my sql wrapper are below;

function change_variable()
{
TEMP_VARIABLE=""
echo ""
echo "$1"
echo ""
read response
if [ ${#response} -ge 3 ]; then
TEMP_VARIABLE=$response
return 1
fi
}

function change_ip_and_ports()
{
echo ""
echo "###########################################"
echo "# CHANGING DEFAULT IP ADDRESSES AND PORTS #"
echo "###########################################"
echo ""
change_variable "ENTER NEW MASTER IP:"
if [ "$?" = 1 ]; then
sed -i "s/^MASTER_IP=\".*/MASTER_IP=\"$TEMP_VARIABLE\"/g" $SQL_PATH$APP_NAME
echo "MASTER IP UPDATED TO $MASTER_IP"
fi
echo ""
change_variable "ENTER NEW SLAVE IP:"
if [ "$?" = 1 ]; then
sed -i "s/^SLAVE_IP=\".*/SLAVE_IP=\"$TEMP_VARIABLE\"/g" $SQL_PATH$APP_NAME
echo "SLAVE IP UPDATED TO $SLAVE_IP"
fi
echo ""
change_variable "ENTER NEW MASTER PORT:"
if [ "$?" = 1 ]; then
sed -i "s/^MASTER_PORT=\".*/MASTER_PORT=\"$TEMP_VARIABLE\"/g" $SQL_PATH$APP_NAME
echo "MASTER PORT UPDATED TO $MASTER_PORT"
fi
echo ""
change_variable "ENTER NEW SLAVE PORT:"
if [ "$?" = 1 ]; then
sed -i "s/^SLAVE_PORT=\".*/SLAVE_PORT=\"$TEMP_VARIABLE\"/g" $SQL_PATH$APP_NAME
echo "SLAVE PORT UPDATED TO $SLAVE_PORT"
fi
echo ""
echo "###########################################"
echo "# IP ADDRESSES AND PORTS UPDATED #"
echo "###########################################"
echo ""
}

Assuming TEMP_VARIABLE has been declared earlier, calling the change_ip_and_ports function would in this case allow the changing of 4 variables within the file, MASTER_IP, SLAVE_IP, MASTER_PORT and SLAVE_PORT (again assuming they have been declaired earlier).


No comments:

Post a Comment

Note: only a member of this blog may post a comment.