1834187 Members
2866 Online
110064 Solutions
New Discussion

Shell Help

 
SOLVED
Go to solution
Shannon Petry
Honored Contributor

Shell Help

Is there a way in Korn Shell, to remove file extentions from filenames with internal shell variables like in c-shell? if so, how would I do this?

Scenario. I need to list zip files in a directory of about 1000 files. Shell internals are much faster than piping to awk or sed to get the same thing done.

Thanks!
Shannon
Microsoft. When do you want a virus today?
3 REPLIES 3
James R. Ferguson
Acclaimed Contributor
Solution

Re: Shell Help

Hi Shannon:

See the man pages for 'ksh':

${parameter#pattern}
${parameter##pattern}

${parameter%pattern}
${parameter%%pattern}

For example:

#!/usr/bin/ksh
X=/tmp/myfile.log
Y=${X%%.log}
echo $X
echo $Y
exit 0

...echos:

/tmp/myfile.log
/tmp/myfile

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Shell Help

Hi (again) Shannon:

I should hasten to add that this inbuilt substitution works identically in the Posix shell, too (man 'sh-posix').

Regards!

...JRF...
Shannon Petry
Honored Contributor

Re: Shell Help

Thanks a bunch.
I try to avoid POSIX as I script for HP-UX, SunOS/Solaris, IRIX, and AIX for the same applications and utilities. So, while I think it's grand that HP-UX supports the POSIX shell, it is not globally available, and I need to write things that work everywhere.
Literally the first part of any of my scripts is
OS=`uname`
case $OS in
HP-UX) ;;
SunOS);;
etc...
esac
In the case I define OS specific paths, and redefine awk to nawk, etc... but that is as much as I modify to make things work across the board.

Thanks again!
Shannon
Microsoft. When do you want a virus today?