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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find and separate dirname using awk

Status
Not open for further replies.

B0rderLine

Technical User
Jun 1, 2003
5
AU
Hi first timer here...Can anyone help with script below. Seems to work okay, but problem arises if user enters full path of file .. mv will not work.
Apparently I have to use awk or something to determime path component of $1

#! /bin/sh

# Prog that moves file specified by user
# File is moved to "trash" folder where it can be
# recovered from if required# To use for adding removed file
# details to tra.db

TRASH="${HOME}/trash"

if [ $# -ne 1 ]
then
echo "name of file: \c"
read fname

if [ -z "$fname" ]
then
echo " $No file ... Quitting.... "
exit 1
else
echo "checking $fname"
fi
else
fname="$1"
fi

fPID="$fname$$"

# Check file

if [ -f $fname -a -w $fname ]
then
echo " Preparing to remove"
else
echo " No file: $fname "
echo " Check permission and file exists"
exit 1
fi


if [ ! -d $TRASH] # Check "trash" exists
then
mkdir $TRASH
fi

if mv -i $fname ${TRASH}/${fPID}
then
echo "moved file: $fname"
echo ${fname}\ $fPID >> ${HOME}/tra.db
exit 0
else
echo "Error: Can not mv $fname .. Exit and try again"
exit 2
fi

 
Try this:
Code:
fPID=`basename "$fname"`$$
Are you sure you want to escape the $ character when writing
to tra.db file ?

Hope This Help
PH.
 
Thanks PHV
Unfortunately I'm not able to use 'basename' on the system.
the escape character when writing to tra.db is actually to insert a space between $fname & $fPID... \ $fPID

I've been advised that the problem can be worked using awk, but I'm really lost when I try it.
 
fPID=`echo "$fname" | awk -F/ '{print $(NF)}'`

Hope This Help
PH.
 
Tks PH
I'll give it a go.. and post again to let u know.
 
PH you're my hero !! Thankyou thankyou thankyou !!
 
Just an extra tip... try "basename".

No need for awk.
 
Thanks for the tip MaxKnight, but as I said in an earlier post.. I don't have that command available to me on the sys I'm using.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top