I am writing a script to get output if a specific file is created successfully or failed.
Example: In a path /x/y/z/ everyday a file with name abc.out_timestamp will be created.
The script that i am working needs check if the file is created today or not and show the output accordingly.
Path : /x/y/z have the below files
abc.out0114181500
abc.out0115181600
abd.out0116182100
script to be run:
#!/bin/bash
TODAY=$(date +%m%d%y)
cd /x/y/z
if [ -f abc.out_$TODAY ]
then
echo "$file creation successful"
else
echo "$file creation not successful"
fi
output received: file creation not successful (though a file is created today) expected output: file creation successful
How can i correct this, can anyone please let me know how i can the output as file creation successful.
Thanks
You're 98% of the way there
#!/bin/bash
TODAY=$(date +%m%d%y)
cd /x/y/z
if [ -f abc.out${TODAY}???? ]
then
echo "$file creation successful"
else
echo "$file creation not successful"
fi
The pattern abc.out${TODAY}????
matches the filename with today's date followed by 4 more characters (and without the underscore, which was spurious).
If you want more validation, use
if [ -f abc.out${TODAY}[0-2][0-9][0-5][0-9] ]
I hope the abd.
was a typo and it's really abc.
However, if it could be abc
or abd
you can handle that with
if [ -f ab[cd].out${TODAY}[0-2][0-9][0-5][0-9] ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.