Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help with a Kornshell function

Status
Not open for further replies.

ckrieger1

Programmer
Feb 13, 2004
6
US
I am trying to write a Kornshell function that takes a string parameter which represents a filename or directory name. The function checks to see if there are any spaces in the filename or directory name and then replaces the spaces with an underscore. The returned value is a filename or directory name without any spaces. Thanks for any suggestions you may have.
 
Example
Code:
$ f="hello world"
$ echo $f
hello world
$ f=`echo $f | sed 's/ /_/g'`
$ echo $f
hello_world

--
 
use the unix tr (translate) function
newfilename=$(print $filename | tr [' '] ['_'])

pretty sure that will work; don't have access to k-sh at this time.

Sometimes the grass is greener on the other side because there is more manure there - original.
 
The syntax for tr is :

[tt]newfilename=$(print "$filename" | tr ' ' '_')[/tt]

If you want to define a function :
[tt]
function ReplaceSpaces {
echo "$1" | tr ' ' "${2:-_}"
}
newfilename=$(ReplaceSpaces "$filename")
[/tt]



Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top