我有一个 Bash 脚本 (Cygwin),它使用一些带有空格的 Windows 路径。因此,我逃离了这个空间\
在我的变量定义中。
脚本中的所有内容都运行良好。但是,我需要将此变量作为参数传递给命令行可执行文件。当我这样做时,我的逃跑就会变得一团糟。
非功能性脚本示例:
#!/bin/sh
# File paths
destinationPath="/cygdrive/c/Documents and Settings/Eric/My Documents/"
attachments="\n2013-12-12.pdf"
body="Test Body"
recipient="[email protected] /cdn-cgi/l/email-protection"
# Prepare attachments
args=""
for file in $attachments ; do
file=${file//[ \\n]/}
touch $file
mv $file "$destinationPath/$file"
args="$args -a $destinationPath/$file"
done
# Send email
echo -e $body | email --from-addr [email protected] /cdn-cgi/l/email-protection --from-name "Automated CSS Downloader" --subject "Downloaded new file(s) \
from CSS" $args [email protected] /cdn-cgi/l/email-protection
脚本的输出:
$ bash -x test.sh
+ destinationPath='/cygdrive/c/Documents and Settings/Eric/My Documents/'
+ attachments='\n2013-12-12.pdf'
+ body='Test Body'
+ [email protected] /cdn-cgi/l/email-protection
+ args=
+ for file in '$attachments'
+ file=2013-12-12.pdf
+ touch 2013-12-12.pdf
+ mv 2013-12-12.pdf '/cygdrive/c/Documents and Settings/Eric/My Documents//2013-12-12.pdf'
mv: listing attributes of `2013-12-12.pdf': Invalid argument
+ args=' -a /cygdrive/c/Documents and Settings/Eric/My Documents//2013-12-12.pdf'
+ echo -e Test Body
+ email --from-addr [email protected] /cdn-cgi/l/email-protection --from-name 'Automated CSS Downloader' --subject 'Downloaded new file(s) from CSS' -a /cygdrive/c/Documents and Settings/Eric/My Documents//2013-12-12.pdf [email protected] /cdn-cgi/l/email-protection
email: WARNING: Email address 'and' is invalid. Skipping...
email: FATAL: Could not open attachment: /cygdrive/c/Documents: No such file or directory
因此,如您所见,路径中的转义空间不会导出到 $args 变量中。我假设错误出现在“args=...”行上。但我不确定如何转义 $destinationPath 以确保保留转义的字符。
我尝试在destinationPath中使用双引号(没有转义空格),但无济于事。如果我尝试在 args= 行中双引号 $destinationPath,那么输出也会因一堆额外的引用而变得混乱。
我怎样才能让它发挥作用?我尝试过使用 $IFS 变量,但我真的不知道我在用它做什么,而且似乎也无法让它与它一起工作,尽管我怀疑该解决方案与 $ 有关国际金融服务协会。
如果没有数组,就没有好的方法来做到这一点。 (有一个不好的方法,使用eval
,但数组更简单。)
问题是您希望每个文件最终作为一个参数email
,但您希望将所有这些参数累积到一个 Bash 变量中。没有办法做到这一点:您可以将 Bash 变量作为单个参数插入("$arg"
) 或者您可以将其插入为任意数量的单词 ($arg
),但你不能根据变量创建时是否转义空格来分割它,因为 Bash 只记住分配给变量的字符串,而不记住转义标记。
但是,您可以使用数组来完成此操作,因为您可以使每个文件名恰好成为一个数组元素,并且您可以让 Bash 插入一个数组作为每个元素的一个参数。
就是这样:
# File paths
destinationPath="/cygdrive/c/Documents and Settings/Eric/My Documents/"
attachments="\n2013-12-12.pdf"
body="Test Body"
recipient="[email protected] /cdn-cgi/l/email-protection"
# Prepare attachments
args=()
for file in $attachments ; do
file=${file//[ \\n]/}
touch $file
mv $file "$destinationPath/$file"
args+=(-a "$destinationPath/$file")
done
# Send email
echo -e $body |
email --from-addr [email protected] /cdn-cgi/l/email-protection \
--from-name "Automated CSS Downloader" \
--subject "Downloaded new file(s) from CSS" \
"${args[@]}" [email protected] /cdn-cgi/l/email-protection
您可能还想做attachments
变量存入数组;从换行符的存在来看,我假设您实际上是以某种更复杂的方式设置它,所以我没有更改代码。但是如果附件名称中有空格,您当前的代码将无法工作(并且我非常确定换行符将被中的参数扩展消除)for
形式,除非您更改了值$IFS
, so file=${file//[ \\n]/}
应该没有必要。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)