Midhun 123
asked on
To read filenames and according to that do some functions in shell script
Hi Experts,
I have to write a script which first read filenames in a directory and according to the name it should run jar files.
for eg filenames are according to the month and year-----07-18-01.txt ,07-18-02.txt , 08-18-01.txt,-------- (month-year-randomnumbers. txt).
My script should read these filenames , If moth-year matches with current month-year.It should run a.jar
otherwise b.jar
Current month year ,I am giving as
if not ,it should run b.jar
Could you please help me to solve it..
Thanks in Advance.
I have to write a script which first read filenames in a directory and according to the name it should run jar files.
for eg filenames are according to the month and year-----07-18-01.txt ,07-18-02.txt , 08-18-01.txt,-------- (month-year-randomnumbers.
My script should read these filenames , If moth-year matches with current month-year.It should run a.jar
otherwise b.jar
Current month year ,I am giving as
date +%m-%y
If filename contains value of this , It should run a.jarif not ,it should run b.jar
Could you please help me to solve it..
Thanks in Advance.
@Midhun 123
Updated comment/code inside the code
Updated comment/code inside the code
#!/bin/sh
# Better use full path for
# /bin/ls
# /usr/bin/cut
# /bin/date
# Comment: unalias date if alias date found
# Use comments to handle documentation bug fix/enhancement/
#Error can happen due to alias
alias date >/dev/null 2>&1
if [ $? -eq 0 ]
then
unalias date
fi
#Error can happen due to functions
#Comment: Define date function
date ()
{
echo "Use full path for date function"
return 1
}
this_month=$(/bin/date +%m-%y)
echo "this_month = ${this_month}"
#Handle exception using 2>/dev/null
for file in $(/bin/ls [0-9][0-9]-[0-9][0-9]-[0-9][0-9].txt 2>/dev/null)
do
#Validate existens of $file
if [ -f "$file" ]
then
#Cut from first character till fifth character
file_prefix=$(echo $file | /usr/bin/cut -c1-5)
#OR
#Replace .txt to //
#Using \. to represent dot characer since . represent any character
file_prefix=$(echo $file | /bin/sed "s/\.txt//;")
#OR
#Use delimiter as .txt and print first column using /bin/awk
file_prefix=$(echo $file | /bin/awk -F".txt" '{ print $1}')
#OR
#Delete .txt using /usr/bin/tr
file_prefix=$(echo $file | /usr/bin/tr -d ".txt")
if [ "${this_month}" = "${file_prefix}" ]
then
echo "Run $file using a.jar"
else
echo "Run $file using b.jar"
fi
else
echo "/bin/ls -l $file"
/bin/ls -l "$file"
fi
done
If you are going to use full path, then there is no need to define a function that is never used and to unalias. Full path would negate those things.
Also, just removing the .txt from the file is useless. You have too many characters left. The specification quite clearly states that the last set of numbers is not to be compared. I could also post at least another 5 ways to do what was asked, but I thought that one way would be sufficient.
If you want to use full path, then for portability, I suggest doing it this way:
As this is just sample code to show a possible way to solve the issue, comments don't really seem necessary. It should be understood by the developer putting into production and commented to company standards.
Also, just removing the .txt from the file is useless. You have too many characters left. The specification quite clearly states that the last set of numbers is not to be compared. I could also post at least another 5 ways to do what was asked, but I thought that one way would be sufficient.
If you want to use full path, then for portability, I suggest doing it this way:
#!/bin/sh
DATE="/usr/bin/date"
ECHO="/usr/bin/echo"
CUT="/usr/bin/cut"
this_month=`${DATE} +%m-%y`
for file in `ls [0-9][0-9]-[0-9][0-9]-[0-9][0-9].txt`
do
file_prefix=`${ECHO} $file | ${CUT} -c1-5`
if [ "${this_month}" = "${file_prefix}" ] ; then
a.jar $file
else
b.jar $file
fi
done
This way, when you move to another platform and the full paths change, then it is easy to change them in one place rather than have to find it everywhere in the code. Also, on the version of Linux that I am using /bin is just a symbolic link to /usr/bin.As this is just sample code to show a possible way to solve the issue, comments don't really seem necessary. It should be understood by the developer putting into production and commented to company standards.
@Midhun 123
Either accept/assist given answer/answers. If not resolved provide related comment/query you have.
Either accept/assist given answer/answers. If not resolved provide related comment/query you have.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Open in new window