The script I am writing must be able to run several commands (tar, gzip etc) on filenames that are supplied by variables. I am unsure as to what syntax is required/ideal when referencing variables in filenames. The following is a sample command that I would like the script to execute:
tar cvf bk{fileName}.${DATEyear}${DATEmonth}.tar {fileName}.${DATEyear}${DATEmonth}* >> /ebccsBackupReport.txt
Is this really poor form? The tar filename format must be bkFILENAME.YEARMONTH.tar, where the filename, year and month are variables set earlier in the script.
Also, is there a way to verify where one or more files bearing a variation on a filename exist within the directory? For example, all files under filename.*? I have been using
if [ -f filename.* ]
but I've been told that this statement is intended more for verifying the existance of a single file, not a range of files.
Almost there, but you'd need a $ before {filename} so that the variable's value is evaluated, like you've done with the DATEyear and DATEmonth variables. Apart from that, spot on.
This really depends on exactly what you're looking for... say if I had some files in my current directory named file1, file2, file3, filen, etc.... you could check how many files match a certain pattern (i.e. which contain the string "file") using find and piping through wc.... Then check that value...
... this line will hold true if any file in the directory starts with "filename." so you will actually see what you're looking for if at least one of them exists in the directory ... verify this with ...
root_box:/tmp # touch filename.1 filen.1 filename.3 file.3 file4
root_box:/tmp # ls file*
file.3 file4 filen.1 filename.1 filename.3
root_box:/tmp # if [ -f filename.* ]
> then
> ls filename.*
> else
> ls
> fi
filename.1 filename.3
root_box:/tmp # rm filename.1 filename.3
root_box:/tmp # ls
file.3 filen.1 orbit-root speckeysd.lock
file4 mpHyay4c plugtmp
root_box:/tmp # ls file*
file.3 file4 filen.1
root_box:/tmp # if [ -f filename.* ]
> then
> ls filename.*
> else
> ls
> fi
file.3 filen.1 orbit-root speckeysd.lock
file4 mpHyay4c plugtmp
root_box:/tmp #