Rahul
Rahul

Reputation: 726

How to concat string and variable with wild card in shell script

I am using Amazon Centos server, I want to check if file exists or not using shell script.

I have a directory location and file name format.

my code:

yesterdaydate= date -d '-1 day' '+%Y%m%d'
echo $yesterdaydate
Filetemp='/data/ftp/vendor/processed/ccs_'
Filetemp=$Filetemp$yesterdaydate
echo $Filetemp

Expected final result should be like this /data/ftp/vendor/processed/ccs_20210221_171503_itemsku.xml

Current result: /data/ftp/corecentric/processed/ccs_

I want result in format like Filetemp_yesterdaydate_wildcard(*)_itemsku.xml

Wildcard(*) is use as time varies of file creation.

How can I achieve this?

Upvotes: 0

Views: 1397

Answers (1)

Socowi
Socowi

Reputation: 27225

Globs (= wildcards) work perfectly fine with variables as long as you don't quote the wildcard.
Here we use arrays and bash's nullglob option to count the number of files found.

#! /bin/bash
shopt -s nullglob
yesterday=$(date -d '-1 day' '+%Y%m%d')
files=(/data/ftp/vendor/processed/ccs_"$yesterday"_*_itemsku.xml)
if (( "${#files[@]}" > 0 )); then
   echo "Found the following ${#files[@]} file(s)"
   printf %s\\n "${files[@]}"
else
   echo "Found nothing"
fi

Note that this would also match ...20210222_something that is not a time string_itemsku.xml if there was such a file. If that's the case you can change the glob * to [0-2][0-9][0-5][0-9][0-5][0-9].

Upvotes: 1

Related Questions