I want to add it to any end of the line in the shell script.

Asked 2 years ago, Updated 2 years ago, 56 views

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.

shellscript sh

2022-09-29 22:32

3 Answers

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


2022-09-29 22:32

test.sh

#!/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
        }
    '

Example Results (Standard Output)

valueA="aaaaaaa"
valueB="[@$]"'/home/aaa/'/usr/bbb/'/foo/ccc/'

If I were myself, I would do the above.The point is:

  • Shell script processing is basic line oriented, so you create the content of the line you want to add when you load it from include.lst.
  • sed uses awk because it is difficult to treat strings as variables (which are assumed to be control characters).
  • The most stable way to pass shell variables to awk is to pass them via ENVIRON as described above. (Reference: https://stackoverflow.com/a/26779845/3090068)

Scripting from above:

    <li>convert_include_list: The contents of include.lst are read from the standard output, and finally contents to be added are outputted to the standard output. While.
  • append_str:Convert_include_list the contents of include.lst by replacing the command, and then delete the last space and keep it in this variable.
  • Around the last awk: Execute the awk command as an append_str environment variable.At that time, the variable can be input as it is by enclosing it with a double quote (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.


2022-09-29 22:32

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)


2022-09-29 22:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.