#!/bin/sh
for i in "/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_"$(date +%Y-%m-%d --date "4 day ago")"_tmptcmsaslva2_"*".log"
do
echo "${/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/##*/}"
done < output_file.txt
Got error as
line 4: ${/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/##*/}: bad substitution
My requirement was to have only the file name (instead of filename with directories) in output_file.txt
---------- Post updated at 07:20 AM ---------- Previous update was at 07:16 AM ----------
My script location and output file location is /usr/sas/tir/test/loganalysis.
I need to search for a file in /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/.
Now how to modify your script to include the directories?
#!/bin/sh
for i in "/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_"$(date +%Y-%m-%d --date "4 day ago")"_tmptcmsaslva2_"*".log"
do
echo "${i##*/}"
done < output_file.txt
With this code I can get a .txt file as required. However, I would like to get a fresh file everyday instead of the appending the the same .txt file.
#!/bin/sh
for i in "/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_"$(date +%Y-%m-%d --date "4 day ago")"_tmptcmsaslva2_"*".log"
do
echo "${i##*/}" < output_file.txt
done
e.g. If I run today I may get 3 records in output_file.txt. When I run tomorrow I should not get the same records along with current day's records in output_file.txt rather I should get only current day's records in output_file.txt
There are two ways to complete this request.
I- Keep getting a output file which has particular day's date in it's name itself, so each day files will be unique and different names.
#!/bin/sh
DATE=`date +%y%m%d`
for i in "/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_"$(date +%Y-%m-%d --date "4 day ago")"_tmptcmsaslva2_"*".log"
do
echo "${i##*/}" >> "output_file_"$DATE".txt"
done
II- Each day check for file named output_file.txt and rename it to that's day's date and create a output file with name output_file.txt.
#!/bin/sh
DATE=`date +%y%m%d`
if [[ -f output_file.txt ]]
then
mv output_file.txt "output_file_"$DATE".txt"
fi
for i in "/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_"$(date +%Y-%m-%d --date "4 day ago")"_tmptcmsaslva2_"*".log"
do
echo "${i##*/}" >> output_file.txt
done
There is other way which I found now:). Just I put a remove command at the beginning of the script like below.
#!/bin/sh
rm output_file.txt
for i in "/usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_"$(date +%Y-%m-%d --date "1 day ago")"_tmptcmsaslva2_"*".log"
do
echo "${i##*/}" >> output_file.txt
done
Now I'm looking to capture the script execution in log files (.log)
With this code snippet, you won't get any output_file.txt . Replacing < with > , you'd get an output_file.txt with exactly the last .log file name encountered.
Putting the > output_file.txt right after the done , as proposed by Corona688 in post#8, you'll have a new output file every time you run the snippet.