[英]Linux Bash Script : Comparison of strings in if with escape characters
[英]If statements with escape characters in Alpine Linux (ash busybox)
我有簡單的腳本來准備文件,它按預期工作。 所有代碼都在 docker image alpine:latest 執行(so ash busybox)
echo "credentials \"url.com\" {token = \"${VALUE}\"}" > ~/.terraformrc
稍后我想准備測試,為此我使用 if [[ ]] 語法。
if [[ $(cat ~/.terraformrc) != "credentials \"url.com\" {token =\"VALUE\" }" ]]; then exit 1; fi
在 Bash 上測試有效但在 ash/busybox/alpine 上我得到
sh: "url.com": unknown operand
同樣在 ash/busybox/alpine 上,當我 echo all 時,正確的是 escaping
echo "credentials \"url.com\" {token = \"VALUE\" }"
credentials "url.com" {token = "VALUE" }
我應該如何在 [ ] 或 [[ ]] 中轉義 "?謝謝幫助
[[ ]]
用於 bash,對於 ash 你應該使用 POSIX [ ]
代替:
#!/bin/sh
value=VALUE001
echo "credentials \"url.com\" {token = \"$value\"}" > ~/.terraformrc
if [ "$(cat ~/.terraformrc)" != "credentials \"url.com\" {token =\"VALUE001\" }" ]
then
exit 1
fi
順便說一句,你正確地做了 escaping 但你忘了雙引號$(cat ~/.terraformrc)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.