我需要从文件中缓存一个字符串,以便稍后进行grep搜索。该字符串在括号前有一个斜杠。在抓取字符串并在外壳中回显之后,它的行为便与我期望的一样。但在我的脚本中,它会删除斜线。
下面有很多文本,但是字符串中最重要的部分是“(remainingTime)”]
文件中的行:
"session_will_expire_message" = "User session will expire in \(remainingTime). Please sign in again to avoid expiration in the middle of the transaction.";
Shell命令(按预期运行:)>
% line="\"User session will expire in \(remainingTime). Please sign in again to avoid expiration in the middle of the transaction.\";" % echo $line "User session will expire in \(remainingTime). Please sign in again to avoid expiration in the middle of the transaction."; % echo -E - $line "User session will expire in \(remainingTime). Please sign in again to avoid expiration in the middle of the transaction."; % val="$(echo -E - $line | cut -d";" -f1 | cut -d"=" -f2-)" % echo $val "User session will expire in \(remainingTime). Please sign in again to avoid expiration in the middle of the transaction." % echo -E - "${val}" "User session will expire in \(remainingTime). Please sign in again to avoid expiration in the middle of the transaction."
脚本(无法正常工作):
while read line; do if [[ $(echo $line | grep -c "=") -gt 0 ]]; then key="$(echo $line | cut -d";" -f1 | cut -d"=" -f1 | tr -d " ")" val="$(echo -E - $line | cut -d";" -f1 | cut -d"=" -f2-)" /* !!--> */ [[ $IS_VERBOSE == "TRUE" ]] && echo "key: $key" && echo -E - "value: ${val}" /* !!--> */ [[ $key != "" && "${val}" != "" ]] && LOCALIZED_PAIR[$key]="${val}" fi done < $LOCALIZED_DICT
标有“ !!->”的行将输出到shell和没有斜杠的文件中。
键:“ session_will_expire_message”
值:“用户会话将在(remainingTime)中到期。请重新登录以避免交易中间到期。”
为什么我的脚本工作方式不同,如何解决?
编辑:
似乎斜线正被while循环剥夺:
% while read l; do echo $l; done < ~/Desktop/tmpOutput "User session will expire in (remainingTime). Please sign in again to avoid expiration in the middle of the transaction.";
如果while循环读取行时斜线被分割,有没有办法避免这种情况?
我需要从文件中缓存一个字符串,以便稍后进行grep搜索。该字符串在括号前有一个斜杠。在抓取字符串并在外壳中回显之后,它的行为便与我期望的一样。但是在我的...
您有很多不必要的额外过程。