如何在传递给命令行参数时转义bash中的变量

Eri*_* B. 1 variables bash escaping ifs

我有一个bash脚本(cygwin),它使用了一些带有空格的窗口路径.因此,我在变量定义中使用\来逃避空间.脚本中的所有内容都可以正常工作.但是,我需要将此变量作为参数传递给命令行可执行文件.当我这样做时,我的逃避变得混乱.

示例非功能脚本:

#!/bin/sh

# File paths
destinationPath="/cygdrive/c/Documents and Settings/Eric/My Documents/"
attachments="\n2013-12-12.pdf"
body="Test Body"
recipient="asdf@asdf.com"


# 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 nosender@mydomain.com --from-name "Automated CSS Downloader" --subject "Downloaded new file(s) \
  from CSS" $args eric@mydomain.com
Run Code Online (Sandbox Code Playgroud)

脚本输出:

$ bash -x test.sh
+ destinationPath='/cygdrive/c/Documents and Settings/Eric/My Documents/'
+ attachments='\n2013-12-12.pdf'
+ body='Test Body'
+ recipient=asdf@asdf.com
+ 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 nosender@mydomain.com --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 eric@mydomain.com
email: WARNING: Email address 'and' is invalid. Skipping...
email: FATAL: Could not open attachment: /cygdrive/c/Documents: No such file or directory
Run Code Online (Sandbox Code Playgroud)

因此,您可以看到路径中的转义空间未在$ args变量中导出.我假设错误出现在"args = ..."行.但我不确定如何转义$ destinationPath以确保保留转义字符.

我尝试在destinationPath中使用双引号(没有转义空格)但无济于事.如果我尝试在args =行中双引号$ destinationPath,那么输出也会被一堆额外的引用搞砸了.

我怎样才能让它发挥作用?我试过玩$ IFS变量,但我真的不知道我正在做什么,并且似乎也无法使用它,尽管我怀疑解决方案与之有关$ IFS.

ric*_*ici 5

没有数组就没有好办法做到这一点.(有一种不太好的方法,使用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="asdf@asdf.com"


# 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 nosender@mydomain.com \
        --from-name "Automated CSS Downloader" \
        --subject "Downloaded new file(s) from CSS" \
        "${args[@]}" eric@mydomain.com
Run Code Online (Sandbox Code Playgroud)

您可能还想将attachments变量变为数组; 从换行符的存在,我假设你实际上是以一种更复杂的方式设置它,所以我没有更改代码.但是如果附件名称中有空格,那么你当前的代码将不起作用(我很确定新行将通过for表单中的参数扩展来消除,除非你改变了它的值$IFS,所以不file=${file//[ \\n]/}应该'是必要的.)