Reputation: 187
I'm working on my University assignment and I've stumbled upon an issue I can't fix by myself, perhaps somebody here will be able to help, since I'm out of ideas...
I have to pass filenames specified as a list of arguments passed to my main script (Bash) and later process these files in another script (Perl).
This is how I am doing it in Bash:
perl DisplayGroupedByArtist.pl "$@"
Then in perl I iterate over the files like that:
my @files = @ARGV;
foreach $file (@files)
{
#some stuff here
}
The problem is - it's working just fine as long as I don't invoke the script from outside the directory.
test/bash_script.sh Samples/*.mp3
Then, instead of something like "Samples/01 Lycia - Frozen.mp3 Samples/01 Slowdive - Alison.mp3 Samples/01 The Cure - Plainsong.mp3", the value passed to my Perl script is simply "Samples/*.mp3".
I guess it's worth mentioning that in my Bash script I have this line to ensure that I'm invoking Perl script from the correct directory:
cd "$(dirname "$0")"
Update:
To make things clear: The main problem here is the fact that if I run the script like bash_script.sh *.mp3
- it passes the file list to Perl script with no issues, whilst if I execute it like test/bash_script.sh *.mp3
, the value passed to Perl is simply a string "*.mp3", not a list of files.
Upvotes: 0
Views: 476
Reputation: 22237
This is not a Perl problem, but a problem of the calling shell. Assuming that your shell is bash (but note that your teacher's shell might be something differently), a wild card pattern such as Samples/*.mp3 is usually expanded by the shell into a list of files; now what exactly happens if no file matches the pattern, depends on the shell and how it is configured. For example, the default in bash is that the string representing a pattern is passed unchanged to the application, but the shell could also be configured so that the pattern is simply removed, or that an error message is printed.
You have no idea how the teacher's shell is configured, and if your teacher uses a different shell from bash, the behaviour might even be different.
When you define an interface to a program, you never specify "how it is typed in the calling shell", because this doesn't mean much. You specify, what parameters the program accepts (i.e. the contents of ARGV) and how the these parameters affect the execution of the program. What the caller (yourself, or the teacher) exactly has to type so that your program receives the arguments in the correct form, is not part of an interface specification.
For example, you can allow relative pathes to be passed, and define a search order where to look for the files. You can also allow that the strings passed to your command contain wildcards. You can even allow regular expressions or what else comes to your mind, but never forget: You have no access to the sequence of characters, which the caller has actually typed, so you can't do any interpretation on them.
Upvotes: 0
Reputation: 531325
You have to adjust the argument; Samples/*.mp3
is relative to where the script is called from, not to the location of the script.
tests/bash_script.sh ../Samples/*.mp3
Upvotes: 1