我正在撰寫一個 Jenkins 作業,它將在遠程服務器上的兩個 chroot 目錄之間移動檔案。
這使用 Jenkins 多行字串變數來存盤一個或多個檔案名,每行一個。
以下將適用于沒有特殊字符或空格的檔案:
## Jenkins parameters
# accountAlias = "test"
# sftpDir = "/path/to/chrooted home"
# srcDir = "/path/to/get/files"
# destDir = "/path/to/put/files"
# fileName = "file names # multiline Jenkins shell parameter, one file name per
#!/bin/bash
ssh user@server << EOF
#!/bin/bash
printf "\nCopying following file(s) from "${accountAlias}"_old account to "${accountAlias}"_new account:\n"
# Exit if no filename is given so Rsync does not copy all files in src directory.
if [ -z "${fileName}" ]; then
printf "\n***** At least one filename is required! *****\n"
exit 1
else
# While reading each line of fileName
while IFS= read -r line; do
printf "\n/"${sftpDir}"/"${accountAlias}"_old/"${srcDir}"/"\${line}" -> /"${sftpDir}"/"${accountAlias}"_new/"${destDir}"/"\${line}"\n"
# Rsync the files from old account to new account
# -v | verbose
# -c | replace existing files based on checksum, not timestamp or size
# -r | recursively copy
# -t | preserve timestamps
# -h | human readable file sizes
# -P | resume incomplete files show progress bars for large files
# -s | Sends file names without interpreting special chars
sudo rsync -vcrthPs /"${sftpDir}"/"${accountAlias}"_old/"${srcDir}"/"\${line}" /"${sftpDir}"/"${accountAlias}"_new/"${destDir}"/"\${line}"
done <<< "${fileName}"
fi
printf "\nEnsuring all new files are owned by the "${accountAlias}"_new account:\n"
sudo chown -vR "${accountAlias}"_new:"${accountAlias}"_new /"${sftpDir}"/"${accountAlias}"_new/"${destDir}"
EOF
使用檔案名“ sudo bash -c 'echo "hello" > f.txt'.txt”作為測驗,我的腳本在檔案名中的“sudo”之后會失敗。
我相信我的問題是我的 $line 變數沒有正確參考或轉義,導致 bash 不將 $line 值視為一個字串。
我嘗試過單引號或使用 awk/sed 在變數字串中插入反斜杠,但這沒有用。
我的理論是我遇到了特殊字符和 heredocs 的問題。
uj5u.com熱心網友回復:
盡管從您的描述中我不清楚您到底遇到了什么錯誤或在哪里遇到了錯誤,但您在所提供的腳本中確實存在一些問題。
主要的可能只是sudo您嘗試在遠程端執行的命令。除非user具有無密碼sudo權限(相當危險)sudo,否則將提示輸入密碼并嘗試從用戶終端讀取密碼。您沒有提供密碼。如果實際上您收集了它,您可能只是將其插入命令流(在此處的檔案中)。盡管如此,這仍然存在一個潛在問題,因為您可能執行許多 sudo 命令,并且根據遠程 sudo 配置和 sudo 命令之間的時間,它們可能會或可能不會請求密碼。最好的辦法是構造命令流,以便只sudo需要一次執行。
其他注意事項如下。
## Jenkins parameters # accountAlias = "test" # sftpDir = "/path/to/chrooted home" # srcDir = "/path/to/get/files" # destDir = "/path/to/put/files" # fileName = "file names # multiline Jenkins shell parameter, one file name per #!/bin/bash
沒有腳本的#!/bin/bash第一行,因此它不能用作 shebang 行。相反,它只是一個普通的評論。因此,當腳本直接執行時,它可能會或可能不會bash運行它,如果是bash,它可能會或可能不會在 POSIX 兼容模式下運行。
ssh user@server << EOF #!/bin/bash
這#!/bin/bash也不是 shebang 行,因為這僅適用于從常規檔案讀取的腳本。結果,以下命令由user的默認 shell 運行,無論發生什么。如果您想確保其余部分由 運行bash,那么也許您應該bash顯式執行。
printf "\nCopying following file(s) from "${accountAlias}"_old account to "${accountAlias}"_new account:\n"
$accountAlias(通過本地 shell)的兩個擴展導致在遠程 shell 中傳遞給未參考的文本。printf您可以考慮只洗掉反引號,但這仍然會使您容易受到accountAlias包含雙引號字符的惡意值的影響。請記住,在通過網路發送命令之前,這些將在本地擴展,然后資料將由遠程 shell 處理,遠程 shell 將解釋參考。
這可以通過
在heredoc之外,準備一個可以安全地呈現給遠程shell的帳戶別名版本
accountAlias_safe=$(printf %q "$accountAlias")和
在 heredoc 內部,將其擴展為未參考。我還建議將其作為單獨的引數傳遞,而不是將其插入到更大的字串中。
printf "\nCopying following file(s) from %s_old account to %s_new account:\n" ${accountAlias_safe} ${accountAlias_safe}
類似的情況適用于大多數其他地方,其中來自本地 shell 的變數被插入到 heredoc 中。
這里 ...
# Exit if no filename is given so Rsync does not copy all files in src directory. if [ -z "${fileName}" ]; then
...您為什么要在遠程端執行此測驗?您可以通過在本地執行它來節省一些麻煩。
這里 ...
printf "\n/"${sftpDir}"/"${accountAlias}"_old/"${srcDir}"/"\${line}" -> /"${sftpDir}"/"${accountAlias}"_new/"${destDir}"/"\${line}"\n"
...遠程shell變數$line在printf命令中不加引號使用。應該參考它的外觀。此外,由于您每次使用源名稱和目標名稱兩次,因此將它們放入(遠程端)變數中會更加簡潔明了。并且,如果目錄名稱具有腳本注釋中顯示的形式,那么您正在引入多余的/字符(盡管這些可能無害)。
對你有好處,記錄所有使用的rsync選項的含義,但你為什么要通過電線將所有這些發送到遠程端?
此外,您可能希望包含 rsync 的-p選項以保留相同的權限。可能您也想包含該-l選項,將任何符號鏈接復制為符號鏈接。
把所有這些放在一起,更像這樣(未經測驗)的東西可能是有序的:
#!/bin/bash
## Jenkins parameters
# accountAlias = "test"
# sftpDir = "/path/to/chrooted home"
# srcDir = "/path/to/get/files"
# destDir = "/path/to/put/files"
# fileName = "file names # multiline Jenkins shell parameter, one file name per
# Exit if no filename is given so Rsync does not copy all files in src directory.
if [ -z "${fileName}" ]; then
printf "\n***** At least one filename is required! *****\n"
exit 1
fi
accountAlias_safe=$(printf %q "$accountAlias")
sftpDir_safe=$(printf %q "$sftpDir")
srcDir_safe=$(printf %q "$srcDir")
destDir_safe=$(printf %q "$destDir")
fileName_safe=$(printf %q "$fileName")
IFS= read -r -p 'password for user@server: ' -s -t 60 password || {
echo 'password not entered in time' 1>&2
exit 1
}
# Rsync options used:
# -v | verbose
# -c | replace existing files based on checksum, not timestamp or size
# -r | recursively copy
# -t | preserve timestamps
# -h | human readable file sizes
# -P | resume incomplete files show progress bars for large files
# -s | Sends file names without interpreting special chars
# -p | preserve file permissions
# -l | copy symbolic links as links
ssh user@server /bin/bash << EOF
printf "\nCopying following file(s) from %s_old account to %s_new account:\n" ${accountAlias_safe} ${accountAlias_safe}
sudo /bin/bash -c '
while IFS= read -r line; do
src=${sftpDir_safe}${accountAlias_safe}_old${srcDir_safe}"/\${line}"
dest="${sftpDir_safe}/${accountAlias_safe}_new${destDir_safe}/"\${line}"
printf "\n\${src} -> \${dest}\n"
rsync -vcrthPspl "\${src}" "\${dest}"
done <<<'${fileName_safe}'
printf "\nEnsuring all new files are owned by the %s_new account:\n" ${accountAlias_safe}
chown -vR ${accountAlias_safe}_new:${accountAlias_safe}_new ${sftpDir_safe}/${accountAlias_safe}_new${destDir_safe}
'
${password}
EOF
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/466759.html
上一篇:將awk輸出用于條件陳述句
下一篇:修復使用錯誤字符集編碼的檔案
