bash, adding line after specific line - linux

For my actual creating intial server setup script i need to add a line thats saved in a variable after a specific line in a specific file.
I want to add the line:
zend_extension = $phpextensiondir/ioncube_loader_lin_$phpextensionnumber.so
after the following line:
;realpath_cache_ttl = 120
in the following files:
/etc/php/$phpversionnumber/cli/php.ini
/etc/php/$phpversionnumber/cli/fpm.ini
I was looking around, but can't find any what I understand. Actually I'm new to bash scripting.
Can anybody explain? It seems that sed is not the right choice for it?

awk '{print} $0=="old line"{print "new line"}' file

Related

sh shell redirecting a subshell to file, can't find the right syntax

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)

Assign new variable from each line of a text file

What I'm basically trying to do is automatically detect if there is text in a line, and if so create a new variable containing the text in said line , within a script. If there is no text in a line then the variable doesn't get created. I can do this manually by opening the file -
$ cat file.txt
sometxt
somemoretext
evenmoretext
...
then adding to my script the appropriate lines -
TXT=file.txt
VAR1=$(sed -n 1p $TXT)
VAR2=$(sed -n 2p $TXT)
...
but this is a pain since I have to count how many lines there are total, then copy and paste each line assigning the variables and changing 'VAR!' to 'VAR2' and '1p' to '2p'. There has to be an easier way. Thanks
#JNevil thanks for pointing me in the right direction.
Heres what ended up working for me -
for var_name in (cat links.txt); do
wget <servername.com>$var_name
done
Still dont know how to use curl but this worked fine!

Edit conf file linux under specific section

I have a file that I want to change from command line. The thing is that it has sections and in some different sections it has the same values that I need to change.
The file looks like:
...
[DEFAULT]
findtime = 600
maxretry = 3
[ssh]
maxretry = 6
And I want to change only the maxretry under [DEFAULT].
Maybe there is a conifuration command line that searches the section in config file and changes value named X ?
The command I wrote with sed changes all occurences and I want only the first occurence after [DEFAULT]
sudo sed -i "s/\(^maxretry =.*$\)/maxretry = ${NUMBER_OF_RETRIES}/" filename
Appreciate your help.
Thanks
One way using awk:
$ NUMBER_OF_RETRIES=5
$ awk '/^\[DEFAULT\]/{f=1;}f && /^maxretry =/{print "maxretry = "x;f=0;next}1' x=$NUMBER_OF_RETRIES file
First, we search for the DEFAULT section, and once found, set a flag. Search for the line beginning with maxretry, and if flag is set, do the replacement.
You can check a default flag, which tells you if a substitution occurred or not:
awk '!f && /^maxretry =.*$/ {$0="maxretry = 666"; f=1} 1'
Note that this will not write the change in the file, just redirect the output to a new file or do smth similar. This approach is very specific and works only for first occurrence. #Guru's answer is more generic and can work for arbitrary section (e.g. you want to replace the 2nd,3rd occurrence depending on the section). Nevertheless this example is simpler when targeting the first occurrence.

Bugzilla + Command or Script to add text to parameter 'shutdownhtml' in param file

I am looking for One Line Command or a simple script to set the 'shutdownhtml' in /var/www/html/bugzilla/data/param file.
From...
'shutdownhtml' => '',
To....
'shutdownhtml' => 'Archiving DB and Bugzilla config... It could takes 5 min or longer.',
Did try to use "sed" but somehow the config above has Single Open quote ' , so seem like "sed" not able to do this... ??
Thanks for helping.
Thanks to Thorsten Schöning. A solution has provided by him.
sed --regexp-extended 's/(.)shutdownhtml\1(\s+)=>(\s+)\1{2},/\1shutdownhtml\1\2=>\3\1someText\1,/' params > params.shutdown
See the link below for more information
http://mozilla.6506.n7.nabble.com/Command-or-Script-to-add-text-to-parameter-shutdownhtml-in-param-file-td279974.html

insert line into a new file in linux using shell script

I need to write shell script to create a new file and insert some stuffs into it. The problem is, using sed 'i/blablabla' command, it only works when there is at least one line existed in the file. What is the command to insert into a new file?
Thanks
echo 'new line' >file_name
Also, you can append to the end without using sed using the >> operator:
echo 'additional line' >>file_name
More variants:
cat >>file_being_appended_to <<some_random_string
This is the first line
This is the second line and it has a variable in it; it's right here: $some_variable
some_random_string
cat >>file_being_appended_to <<'some_random_string'
This is the first line
This is the second line and it has a variable reference in it; it's right here: $some_variable,
but this variable reference is not expanded because the beginning delimiter string has single quotes around it.
some_random_string
Other variants:
echo 'This is the first line
This is the second line.' >file
some_variable="some value"
echo "This is the first line
This is the second line and it has a variable in it; it's right here: $some_variable" >file

Resources