I'm a beginner at shell scripting and I'm studying.Thank you for your cooperation.
The following configuration files are available:
valueA="aaaaaaa"
valueB="[@$]"
of the above valueB value
"[@$]"
followed by
"[@$]"/xxx/'/xxx/'/xxx/'....
You can add any directory path while separating it with spaces like
I would like to create a shell script.
Create a list of /xxx/
parts in the list file
include.lst.
I tried to read it in the while statement and execute it, but
sed didn't work as expected.
Here is an example of the contents of include.lst.
/home/aaa/
/usr/bbb/
.
.
.
This is a failure example of a failure.
<Failure Example 1>
while read line
do
sed-i-e "/valueB/s/$/$line/g"
done<include.lst
==============================================
<Failure Example 2>
while read line
do
sed-i-e "|valueB|s|$|$line|g"
done<include.lst
==============================================
If this is not a variable such as $line
, sed itself succeeds.
Example)
sed-i-e "/valueB/s/$/\'\/var\/\'/g"
Also,
sed-i-e "s|xxx|$line|g"
If you do not include the search string first as shown in , this will also succeed.
If anyone knows what to do in such a case, could you please let me know?
I am not particular about while statements or seds.
Thank you for your cooperation.
Assume that both ends of the path are enclosed by '
.
However, if '
is inside the path, escape is required, so this time we will replace it with '"
for shell escape.
#include.lst
/home/aaa/
/usr/bbb/
/tmp /'a/
↓
# config.txt
valueB="[@$]"'/home/aaa/'/usr/bbb/'/tmp/'"'"a/'
The entire script is as follows:
$qq="'"
$ value_b_paths=$(sed'-e:loop;${;s/'$qq'/'$qq'"$qq'/g;s/^/'$q'/;s/$/'$q'/;s/\n/'$qq'''$q'/g;};N;bloop'include.lst)
$ sed-i/valueB/a\\"$value_b_paths"config.txt&sed-i'-e/valueB/{;N;s/\n/;}'config.txt
Create a string value_b_paths
to be added to the end of the valueB
in the second line.
Load include.lst
in full line at once and replace the quotation marks
Update the file by adding the path to the next line of valueB
in the first line of the third line, and update the file again in the second line.
Avoid the trouble of properly escaping patterns containing special characters.
The second line looks complicated because of the complexity of escape, but it becomes a simple sed script.
:loop
${
s/'/'"'/g
s/^/'/
US>%s /$/'/
US>%s/\n/'/g
}
N
bloop
#!/bin/sh
convert_include_list(){
while read-r line
do
printf "'%s'" "$line"
done
echo
}
append_str =$(
cat include.lst |
convert_include_list|
sed's /$//')
cat config.txt |
append_str="${append_str}"awk'
{ if($0~/^valueB/)
print$0""ENVIRON ["append_str" ]
else
print$0
}
'
valueA="aaaaaaa"
valueB="[@$]"'/home/aaa/'/usr/bbb/'/foo/ccc/'
If I were myself, I would do the above.The point is:
Scripting from above:
append_str="${append_str}"
). In awk, if a line matches a regular expression, take append_str
out of the ENVIRON
variable and add it to the end.Because the above script results in the standard output, if you want to replace an existing file, redirect it to a different location and then mv it.
The $line is deployed by the shell and then passed to sed, so if the $line contains /, it becomes unknown option to 's'.You have almost reached the correct answer, but you will find sed-i-e"/valueB/s|$|$line|g"config.txt.However, if the $line contains characters|, it will still be an error (although it is unlikely)
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
915 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
578 Understanding How to Configure Google API Key
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.