Im testing my code for automation of the installation of a software
In bashrc file below:
# User specific aliases and functions
export JAVA_HOME=/opt/jdk-9.0.1
export JRE_HOME=/opt/jdk-9.0.1/jre
export SCALA_HOME=/opt/scala-2.13.0
export PATH=$PATH:/opt/jdk-9.0.1/bin:/opt/jdk-9.0.1/jre/bin
Here im trying to add $SCALA_HOME/bin to PATH.
this is the required output:
`export PATH=$PATH:/opt/jdk-9.0.1/bin:/opt/jdk-9.0.1/jre/bin:/opt/scala-2.13.0`
`sed -i '1n;/^export PATH/i\export SCALA_HOME=/opt/scala-2.13.0' .bashrc`
the above code worked to append SCALA_HOME above path but for appending in the same line im not able to do
`sed -i "s/\"export PATH\":.*,$/\"export PATH\": \":$SCALA_HOME/bin\",/g" .bashrc
sed: -e expression #1, char 40: unknown option to `s'`
please help me get the correct sed command to append SCALA_HOME in the PATH
You can use this:
sed '/export PATH/ s/$/:\$SCALA_HOME\/bin/' .bashrc
's/\(export PATH=.*\)/\1:\$SCALA_HOME\/bin/'
To go through your expression:
s/\"export PATH\":.*,$/\"export PATH\": \":$SCALA_HOME/bin\",/g"
The \"export will look for "export in your file. Why do you expect a double quote before the export? It isn't there in the example. Likewise, PATH\": in the pattern will look for PATH": in the file. That double quote isn't there either. Your ,$ at the end of your pattern will also prevent it from matching anywhere.
Related
i want to run a sed command with programatically with changing parameters.
the thing is that i cant find the correct syntax to do so.
i want to configure a conf file with this and
change a dir path to another.
i'm currently using:
RESULT=$("sed 's/--ROOT_DIR--/${root_inst_dir}/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf > ${SOURCE_DIR}/${tool_name}.conf")
and i get the error message:
./change_tst.sh: line 7: sed 's/--ROOT_DIR--//home/test_dir/g' /home/tst/conf.conf > /home/script_tst/conf.conf: No such file or directory
the ">" is not working for some reason.
what am i doing wrong? or what is the best way to do this ?
UPDATE
i drooped the result variable and now running this:
(sed 's/--ROOT_DIR--/$root_inst_dir/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf) > ${SOURCE_DIR}/${tool_name}.conf
the new file is being created in > ${SOURCE_DIR}/${tool_name}.conf,
but the search/replace is happening literally and not as a variable...
thanks.
Putting " inside parenthesis will result in bash wanting to execute a command named exactly:
sed 's/--ROOT_DIR--/${root_inst_dir}/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf > ${SOURCE_DIR}/${tool_name}.conf"
Such command does not exist on your system.
Probably you intended to put " outside $(...):
RESULT="$(sed 's/--ROOT_DIR--/${root_inst_dir}/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf > ${SOURCE_DIR}/${tool_name}.conf)"
Better way, if you don't need the RESULT variable and if you want to properly escape root_inst_dir variable:
sed 's#--ROOT_DIR--#'"${root_inst_dir}"'#g' "${root_inst_dir}/${tool_name}/etc/${tool_name}.conf" > "${SOURCE_DIR}/${tool_name}.conf"
Or if you need RESULT variable:
sed 's#--ROOT_DIR--#'"${root_inst_dir}"'#g' "${root_inst_dir}/${tool_name}/etc/${tool_name}.conf" > "${SOURCE_DIR}/${tool_name}.conf"
RESULT=$(cat ${SOURCE_DIR}/${tool_name}.conf)
I am trying replace a line with another line and add new line below that using shell script. My file content looks like below. I want to replace export PATH with JAVA_HOME=/usr/lib/jvm/java-1.8.0-ibm-1.8.0.2.10-1jpp.1.el7.x86_64
export PATH JAVA_HOME
before:
export PATH
It should be after:
JAVA_HOME=/usr/lib/jvm/java-1.8.0-ibm-1.8.0.2.10-1jpp.1.el7.x86_64
export PATH JAVA_HOME
That means, it has to replace replace export PATH with these two lines.
Please help me how can I do with sed or shell script
Thanks,
Kumar.
sed -e 's#^export PATH$#JAVA_HOME=/usr/lib/jvm/java-1.8.0-ibm-1.8.0.2.10-1jpp.1.el7.x86_64\n\nexport PATH JAVA_HOME#' <yourfile.txt
This is just a single substitute command in sed.
Some notes:
The # is used after s instead of the usual / to avoid having to quote all slashes in the path. Otherwise you have to quote each / in the path this way: /. ow you only have to quote #.
The escape sequence \n insert a newline.
Add the -i parameter to sed if you want to actually update the file. The code above only prints the new file to stdout.
The regex is anchored (^...$) so that it matches the whole line and not just a part of it.
What I'm trying to accomplish is having a central configuration file, in bash, that defines some variables that are re-used in different bash files. The example below attempts to generate a file name with the current date included in the file name as well as a variable defined in another shell script. However whenever I try to concatenate this external variable it doesn't work. I can concatenate the variable in any other situation.
Example Code:
../config/vars.sh
#!/bin/bash
mysqlUser="backupuser"
mysqlPwd="fakePwd"
mysqlSocket="/var/run/mysqld/mysqld.sock"
mysqlPort="3306"
serverName="s01.catchyservername.com"
./dbBackup.sh
#!/bin/bash
source ../config/vars.sh
tempName=$(date +"%Y%m%d.sql.gz")
fileName="mysqld_${mysqlPort}_${tempName}"
echo "mysqld_${mysqlPort}"
echo ${tempName}
echo ${fileName}
output of dbBackup.sh
mysqld_3306
20140926.sql.gz
_20140926.sql.gz
As you can see when echoing "mysqld_${mysqlPort}" I get the expected output, but when echoing ${fileName} the entire first half of the string is ignored. What am I misunderstanding?
Your vars.sh file was probably created with a DOS/windows text editor:
$ ./dbBackup.sh
mysqld_3306
20140926.sql.gz
_20140926.sql.gz
$ dos2unix vars.sh
dos2unix: converting file vars.sh to Unix format ...
$
$ ./dbBackup.sh
mysqld_3306
20140926.sql.gz
mysqld_3306_20140926.sql.gz
$
As you can see above, I use the dos2unix utility to convert the line separators to Unix style.
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.
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