I want to substitute a value for a variable in a file with Bash.

Asked 2 years ago, Updated 2 years ago, 91 views

I want to substitute the variable I set in the command to the variable in the file, but I'm having trouble because I don't know how to substitute it.
Isn't there a way to substitute the value outside the file for the variable in the file?
Run $bash./test.sh and
I'd like to get a result where sample.json is "green":"123".

sample.json

{
    "red": "aaaaa",
    "green": "$ver",
    "yellow": "cccccc",
    "blue": "dddddd"
}

test.sh

#!/bin/bash
ver="123"
cat docs/template.json

Directory Configuration ↓

--sample.json
--test.sh

The echo command allows you to view variables within test.sh, so
I tried rewriting test.sh to see if I could use the cat command to output variables.
In the first place, I am troubled because I cannot output variables.
test.sh

#!/bin/bash
ver="1234"
echo$ver
# cat docs/template.json
cat<<EOS>sample1.json
$ver
EOS

Sample1.json Obtained



bash shellscript

2022-09-30 21:45

3 Answers

Why don't you use envsubst?Expand the environment variables.

cat sample.json | ver=123envsubst 

Reference

  • ver=123 describes what you are doing


2022-09-30 21:45

The jq command, which is well known as a JSON parser, has the option to treat JSON in the form shown in the questionnaire as a template: Using jq as a template engine.

$cat sample.json
{
    "red": "aaaaa",
    "green"—$ver,
    "yellow": "cccccc",
    "blue": "dddddd"
}
$ cat test.sh
ver=123
jq-n --argjson ver"\"$ver\"-f sample.json>output.json
$ bash test.sh
$ cat output.json
{
  "red": "aaaaa",
  "green": "123",
  "yellow": "cccccc",
  "blue": "dddddd"
}

If the syntax of the template is simple, you can also use the sed command to handle it.

$cat sample.json
{
    "red": "aaaaa",
    "green": "$ver",
    "yellow": "cccccc",
    "blue": "dddddd"
}
$ cat test.sh
ver=123
sed"s/\$ver/$ver/g"sample.json>output.json
$ bash test.sh
$ cat output.json
{
    "red": "aaaaa",
    "green": "123",
    "yellow": "cccccc",
    "blue": "dddddd"
}


2022-09-30 21:45

You can add the following one liner to the sh script and use extcat instead of cat to get the results you requested.

I also added the sh scripted version.This one is easier to use.

One Liner

function extcat() {cat$@|bash<<"cat<<"\"$(cat)"\"";};export-fextcat

Note
 In addition to expanding environment variables.Command replacement is also done, so be careful when using it.

If you don't mind, please use it.

Simple Template Engine extcat coneta #1

test.sh

#!/bin/bash
function extcat() {cat$@|bash<<"cat<<"cat<"\"$(cat)"\"";};export-fxtcat
ver="123" extcat docs/template.json>sample.json

Or

#!/bin/bash
function extcat() {cat$@|bash<<"cat<<"cat<"\"$(cat)"\"";};export-fxtcat
export ver="123"
extcat docs/template.json>sample.json

sample.json

{
    "red": "aaaaa",
    "green": "123",
    "yellow": "cccccc",
    "blue": "dddddd"
}

docs/template.json

You must include " at the beginning of the template, " at the end, and $ver around '.

"'{
    "red": "aaaaa",
    "green": "'$ver',
    "yellow": "cccccc",
    "blue": "dddddd"
}'"

Template variable replacement uses the bash feature.
Because the template is evaluated after being enclosed in double quotation marks ", if you use " in the template, you must change the quotation to single quotation marks ' and resolve the quotation before and after ${variable name}.

Another way is to escape " with \.

{
    \"red\":\"aaaa\",
    \"green\":\"$ver\",
    \"yellow\":\"cccccc\",
    \"blue\":\"dddddd\"
}

Directory Configuration

--test.sh
--docs/template.json
--sample.json

This is the sh scripted version of extcat.
There is no need to escape double quotation marks " in the template.

extcat.sh

#!/bin/sh
 cat<<^D|sh
 cat<^D2
`cat$@`
^D2
^D

^D is supposed to be a control character.Copy paste and control character ^D moves even if it becomes normal string^D

How-to

ver="123"./extcat.sh docs/template.json>sample.json

Results (sample.json)

{
    "red": "aaaaa",
    "green": "123",
    "yellow": "cccccc",
    "blue": "dddddd"
}

Templates (docs/template.json)

{
    "red": "aaaaa",
    "green": "$ver",
    "yellow": "cccccc",
    "blue": "dddddd"
}


2022-09-30 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.