0

I have a string Contain key=value format separated by # I am trying to replace the '=' char occurrences with ':' in the value of TITLE using BASH script.

"ID=21566#OS=Linux#TARGET_END=Synchronica#DEPENDENCY=Independent#AUTOMATION_OS=Linux#AUTOMATION_TOOL=JSystem#TITLE=Session tracking. "DL Started" Status Reported.Level=none"   

later on i am parsing this string to execute the eval operation

eval $(echo $test_line | sed 's/"//g' | tr '#' '\n' | tr ' ' '_' | sed 's/=/="/g' | sed 's/$/"/g')

When the sed 's/=/="/g' section will also change ..Level=none to Level="none This leads to

eval: line 52: unexpected EOF while looking for matching `"'

What will be right replace bash command to replace my string ?

1
  • Just remove the /g modifier from sed 's/=/="/g'. Commented Nov 3, 2019 at 18:56

3 Answers 3

1

As an alternative, consider pure-bash solution to bring the variables into bash, avoiding the (risky) eval.

IFS=# read -a kv <<<"ID=21566#OS=Linux#TARGET_END=Synchronica#..."

for kvp in "${kv[@]}" ; do
    declare "$kvp"
done
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion. Why the eval is risky?
I'm assuming the input comes from external system. This open your script to shell injection. e.g. , if the input will contain " ; rm -rf / (quotes, followed by any command) - it will execute that command - in the rm - it can wipe out part of your system.
0

I found the way to solve it.
I will add sed 's/=/:/8g' to my eval command. It will replace 8th to nth occurrences of '='.
The action will only effect the value of TITLE as expected.

 eval $(echo $test_line | sed 's/=/:/8g' | sed 's/"/"/g' | tr '#' '\n' | tr ' ' '_' | sed 's/=/="/g' | sed 's/$/"/g')

Comments

0

I did it like this :

echo '"ID=21566#OS:Linux#TARGET_END:Synchronica#DEPENDENCY:Independent#AUTOMATION_OS:Linux#AUTOMATION_TOOL:JSystem#TITLE:Session tracking. "DL Started" Status Reported.Level=none"' \
 | 
sed -E 's/(#)?([A-Z_]+)(=)/\1\2:/g'

Let me know if it works for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.