I have two servers and my computer where I can access both of them. I made a script where the shell ask the user to input a job number. Then all I need the script to do is copy the files from one dir to another. The problem is one of the dir in the file path to the files starts with the job number but ends with some stuff I don’t want the user to have to input.
The file path starts like /Volumes/JDRIVE/Jobs/${Jobnumber}_text/Definedfolders/Files/
I want the user to be able to type ${Jobnumber}
and ignore all the text after, then get the files in the files dir.
The job number folder is like this 84464_ARAGMG I need the user to be able to type just the 84464 and ignore the text after it.
I tried storing the file path in the script in a variable.
basepath="/Volumes/JDRIVE/Jobs/"
echo "job number?"
read Jobnumber
filepath="${basepath}${Jobnumber}_*/Definedfolders/Files/"
I tried using the *
for the unwanted text. What am I doing wrong?
6
First of all, there is no glob expansion done in an assignement, unless you assign to array. For instance
f=*
assigns to the shell variable f
the string *
, while
f=(*)
assigns all the files in your working directory to the array variable f
.
The reason is that a glob usually matches all the files, and if you don’t use an array, it’s not clear what should be stored in the variable.
The other problem in your code is that you write the *
inside a double-quoted string, and this would inhibit glob expansion, no matter where you do it. For instance
f=("*")
creates an array with a single element, containing a literal asterisk.
A final problem is, that if your glob does not match any file, you will store the unexpanded glob string.
Hence in your case you should write
filepath=($basepath${Jobnumber}_*/Definedfolders/Files/(N))
if (( $#filepath == 0 ))
then
print -u 2 No directory for jobnumber $Jobnumber
else
echo Found folder $filepath[1]
fi
The (N)
ensures that the array is empty, if no matching path is found.