ToX 82
ToX 82

Reputation: 1074

Bash insert a variable into a backticked command

I need to use this command

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

which returns me this

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

into a BASH script. The problem is that I need the last parameter (.bz2) to be a variable.

I've tried with this

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

But obviously it is not working. After some research on StackOverflow and on the web I have found several ways to do something like that (even using backticks), but still I can't manage to make it work.

What is the correct way to do that?

Upvotes: 0

Views: 2241

Answers (3)

clt60
clt60

Reputation: 63922

You can try save the following as e.g. ./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

and use it as

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

and will print

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

Upvotes: 1

tripleee
tripleee

Reputation: 189457

You mean like this?

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

Upvotes: 1

sotcha
sotcha

Reputation: 923

I think you want that :

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

Upvotes: -1

Related Questions