Re: Checking for file type (147 Views)
Reply
Occasional Contributor
Jeff Crump
Posts: 7
Registered: ‎02-03-2005
Message 1 of 5 (147 Views)
Accepted Solution

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
Exalted Contributor
Steven E. Protter
Posts: 33,806
Registered: ‎08-15-2002
Message 2 of 5 (147 Views)

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
Trusted Contributor
c_51
Posts: 60
Registered: ‎10-05-2004
Message 3 of 5 (147 Views)

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

Trusted Contributor
Gordon  Morrison
Posts: 145
Registered: ‎07-28-2004
Message 4 of 5 (147 Views)

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?
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 5 of 5 (147 Views)

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
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.