[英]Generate random string where it must have a special character in shell script
[英]Special character in string variable in shell script
PubkeyContent=$(cat $Pubkey)
read -r -d '' SSH_SCRIPT <<EOM
\$(grep ${PubkeyContent} ~/.ssh/authorized_keys")
EOM
我得到了錯誤的結果。
[Debug] SSH_SCRIPT = $(grep ssh-rsa AAAAB3NzaC1yc2EAAAzSb7WKnMsysGA8kuq5Ysp02Y75d5 sam ~/.ssh/authorized_keys")com
我想要這個結果
[Debug] SSH_SCRIPT = $(grep "ssh-rsa AAAAB3NzaC1yc2EAAAzSb7WKnMsysGA8kuq5Ysp02Y75d5 sam@com" ~/.ssh/authorized_keys")
一個簡單的解決方法是在 here-doc 中引用PubkeyContent
變量內容,即
read -r -d '' SSH_SCRIPT <<EOM
\$(grep "${PubkeyContent}" ~/.ssh/authorized_keys")
EOM
或者,如果您使用的是 bash/ksh/zsh shell,它支持一種使用輸入重定向< file
自動插入文件內容的方法,從而避免無用的使用cat
。 您可以將內部命令替換應用為
read -r -d '' SSH_SCRIPT <<EOM
\$(grep "$(<"$Pubkey")" ~/.ssh/authorized_keys")
EOM
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.