Bash将变量插入到反引号命令中

时间:2014-10-05 09:31:27

标签: bash variables backticks

我需要使用此命令

/usr/local/bin/mcl find -f .bz2

返回给我

:???????? '/Cloud Drive/test1.bz2'
:???????? '/Cloud Drive/test2.bz2'

进入BASH脚本。问题是我需要最后一个参数(.bz2)作为变量。

我已尝试过这个

FILENAME=".bz2"
UPLOADED=$(/usr/local/bin/mcl find -f $FILENAME)
# do something with $UPLOADED

但显然它不起作用。经过对StackOverflow和网络的一些研究后,我发现了几种方法(甚至使用反引号),但我仍然无法使其工作。

这样做的正确方法是什么?

3 个答案:

答案 0 :(得分:1)

您可以尝试保存以下内容,例如./script.sh

filename="${1:-.bz2}"    #<-- your variable as 1st argument, defaults to .bz2

do_my_work() {
    local uploaded="$1"
    #do whatever you want with the "uploaded"
    printf "got:==%s==\n" "$uploaded"
}

while IFS= read -r __mc1path
do
    do_my_work "$__mc1path"
done < <(mc1 find -f "$filename" | sed "s/.*'\(.*\)'.*/\1/")
#        variable----^^^^^^^^^^^         ^^^^^^^^^^^^^^^^^^- keep only stuff inside of quotes

并将其用作

./script.sh .bz2   #or anything, defaults to ".bz2"

将打印

got:==/Cloud Drive/test1.bz2==
got:==/Cloud Drive/test2.bz2==

答案 1 :(得分:1)

你的意思是这样吗?

uploaded=$(mcl find -f "$FILENAME" | cut -d"'" -f2)
for u in $uploaded; do
    echo "$u"
    # process "$u"
done

答案 2 :(得分:-1)

我想你想要那个:

UPLOADED=`/usr/local/bin/mcl find -f $FILENAME`