我写过一个shell脚本,它读取文件并获取执行特定任务所需的文件路径,但如果文件路径包含任何特殊字符没有服用完整的路径如何在shell中获取特殊字符作为普通字符串
EG的路径\网络\搭\ AB 我得到的值是\ WEB AKE \ AB这治疗\ T作为标签和打印标签那么如何避免这种
我的代码 grepresult = grep "Cannot " input.txt | cut -f 2,3 -d":"
回声$ grepresult
所以,请帮助我怎样才能得到的特殊字符转换成字符串
我写过一个shell脚本,它读取文件并获取执行特定任务所需的文件路径,但如果文件路径包含任何特殊字符没有服用完整的路径如何在shell中获取特殊字符作为普通字符串
EG的路径\网络\搭\ AB 我得到的值是\ WEB AKE \ AB这治疗\ T作为标签和打印标签那么如何避免这种
我的代码 grepresult = grep "Cannot " input.txt | cut -f 2,3 -d":"
回声$ grepresult
所以,请帮助我怎样才能得到的特殊字符转换成字符串
在POSIX兼容的外壳,有一些例外,反斜线的解释是默认启用。
的问题,因为你知道的是,shell解释\
以其特殊的meaning.To在输出反斜线,只需反斜线它。
输入
$ grepresult="sometestt\n\b\a"
脚本
echo "${grepresult//\\/\\\\}"
#since \ has special meaning here as well, `\\` makes the character `\`
应该做的伎俩。在Shell Parameter Substitution中查找${var//Pattern/Replacement}
。
输出
$ echo "${grepresult//\\/\\\\}"
sometestt\n\b\a
特殊字符存储在正确的变量grepresult
,它是echo
谁转换\t
到TAB
尝试:
echo -E $grepresult
其中-E
防止echo
从做反斜杠