misguided
misguided

Reputation: 3789

Retrieving File name for bash/shell programing

I need to access two files in my shell script. The only issue is , I am not sure what the file name is going to be as it is system generated.A part of the file name is always constant , but the rest of it is system generated , hence may vary. I am not sure how to access these files?

Sample File Names

Type 1

MyFile1.yyyy-mm-dd_xx:yy:zz.log

In this case , I know MyFile1 portion is a constant for all the files, the other portion varies based on date and time. I can use date +%Y-%m-%d to get till MyFile1.yyyy-mm-dd_ but I am not sure how to select the correct file. Please note each day will have just one file of the kind. In unix the below command gives me the correct file .

unix> ls MyFile1.yyyy-mm-dd*

Type 2

MyFile2.yyyymmddxxyyxx.RandomText.SomeNumber.txt

In this file , as you can see Myfile2 portion is common,I can user Date +%Y%m%d to get till (current date) MyFile2.yyyymmdd, again not very clear how to go on from there .In unix the below command gives me the correct file .Also I need to have previous date in the dd column for File 2.

unix> ls MyFile2.yyyymmdd*

basically looking for the following line in my shell script

#!/bin/ksh
timeA=$(date +%Y-%m-%d)
timeB=$(date +%Y%m)
sysD=$(date +%d)
sysD=$((sysD-1))
filename1=($Home/folder/MyFile1.$timeA*)
filename2=($Home/folder/MyFile2.$timeB$sysD*)

Just not sure how to get the RHS for these two files.

The result when running the above scripts is as below

Script.ksh[8]: syntax error at line 8 : `(' unexpected

Upvotes: 2

Views: 221

Answers (1)

Zombo
Zombo

Reputation: 1

Perhaps this

$ file=(MyFile1.yyyy-mm-dd*)

$ echo $file
MyFile1.yyyy-mm-dd_xx:yy:zz.log

It should be noted that you must declare variables in this manner

foo=123

NOT

foo = 123

Notice carefully, bad

filename1=$($HOME/folder/MyFile1.$timeA*)

good

filename1=($HOME/folder/MyFile1.$timeA*)

Upvotes: 2

Related Questions