How To Get The String Right After the Directory Name

i have this basic line of code that accepts for an input parameter. Example of input string is "/u02/app/ccalloc/dev/out/*.txt", a directory name with a wild card filename. my code below can get the directory name by using the `dirname $p1` command. when i attempted to use the `basename $p1` to get the filename with wild card character it failed.

$ sh sample.sh "/u02/app/ccalloc/dev/out/*.txt"
Usage: basename String [Suffix]
the directory is /u02/app/ccalloc/dev/out

the file string is

$

below is the code

#!/bin/sh

p1=$1
pdir=`dirname $p1`
pfile=`basename $p1`
echo "the directory is $pdir\n"
echo "the file string is $pfile\n"

please help. thanks.

Do you have more than one .txt file in that directory?

By quoting this "/u02/app/ccalloc/dev/out/*.txt" all text files will become $1 to the script. Basename can only handle one file at a time.

Either unquote the argument so basename can use the first one, or use a loop to process all of the .txt files:

for FILE in $@; do
  basename "$FILE"
done

great that works. i need to add a second parameter when i added a second parameter it appears that it is also including the second parameter string.

$ sh sample.sh "/u02/app/ccalloc/dev/out/*.txt" "/export/home/ccalftdv/data"
EATVDAILY02132008.txt
EATVDAILY03292007.txt
EATVDAILY04082008.txt
EATVDAILY05012008.txt
EATVDAILY05282008.txt
EATVDAILY06052007.txt
EATVDAILY06062007.txt
EATVDAILY06192009.txt
EATVDAILY06222009.txt
EATVDAILY06242009.txt
EATVDAILY07132009.txt
EATVDAILY07142009.txt
EATVDAILY07152009.txt
EATVDAILY07162009.txt
EATVDAILY10212009.txt
PCARDDAILY07102009.txt
citicc_pre_pack.txt
data
$

the last display data is coming from the second parameter "/export/home/ccalftdv/data". how do i exclude that? thanks.

In the for-loop, change $@ to $1.

$2 is "/export/home/ccalftdv/data"

wonderful that works. many thanks.