0

i'm trying to insert variables before specific line in my script. this is the code that i'm using:

var1=$(echo "database1=")
var2=$(echo "database2=")
var3=$(echo "database3=")

sed -i "/#variables/i \
$var1\
$var2\
$var3" /data1/create_database 

i expect that create_database be like this after i run above command:

database1=
database2=
database3=
#variables 

but i get this result:

database1= database2= database3=
#variables

tried few ways nothing worked. what should i do?

0

3 Answers 3

1

You don't need command substitutions to set your variables:

var1="database1="
var2="database2="
var3="database3="

I think you need single quotes to add newlines, but it looks quite ugly if you quote your variables with double quotes. The double quotes are needed if your variables contain space characters:

sed -i '/#variables/i\
'"$var1"'\
'"$var2"'\
'"$var3" /data1/create_database

With GNU sed you could simply add a few newlines:

sed -i "/#variables/i$var1\n$var2\n$var3" /data1/create_database
1
  • of course i dont need command substitutions. in main script they are out put of command, forgot to fix it. btw, i got an error: sed: couldn't open temporary file /data1/create_database/sedRtx3Di: Permission denied. changed the folder permission and error is gone now. Commented Jul 28, 2019 at 8:05
1

You can use an escape sequence (\n) to insert newlines. Note that

sed -i "/#variables/i \
$var1\n$var2\n$var3" /data1/create_database

is equivalent to

sed -i "/#variables/i $var1\n$var2\n$var3" /data1/create_database

so you'd need a double backslash

sed -i "/#variables/i\\
$var1\n$var2\n$var3" /data1/create_database

Perhaps also consider multiple expressions to make it a single line command, where each expression is effectively a line.

sed -i -e '/#variables/i\' -e "$var1\n$var2\n$var3" /data1/create_database

And finally you might need to escape your variables

var1=$(echo "database1=" | sed 's/[]\/$*.^&[]/\\&/g')
var2=$(echo "database2=" | sed 's/[]\/$*.^&[]/\\&/g')
var3=$(echo "database3=" | sed 's/[]\/$*.^&[]/\\&/g')

sed -i -e '/#variables/i\' -e "$var1\n$var2\n$var3" /data1/create_database
1

You need to double the number of backslashes since you are operating under double quotes:

sed -i "/#variables/i \\
$var1\\
$var2\\
$var3" /data1/create_database

And if you have better things to do than count those darned backslashes, you could allow sed to take care of the details for you, but it's gonna cost you in terms of code:

sed -i "/#variables/{H;s/.*//;x;}
s/\\n/$var1&$var2&$var3&/
" /data1/create_database

Or this where no mention of a newline is made:

sed -i "/#variables/{
           h
           s/.*/$var1/p
           s/.*/$var2/p
           s/.*/$var3/p
           g
}" /data1/create_database

Of course care has to be exercised while planting the shell variables inside sed code. They have to be neutered first and then used. Fo example, what if one if your shell vars had a slash '/' in it?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .