wbport
January 26, 2015, 9:50am
1
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
Ditto
January 26, 2015, 10:09am
2
wbport:
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 )
1 Like
RudiC
January 26, 2015, 10:32am
3
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
Ditto
January 26, 2015, 11:22am
4
Cute .. learn something new everyday
I guess I'm done for today ... G'night everyone!
wbport
January 26, 2015, 12:59pm
5
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