アーカイブ中にtar --transform
DOCUMENTNAMES配列に基づいて文書名を変更するためにSSH呼び出しを使用しています。
tar -uvf /tmp/$TARGET''TVL_document.tgz'' --transform='s|'\${doc}'|'\$docname'|' -C /appl-doc/a-mc-acc-dms/DATA \${doc}
DOCUMENTNAMESにスペースがない場合(例: "application.pdf")、tar出力を取得できます。空白のあるDOCUMENTNAMES(例: "project form.pdf")の場合、シェルはそれを正しく拡張できず、エラーが発生します。tar invalid transform expression
--transform='s|'\${doc}'|'\$docname'|'
DOCUMENTNAMESのスペースを正しくエスケープする方法でシーケンスをエスケープするにはどうすればよいですか?
完全なコード:
DOCUMENTNAMES=("project form.pdf" "application.pdf")
# SSH to Linux fileshare to create tar
sshpass -p $IPA_PASS ssh -tt -o StrictHostKeyChecking=no $IPA_NAME@$TARGET "sudo su - jboss <<'EOF'
# create an empty tar
tar -cvf /tmp/$TARGET''TVL_document.tgz'' -T /dev/null
# transfer local array to remote
eval `typeset -p DOCUMENTNAMES`
index=0
for doc in "${DOCUMENTIDS[@]}"
do
docname=\${DOCUMENTNAMES[\$index]}
# Update tar with one document at a time and update name using transform
tar -uvf /tmp/$TARGET''TVL_document.tgz'' --transform='s|'\${doc}'|'\$docname'|' -C /appl-doc/a-mc-acc-dms/DATA \${doc}
index=$((index+1))
done
EOF
"
答え1
あなたが望むようです:
DOCUMENTNAMES=("project form.pdf" "application.pdf")
# never pass password on the command line. Here passing it via an env var
# instead
SSHPASS=$IPA_PASS sshpass -e \
ssh -o StrictHostKeyChecking=no "$IPA_NAME@$TARGET" '
sudo -Hu jboss bash' << EOF
# pass variables across by dumping their definition on the client shell
# into the code interpreted by the remote shell
$(typeset -p TARGET DOCUMENTNAMES DOCUMENTIDS)
# rest in a heredoc with quoted delimiter so no expansion happens.
$(cat << 'END_OF_SCRIPT'
# never put files with fixed name in world writable directories
# here using a ~/tmp dir instead. You could also use things like:
# tmpdir=$(mktemp -d) || exit
tmpdir=~/tmp
tarfile=$tmpdir/${TARGET}TVL_document.tgz
mkdir -m 700 -p ~/tmp || exit
# create an empty tar
tar -cvf "$tarfile" -T /dev/null
index=0
for doc in "${DOCUMENTIDS[@]}"
do
docname=${DOCUMENTNAMES[index]}
tar -uvf "$tarfile" --transform="s|$doc|$docname|" -C /appl-doc/a-mc-acc-dms/DATA "$doc"
(( index++ ))
done
END_OF_SCRIPT
)
EOF