Operating System - HP-UX
1748226 Members
4366 Online
108759 Solutions
New Discussion юеВ

Re: Checking for file type

 
SOLVED
Go to solution
Jeff Crump
Occasional Contributor

Checking for file type

Hi:

I am using a change management system that makes available a variable called $vcsfile (this is the name of the file(s) being moved from location to location) and $vcsfile_suffix (this the file extension of the file(s) being moved from location to location).

I am struggling with how to construct the logic to execute one command versus another based on the file extension.

For example:

if $vcsfile_suffix = .qks
then
qd $vcsfile
if $vcsfile_suffix = .cob
then
sc_cobcomp $vcsfile
...
fi

I see the commands but not exactly sure how to string them all together for it to work ...

Thanks in advance for any help.

Jeff
4 REPLIES 4
Steven E. Protter
Exalted Contributor

Re: Checking for file type

if [ "$vcsfile_suffix" = ".qks" ]
then
qd $vcsfile

if [ "$vcsfile_suffix" = ".cob"
then
sc_cobcomp $vcsfile
...
fi


Note on getting the filessuffix.

Lets say the names are all two part:

something.qks
something.cob

extention = $(ls $filename | awk -F. '{print $2}')
vcsfile_suffix=$extension}

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
c_51
Trusted Contributor
Solution

Re: Checking for file type

don't know exactly which shell,etc your using, but this is a pretty good suggestion

case "$vcsfile_suffix" in
".qks") qd $vcsfile;;
".cob") sc_cobcomp $vcsfile;;
esac

Gordon  Morrison
Trusted Contributor

Re: Checking for file type

Personally, I'd go with the "case" example in this case (:o)
but if you do decide to go with "if", don't forget the "else"!
e.g.

#!/bin/ksh
if [[ $vcsfile_suffix = ".qks" ]]
then
qd $vcsfile

else

if [[ $vcsfile_suffix = ".cob" ]]
then
sc_cobcomp $vcsfile
...
fi
What does this button do?
Peter Godron
Honored Contributor

Re: Checking for file type

Jeff,
not sure this adds anything to the "case" solution but:
basename | cut -d'.' -f2
will get you the suffix of filename ;-)
Regards