简体   繁体   中英

Bash: can't expand wildcard in filename from "read" command variable

I've exhausted every search I could.

I'm trying to read a filename/path from the CLI using read so I can link files for sorting in bulk. I've tried all sorts of things from quotation marks to none, trying to use eval to expand, and so many other things. None of them work.

Here's my script in it's current state:

#!/bin/bash
read -p "from? (explicit, folder/) " -e from
read -p "to? (explicit, folder/) " -e to
read -p "base filename before ep name? " -e formatOldStart
read -p "base filename after ep name? " -e formatOldEnd

...

  lnFrom=$from$formatOldStart$currentEpisodeNumber$formatOldEnd

  echo $lnFrom

  #ln "$lnFrom" "$lnTo"

Here's an example usage:

$ from? (explicit, folder/) $HOME/archive/_DATA_ARCHIVE/.data/Example\ Complete\ Series\ +\ Movies\ \[Dual\ Audio\]/Example\ \[Dual\ Audio\ DVD\ 480p\]/
$ to? (explicit, folder/) $HOME/archive/_DATA_ARCHIVE/_video_storage/Anime/Shows/Example/Season\ 1/
$ base filename before ep name? \(Hi10\)_Example_-_
$ base filename after ep name? _(DVD_480p)_(a-S)_(*).mkv

And then here's the output:

$HOME/archive/_DATA_ARCHIVE/.data/Example Complete Series + Movies [Dual Audio]/Example [Dual Audio DVD 480p]/(Hi10)_Example_-_015_(DVD_480p)_(a-S)_(*).mkv

Here's the expected output:

/home/user/archive/_DATA_ARCHIVE/.data/Example Complete Series + Movies [Dual Audio]/Example [Dual Audio DVD 480p]/(Hi10)_Example_-_015_(DVD_480p)_(a-S)_(6852D8ED).mkv

I was only able to get the expected output after manually inserting wildcards (not regex) and file names suggested from this post , but this obviously means that I have to edit this script manually every time I want to use it for a new folder without auto-complete. I have a LOT of stuff to categorize, so that is simply not possible.

Edit: After taking a different approach, I came up with a much easier one-liner that mostly works with tab complete (until the variables of course):

for index in {28..54}; do ln "/home/user/archive/_DATA_ARCHIVE/.data/Example Complete Series + Movies [Dual Audio]/Example [Dual Audio DVD 480p]/(Hi10)_Example_-_0${index}_(DVD_480p)_(a-S)_("*").mkv" "/home/user/archive/_DATA_ARCHIVE/_video_storage/Anime/Shows/Example/Season 2/Example S2E0${index}.mkv"; done

So I had someone help out and thankfully I was able to solve it.

First, we use -r in the read command to not parse the escape characters.

Then we assign the lnFrom and lnTo without quotes:

lnFrom=$from$formatOldStart$currentEpisodeNumber$formatOldEnd
lnTo=$to$formatNewStart$currentEpisodeNumber$formatNewEnd

Then we use eval on the ln command to parse the variables:

eval ln "$lnFrom" "$lnTo"

Now yes, I understand the issues with eval about it being a security issue, but this script is running on a closed network at my home, so I don't care.

If someone can provide a solution that works without eval, I'd be more than happy to accept that answer.

Sorry if this was obvious to a lot of you. I am familiar with JavaScript at work, but I am not super familiar with bash/unix besides basic commands to get around the OS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM