I want to treat the comment symbol `<##>` in the shell as a string

Asked 2 years ago, Updated 2 years ago, 306 views

I want to replace the <#CLIENT_KEY#> string in the source code with 12345789 in the shell script, but I got an error and I can't run it.

./ci_post_clone.sh:13:parse error near`\n'

What should I do if <#> is probably an error due to the comment format?

#!/bin/zsh

#  ci_post_clone.sh

env_file_path="./Source.c"

typeset-AenvValues

CLIENT_KEY=12345789
CLIENT_SECRET_KEY=987654321

envValues [<#CLIENT_KEY#>] = $CLIENT_KEY
envValues [<#CLIENT_SECRET_KEY#>] = $CLIENT_SECRET_KEY

for key in ${(k)envValues}
  sed-i-e "s/${key}/${envValues[$key]}/g "${env_file_path}"

zsh shell

2022-10-28 10:16

2 Answers

Although it has already been resolved, you can also use the -f option of the sed command.

#!/bin/sh

env_file_path='./Source.c'

cat<<EOT | sed-i-E-f - "${env_file_path}"
s/<#CLIENT_KEY#>/12345789/g
s/<#CLIENT_SECRET_KEY#>/987654321/g
EOT


2022-10-28 10:16

I think it will work if you replace the replacement string with a variable and refer to the variable.
You must quote ' when substituting.

#!/bin/zsh
#  ci_post_clone.sh
env_file_path="./Source.c"
typeset-AenvValues
CLIENT_KEY=12345789
CLIENT_SECRET_KEY=987654321
r1 = '<#CLIENT_KEY#>'
r2 = '<#CLIENT_SECRET_KEY#>'
envValues [$r1] = $CLIENT_KEY
envValues [$r2] = $CLIENT_SECRET_KEY

for key in ${(k)envValues}
  sed-i-e "s/${key}/${envValues[$key]}/g "${env_file_path}"


2022-10-28 10:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.