Seeking Help with a Shell Issue

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 请教1个shell 问题

| username: Raymond

[TiDB Usage Environment] Virtual machine environment on personal computer
[TiDB Version]
[Reproduction Path] What operations were performed when the issue occurred
[Encountered Issue: Issue Phenomenon and Impact]
[Resource Configuration]
Dear teachers, while practicing shell scripts in the virtual machine environment on my personal computer, I encountered an issue. When extracting a quoted string from a log, although the string can be extracted, it seems that the quote characters are being escaped during execution. I am not sure why this is happening. Since this string is later used as a password to connect to the database, it results in an error, hence my query.
The effect is as follows:

  1. Could you please advise on how to extract this string directly without the single quotes, just the content inside the quotes?

For the above two points, I kindly ask for your guidance. Thank you.

| username: dba-kit | Original post link

Just execute using the variable ${tempstr}. The bash -x is for demonstration purposes, hence the escaping.

| username: dba-kit | Original post link

The main issue with your problem is that the output test.log is incorrect. If you can’t ensure the content within the password, it’s best not to enclose it in single quotes. Use invisible characters like \xa0 or \u3000 instead.

| username: Raymond | Original post link

Is there a way to remove the quotes when retrieving characters, for example, changing '81gC2d34m^S-M+v7p’ to 81gC2d34m^S-M+v7p?

| username: Raymond | Original post link

When I later used this password to connect to the database, the password became ‘’'‘81g’'‘C2d*34m^S-M+v7p’'‘’. It essentially changed the password directly.

| username: Raymond | Original post link

For question 2, I found a method.
Assuming tempstr= '81gC2d34m^S-M+v7p’
tempstr=${tempstr#'}
tempstr=${tempstr%'
}
echo $tempstr
Finally, the single quotes can be removed.
This works.
I wonder if the teachers have any other methods.

| username: tidb菜鸟一只 | Original post link

tempstr=grep -i 'str' test.log | awk -F "'" '{print $2"'\''"$3}'
Like this

| username: 昵称想不起来了 | Original post link

grep -i str test1.log | awk ‘{print $NF}’ | awk -F ‘.’ ‘{print $1}’ | sed “s/^‘//;s/’$//”

| username: dba-kit | Original post link

Assuming tempstr = '81gC2d34m^S-M+v7p’

You are assuming that the password does not contain ', but looking at your example, the password already includes punctuation marks. If the password contains a single quote, your assumption will be incorrect.

| username: dba-kit | Original post link

Otherwise, after generating the password, you can add a ... | tr -d "'" to remove the single quotes.

| username: redgame | Original post link

str=$(echo “$str” | sed ‘s/’'‘//g’)