parsing full path and filename

March 18, 2008 at 10:24:06
Specs: Unix, Unknown

I have a script that is receiving the full path and filename as 1 parm. Example: dir1/dir2/test.txt.
I need to split the path and filename into 2 variables.
dir = dir1/dir2
file = test.txt

Eric


See More: parsing full path and filename

Report •


#1
March 18, 2008 at 11:17:08

One way uses unix commands and one way uses bash/ksh pattern matching operators:


#!/bin/ksh

var=dir1/dir2/test.txt

# use the unix basename command
file=$(basename $var)
echo $file

# use the unix dirname command
dir=$(dirname $var)
echo $dir

# use ksh/bash patterns


# Using ksh pattern-matching operators, this function
# replaces the unix basename command. If a 2nd argument
# exists, strip off the extension.
function my_basename {
typeset v x

v=${1##*/}
x=${2#.} # get rid of the '.'
v=${v%.$x}
echo $v
}


# Using ksh pattern-matching operators, this function
# replaces the unix dirname command.
function my_dirname {
typeset v="$@"

echo ${v%/*}
}


ffile=$(my_basename $var)
echo $ffile


ddir=$(my_dirname $var)
echo $ddir



Report •

#2
March 18, 2008 at 11:35:50

Thanks Nails...

I needed to parse the parm inorder to cd to the directory and then ftp the file.

That worked.

Eric

Eric


Report •

Related Solutions


Ask Question