Link to home
Start Free TrialLog in
Avatar of Midhun 123
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
date +%m-%y

Open in new window

If filename contains value of this , It should run a.jar
if not ,it should run b.jar

Could you please help me to solve it..
Thanks in Advance.
Avatar of johnsone
johnsone
Flag of United States of America image

This seems to do it for me
#!/bin/sh

this_month=`date +%m-%y`
echo "this_month = ${this_month}"

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

Open in new window

@Midhun 123
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

Open in new window

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:
#!/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

Open in new window

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.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members 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.