Identify full path in argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Identify full path in argument
# 1  
Old 01-26-2015
Question 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:
Code:
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

Last edited by rbatte1; 01-26-2015 at 11:42 AM.. Reason: Removed FONT in CODE block
# 2  
Old 01-26-2015
Quote:
Originally Posted by wbport
I have a small script to send copies of files to another computer used for tests but in the same location:
Code:

 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 "/"

Code:
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:

Code:
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 Smilie )
This User Gave Thanks to Ditto For This Post:
# 3  
Old 01-26-2015
With recent shells' parameter substring expansion
Code:
[ "${FILENAME:0:1}" == "/" ] && echo full path || echo file name

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

Or, as well:
Code:
[[ "$FN" == "${FN##*/}" ]] && echo file name || { [[ "$FN" == "${FN#/}" ]] && echo rel path || echo full path; }

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

# 4  
Old 01-26-2015
Quote:
Originally Posted by RudiC
With recent shells' parameter substring expansion
Code:
[ "${FILENAME:0:1}"

Cute .. learn something new everyday Smilie
I guess I'm done for today ... G'night everyone! Smilie
# 5  
Old 01-26-2015
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.
# 6  
Old 01-26-2015
You can also just use 'realpath':

realpath(1) - Linux man page
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Hardware

How to identify virtual/physic disk path on X4100?

Hello, Currently we have a Oracle X4100 ( Solaris 10 ) server with disk failure. Originally our hardware team thought the disk can be hot-swap, when they stood in front of the server, none of the disk failure LED were on, so now we have no idea which disk is the bad one for replacement. As... (2 Replies)
Discussion started by: sunnychen98
2 Replies

2. Shell Programming and Scripting

Regex to identify a full-stop as a sentence delimiter

Hello, Splitting a sentence using the full-stop/question-mark/exclamation is a common device. Whereas the question-mark / exclamation do not pose too much of a problem; the full-stop as a sentence delimiter raises certain issues because of its varied use: just to name a few. Standard parsers... (9 Replies)
Discussion started by: gimley
9 Replies

3. Programming

Passing full path as argument when it contains variable strings

Hi, In directory "inoutfiles", I have folders fold0001, fold0002 and so on. Every folder has corresponding file file0001.txt, file0002.txt and so on. I want to perform a certain action on multiple files in one go. The cpp file is in the same directory as "inoutfiles". This is my code : ... (1 Reply)
Discussion started by: KidD312
1 Replies

4. UNIX for Advanced & Expert Users

Passing full path as argument when it contains variable strings

Hi, In directory "inoutfiles", I have folders fold0001, fold0002 and so on. Every folder has corresponding file file0001.txt, file0002.txt and so on. I want to perform a certain action on multiple files in one go. The cpp file is in the same directory as "inoutfiles". This is my code : ... (0 Replies)
Discussion started by: KidD312
0 Replies

5. Shell Programming and Scripting

How to extract strings from full path when full path is not fixed

/Path/snowbird9/nrfCompMgrRave1230100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird6/nrfCompMgrRave1220100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird14/nrfCompMgrRave920100920.log.gz:09/20/2010 06:14:51 ERROR Error Message.... (0 Replies)
Discussion started by: Shirisha
0 Replies

6. Shell Programming and Scripting

Retrieve directory path from full file path through sh

Hi, I have a file abcd.txt which has contents in the form of full path file names i.e. $home> vi abcd.txt /a/b/c/r1.txt /q/w/e/r2.txt /z/x/c/r3.txt Now I want to retrieve only the directory path name for each row i.e /a/b/c/ /q/w/e/ How to get the same through shell script?... (7 Replies)
Discussion started by: royzlife
7 Replies

7. Shell Programming and Scripting

list file with full path

This has been bugging me for a while. How can i list file to show full path. /directory/test $ ls file.tst file.tst $ desired output: /directory/test/file.tst (2 Replies)
Discussion started by: ryandegreat25
2 Replies

8. UNIX for Dummies Questions & Answers

Listing files with full path

Hi, I need to store all the files in a directory to a text file with its full path. The example below can explain: ./File1.txt ./File2.txt ./Folder1/File11.txt ./Folder1/File12.txt ./Folder1/Folder11/File111.txt ./Folder2/file21.txt : : The ls -R1 command won't give the result as... (5 Replies)
Discussion started by: r_sethu
5 Replies

9. Shell Programming and Scripting

getting full path from relative path

given a relative path, how do i convert it into a full one. i.e. if i am in /home/polypus and i am given foo/bar then to get a full path i can just concatinate it with pwd, but what if i am given "../mama" how do i programmatically convert: /home/polypus and ../mama into ... (4 Replies)
Discussion started by: polypus
4 Replies

10. Shell Programming and Scripting

getting the path part of an argument

Seems I'm inundating this forum with questions, but anyway: I am writing a script that should accept one and only one argument when called. That argument should designate a file, either with path/filename or just filename. Now to the difficult bit: I want to figure out a way to store... (9 Replies)
Discussion started by: ropers
9 Replies
Login or Register to Ask a Question