BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Checking for file type

SOLVED
Go to Solution
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
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
Trusted Contributor

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

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?
Highlighted
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