0

I'm trying to use this variable

MediaExt="*.{mp4,mkv,avi}"

in this command

mv ${MediaSource}/${MediaExt} ${UploadDir}

but it doesn't seem to work.

Could someone help me, please? Thanks!

Barmar
  • 741,623
  • 53
  • 500
  • 612
André Luís
  • 141
  • 2
  • 7
  • 1
    What doesn't work? Are you moving the wrong files? Do you get an error? If you just do `echo ${MediaSource}/${MediaExt}` what do you get? – Daniel Pryden Jan 10 '19 at 20:52
  • It gives me this: ```/home/me/media/downloaded/testevid/*.{mp4,mkv,avi} /home/me/media/upload```. However, it doesn't move the file at all. If I use ```mv ${MediaSource}/*.{mp4,mkv,avi} ${UploadDir}```, instead, It works. It's not catastrophic, as I could find and replace all, but I'd like to understand what is going on. – André Luís Jan 10 '19 at 21:07
  • 1
    Does that glob actually match anything? A glob expression will expand to itself if it doesn't match any files. – Daniel Pryden Jan 10 '19 at 21:13
  • I'm not sure I understand your question, but I think only ```${MediaSource}``` returns a global variable. ```$MediaExt ```and ```$UploadDir``` are supposed to be local variables. ```${MediaSource}``` = ```MediaSource=${DOWNLOAD_DIR}/${DOWNLOAD_NAME}``` – André Luís Jan 10 '19 at 21:16
  • 1
    "glob" as a verb means to expand a string into a set of filename matches. That's how your shell takes something like `/home/me/media/downloaded/testevid/*.{mp4,mkv,avi}` and turns it into a set of filenames to pass to the target process (`mv` in this case). If the globbing process doesn't match any files, rather than returning an empty argument, it expands to itself. So instead of `mv` getting a set of files it just gets a single string which doesn't match any file. – Daniel Pryden Jan 10 '19 at 21:19
  • 3
    Braces aren't expanded after variable replacement. – Barmar Jan 10 '19 at 21:22
  • Wait. Something is wrong. if I run the ```mv``` command directly, with absolute path (```ls /home/me/media/downloaded/testevid/*.{mp4,mkv,avi}```), I get 3 results: 1 is success for a .mp4 file and 2 others are ```cannot access```, since there are no .mkv or .avi files under that directory. However, if I run the same command in a bash script with the variables, I'm getting ```mv: cannot stat '/home/me/media/downloaded/testevid/*.{mp4,mkv,avi}': No such file or directory```. – André Luís Jan 10 '19 at 21:31

1 Answers1

1

A command in bash is parsed in several passes. The pass that decides whether globbing (expanding *, or *.{mp4,mkv,avi}) should be performed, is occurring before the pass that expands the variables. Once the variables are expanded, there are candidate for globbing, but the decision that no globbing is required has already been made

It will work if you write the expression as:

mv ${MediaSource}/*.{mp4,mkv,avi} ${UploadDir}

You'll probably find some advise that you can use eval. Please don't!
This:

eval mv ${MediaSource}/${MediaExt} ${UploadDir}

will execute as you intended, but eval can be dangerous if you don't control the values of the variables. For example, if UploadDir could be set to:

UploadDir="somedirectory; rm -rf ~"

then eval will execute your request as two statements and remove all your files.

Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71