0

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?

ToX 82
  • 1,064
  • 12
  • 35
  • 1
    This structure works for me. If I create a BASH file containing `VARIABLE="content"` followed by `RESULT=$(echo $VARIABLE)` then `echo $RESULT` outputs simply `content`. What do you get instead of the expected result? Can you edit your question to add the error message or incorrect output? – Bobulous Oct 05 '14 at 10:59
  • @Arkanon, that's true for `content`. It's not true for `$'content\n\n'` -- the trailing newlines get stripped. It's not true for `"*"` unless you fix the quoting at expansion time (`echo "$VARIABLE"`, not `echo $VARIABLE`). It's not true for `-e` or `-n` if your echo eats those parameters. Etc etc. – Charles Duffy Oct 05 '14 at 21:46

3 Answers3

1

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==
clt60
  • 62,119
  • 17
  • 107
  • 194
  • Might clear IFS during the read to avoid stripping whitespace from the end of filenames. Otherwise, this looks about perfect. – Charles Duffy Oct 05 '14 at 15:49
  • @CharlesDuffy YES! and YES! and again YES! everytimes forget it. ;) :) added... thanx. ;) – clt60 Oct 05 '14 at 15:50
1

You mean like this?

uploaded=$(mcl find -f "$FILENAME" | cut -d"'" -f2)
for u in $uploaded; do
    echo "$u"
    # process "$u"
done
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • This is a bit more than what I needed but it works perfectly. I was probably missing the double quotes as you stated in your other comment and and this was causing my check to fail. Thanks!! – ToX 82 Oct 05 '14 at 15:25
  • 3
    @ToX82 Not really understand how this could work for your example, where the `mcl` returns `'/Cloud Drive/test1.bz2'` - e.g. space separated path, and the `for u in $uploaded` will assign to `u` 1st `/Cloud` and 2nd `Drive/test1.bz2`... So strange accept... – clt60 Oct 05 '14 at 15:41
-1

I think you want that :

UPLOADED=`/usr/local/bin/mcl find -f $FILENAME`
sotcha
  • 923
  • 7
  • 15
  • Well yes, but that is the same thing I was trying with backticks instead of $()... I'd like a working version of that :) – ToX 82 Oct 05 '14 at 09:42
  • Just a question : UPLOADED=$(/usr/local/bin/mcl find -f .bz2) gives you back the right result? – sotcha Oct 05 '14 at 09:52
  • Yes, even if it is separated by a space and not a newline (which is the same for what I need) :???????? '/Cloud Drive/test1.bz2' :???????? '/Cloud Drive/test2.bz2' – ToX 82 Oct 05 '14 at 09:55
  • No, it isn't. The space appears when you `echo $UPLOADED` without double quotes. The proper syntax is `echo "$UPLOADED"`. – tripleee Oct 05 '14 at 13:12
  • @sotcha, storing lists of paths in scalar variables isn't exactly good practice -- since the only character that can't exist anywhere in a path is `NUL`, and scalar variables in bash (like every other shell using C-style strings) can't store NULs, such approaches are doomed to having filenames that they can't correctly handle. – Charles Duffy Oct 05 '14 at 15:45
  • @CharlesDuffy I don't get your point, NUL can't exist in a path and scalar can't store NUL. Where is the problem ? Do you have any example where storing lists of paths in scalar caused problem ? Any documentation suggestion ? – sotcha Oct 05 '14 at 19:56
  • @ToX82 PARAM=".jpg"; LIST=$(mcl find -f $PARAM) ; echo "$LIST"; Gives me back a list with my jpg ! – sotcha Oct 05 '14 at 19:57
  • @sotcha, the problem is if you want to have *more than one* path inside a single scalar. You literally can't do it without restricting the range of possible filenames more tightly than the filesystem itself does, because how are you going to separate the names? A newline is common, but inadequate: Newlines are legal inside POSIX filenames. So, how do you tell the difference between one file `$'foo\nbar'` and two files `foo` and `bar`? – Charles Duffy Oct 05 '14 at 21:42
  • @CharlesDuffy Got it thanx. But what's your solution about files with newline, because no solution in this page solves that problem. I tried them with filename that contains a newline. – sotcha Oct 06 '14 at 01:53
  • @sotcha, that depends on the details of how the input format delimits names. The ideal case is to have the input format NUL-delimited, and use a `while IFS='' read -r -d ''` loop to pull names off the stream. Other, less-ideal solutions exist... but depends again on the input stream spec, and I have no idea how `mcl` is intended to handle the case. – Charles Duffy Oct 06 '14 at 02:26
  • ...anyhow -- using an array rather than a scalar means that _storage_ works correctly with arbitrary names, and then decouples the matter of _parsing_ to be separately handled. – Charles Duffy Oct 06 '14 at 02:27
  • (If your input format uses escape sequences like `\n` to represent newlines and special characters, then you'd actually leave the `-r` off the `read` command at parse time, and they'll be interpreted rather than stored literally). – Charles Duffy Oct 06 '14 at 02:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62498/discussion-between-sotcha-and-charles-duffy). – sotcha Oct 06 '14 at 03:24