Problem when concatinating wildcard onto file location in bash script

I am having difficulty with the following script:

#! /bin/bash

filelist=~/data/${1}*
~/./convertFile $filelist ~/temp/output

Essentially, there are a large number of files in the directory ~/data, each with a four-letter code at the beginning (eg. aaaa001 aaaa002 bbbb001 bbbb002 etc). The argument of this script ($1) is one of these four letter codes.

convertFile is a program written in fortran which takes N arguments, the first N-1 being the location of files that will be converted into one file, which will be outputted to the location in the Nth argument.

When I run (for example):

./convertFile aaaa* output

from the terminal, the "aaaa*" is expanded out and convertFile acts on every file in the expanded list (as one would expect). However, when I try to write a script to do this in an automated process, it doesn't expand out the files, instead passing the explicit directory location ~/data/aaaa* (where, for example, $1 = 'aaaa'). I don't know what's wrong, is there something awry with how I'm concatenating the strings?

If you get the directory wrong, it won't expand and will stay a literal *.

You can try 'echo $filelist' to see if it's expanding to what you expected.

Thanks, I've double checked it, and the directory is definitely correct.

Try specifying an absolute path instead of ~/

And is it exactly as you've given it here, or is anything quoted?

What does the #!/bin/sh line of your script look like?

You could also try feeding it directly into convertfile as in echo convertfile ~/data/${1}* output

The directory names have been changed here (to make it read easier) but everything else is exactly the same, including the #! line.

Absolute directory names bring no joy, I'm afraid...

Can you post the entire script in one block instead of clips of it?

That is the entire script.

Above the shell globbing, try

set +o noglob

I'm afraid I don't know what the shell globbing is, but I tried it in the script and in the terminal prior to running the script, and I have the same problem.

The noglob is a bash-specific shell option that's fairly self-explanatory. I thought maybe it'd gotten turned on somehow.

In the script,

echo ~/data/${1}*

...then copy-paste the resulting output into your shell to see if it expands there. If it doesn't there's a subtle typo.

1 Like

Try this
#! /bin/bash
filelist=${1}*
cd ~/data
~/./convertFile $filelist ~/temp/output

Okay, I feel stupid. It turns out that it was expanding, but there was actually a file called "aaaa*" in the directory, which meant that the first record in the expansion was "aaaa*", and this in turn made it look like all convertfile was getting was the unexpanded string. Thanks so much for your help!