为什么我不能用 sed 变量替换字符串
why i can't replace string with variable with sed
echo -n "Username: "
read usernamert
echo -n "Password: "
read passwordt
echo -n "IP: "
read IPt
echo -n "Channel: "
read channelt
t1=$(echo "www://$usernamet:$passwordt@$IPt/TEST/SOMETHING?channel=$channelt1&TRUE=1")
sed -i "s+"t_1", "t_2"+"$t1", "AAA"+g" test.txt
link 应该是这样的
www://用户名:密码@$1.1.1.1/TEST/SOMETHING?channel=2&TRUE=1
所以这种 link 我想用文件 test.txt
中的字符串进行更改
"t_1", "t_2"
但总是出错。我错在哪里?我还尝试将变量放在 t1 变量内的 "" 中,但没有帮助。
您的双引号用于分隔 shell 字符串,而不是在 sed
模式和替换中按字面意义使用。
在 sed
操作两边加上单引号,变量除外。
sed -i 's+"t_1", "t_2"+"'"$t1"'", "AAA"+g' test.txt
echo -n "Username: "
read usernamert
echo -n "Password: "
read passwordt
echo -n "IP: "
read IPt
echo -n "Channel: "
read channelt
t1=$(echo "www://$usernamet:$passwordt@$IPt/TEST/SOMETHING?channel=$channelt1&TRUE=1")
sed -i "s+"t_1", "t_2"+"$t1", "AAA"+g" test.txt
link 应该是这样的 www://用户名:密码@$1.1.1.1/TEST/SOMETHING?channel=2&TRUE=1
所以这种 link 我想用文件 test.txt
中的字符串进行更改"t_1", "t_2"
但总是出错。我错在哪里?我还尝试将变量放在 t1 变量内的 "" 中,但没有帮助。
您的双引号用于分隔 shell 字符串,而不是在 sed
模式和替换中按字面意义使用。
在 sed
操作两边加上单引号,变量除外。
sed -i 's+"t_1", "t_2"+"'"$t1"'", "AAA"+g' test.txt