1

Stuck with the "binary operator expected" message when I run this command :

for variable in "ebooks" "movies" "games" "softwares" "music" "series" "tvreplay"
do
    if [ -d /in/recep/downloader_ftp*/${variable} ]
    then
        mv /in/recep/downloader_ftp*/${variable}/* /in/arch/${variable}/ 
    else
        echo "no files type ${variable}"
    fi
done

I know this is because the command find 2 directories starting with the pattern "downloader_ftp*" in the recep directory, but I can't figure good way to do it.

Sebastian Lenartowicz
  • 4,695
  • 4
  • 28
  • 39
orlin
  • 19
  • 3

2 Answers2

2

Problem is:

-d /in/recep/downloader_ftp*/${variable}

expands to

-d /in/recep/downloader_ftpxxx/${variable} /in/recep/downloader_ftpyyy/${variable}

if you have variable==ebooks and an ebooks directory in each of the downloader_ftpxxx/yyy directory, thus failing the directory test syntax

Fix proposal, no second loop, just check if mv has a correct exit code:

for variable in "ebooks" "movies" "games" "softwares" "music" "series" "tvreplay"
do
     if mv /in/recep/downloader_ftp*/${variable}/* /in/arch/${variable}/  2>/dev/null
     then
        :  # OK
     else
        echo "no files type ${variable}"
     fi
done
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • I don't mind the downvote, please explain. My solution is more faithful to the original problem than the other one and is probably faster because there is no inner for loop. – Jean-François Fabre Aug 28 '16 at 15:01
  • I mind the down-vote — it doesn't seem warranted at all. :( — I'm not sure throwing the error messages away is a good idea, but the rest is OK. – Jonathan Leffler Aug 28 '16 at 15:08
  • thx it's fast but you're right, it would be better to check for `No such file or directory` message, just in case another problem happens in `mv` like access rights, whatever... – Jean-François Fabre Aug 28 '16 at 15:13
  • I didn't downvote, but `x; if [ $? = 0 ]` is usually better written simply `if x` – tripleee Aug 28 '16 at 15:54
  • Also double `==` is permitted by Bash, but not portable. – tripleee Aug 28 '16 at 15:57
  • and my logic was wrong. It was the other way round. Thanks. – Jean-François Fabre Aug 28 '16 at 16:00
  • 1
    I downvoted because, at the time that I downvoted, the answer did not include a solution, merely an explanation of the problem; which could be fine -- sometimes that's all that's needed -- except that the question already included a correct explanation of the problem, so the answer was merely repeating what was already in the question. Now that you've fixed that, I've retracted my downvote. – ruakh Aug 28 '16 at 16:00
  • @ruakh you're right and no hard feelings for the DV anyway those are the "rules". after the DV I figured a better way to do it (IMHO) than the other solution. – Jean-François Fabre Aug 28 '16 at 16:03
1

Well, what I'd do in this scenario might look something like this:

for type in "ebooks" "movies" "games" "softwares" "music" "series" "tvreplay"
do
    for dir in /in/recep/downloader_ftp*/${variable}
    do
        if [[ -d $dir ]]
        then
            mv $dir/* /in/arch/$variable/ 
        else
            echo "no files of type $variable in $dir"
        fi
    done
done

What it does is, loop over every subdir (doing the shell globbing first), and check if it contains the goodies you want.

Sebastian Lenartowicz
  • 4,695
  • 4
  • 28
  • 39