Identify full path in argument

I have a small script to send copies of files to another computer used for tests but in the same location:

pwd=`pwd`
for i in "$@"
do
   echo "rcp -p $i comp-2:$pwd/$i"
   rcp -p $i comp-2:$pwd/$i
   echo "Finished with $i"
done

Is there a way I can check the parameter to see if it is a full path name so it could run an rcp without any "pwd" processing necessary?

TIA

Define "full path" .
is it:

1) starting with a slash "/" ?
then just check first character for a "/"

IAMIN=`pwd`

if [[ `echo $IAMIN | cut -c1` == "/" ]]
then
   echo true
else
   echo false
fi

IAMIN=some_file.txt

if [[ `echo $IAMIN | cut -c1` == "/" ]]
then
   echo true
else
   echo false
fi

2) just a file name, no path:

IAMIN=`pwd`

if [[ `echo $(basename $IAMIN)` == $IAMIN ]]
then
   echo true
else
   echo false
fi

IAMIN=some_file.txt

if [[ `echo $(basename $IAMIN)` == $IAMIN ]]
then
   echo true
else
   echo false
fi

(and there's probably better ways of doing that :slight_smile: )

1 Like

With recent shells' parameter substring expansion

[ "${FILENAME:0:1}" == "/" ] && echo full path || echo file name

---------- Post updated at 16:32 ---------- Previous update was at 16:19 ----------

Or, as well:

[[ "$FN" == "${FN##*/}" ]] && echo file name || { [[ "$FN" == "${FN#/}" ]] && echo rel path || echo full path; }

or

case "$FN" in  "${FN##*/}") echo $FN: file name ;; "${FN#/}") echo $FN: rel path ;; "$FN") echo $FN: full path;; esac

Cute .. learn something new everyday :slight_smile:
I guess I'm done for today ... G'night everyone! :stuck_out_tongue:

Thank you people, that will give me enough to start working on it.

Another version will move everything to a specific directory on the target machine, but I can give that on the first parameter, save it, and shift before going into the loop. It seems simple enough but if I have problems with it, that will be a new thread.

You can also just use 'realpath':

realpath(1) - Linux man page