- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Expand a path passed to a script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-19-2006 06:47 AM
тАО01-19-2006 06:47 AM
For example, if the current directory is "/database/app" and the user passes "../bin", I need to expand it to "/database/bin". Or if the current directory is "/database/app" and the user passes "../bin/myfile.txt" I need it to expand to "/database/bin/myfile.txt".
I've come up with a way to brute force the result but I have to believe there's an easier way. I've included my brute force method in an attachment.
Any help would be appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-19-2006 06:55 AM
тАО01-19-2006 06:55 AM
Re: Expand a path passed to a script
If you need to use expanded path in the script, use, if the path is the first parameter:
PATH=$PATH:$PWD/$1
If you want to expand path for your interactive shell, run
. script dirname
after the shell prompt.
The script should contain the same string
HTH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-19-2006 07:03 AM
тАО01-19-2006 07:03 AM
Re: Expand a path passed to a script
I don't understand whell what you need to do, because if are in /database/app and you use the ../bin or replace by /database/bin, you have the same results. I don't see the difference.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-19-2006 07:26 AM
тАО01-19-2006 07:26 AM
Re: Expand a path passed to a script
Ivan,
Apparently my original explanation was not clear. When I stated that I need to be able to expand a path, I did not mean PATH, I was simply referring to any combination of typed directories the user may pass to a script, which is why I included examples.
As to the reason this is required; while you are correct that the examples I gave are equivalent within the shell, the relative paths have no meaning to another user viewing the results unless they know where the script was run from in the first place. This particular script can be run from anywhere on the system and the results have to indicate absolute paths in order to make any sense.
If that were the only issue, simply reporting the starting directory would reoslve the problem. However, absolute paths are a must when the results have to be sortable in a spreadsheet by path. Attempting to sort relative paths and make any sense of them is impossible.
I hope this helps clarify what I'm looking for.
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-19-2006 07:47 AM
тАО01-19-2006 07:47 AM
Re: Expand a path passed to a script
APP_PATH=/database/app
Then, in your script, always use:
$APP_PATH/$1
So, if APP_PATH is /database/app, and the user passes ../bin, doest matter where he is, he will go to
/database/app/../bin -> /database/bin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-19-2006 07:58 AM
тАО01-19-2006 07:58 AM
Re: Expand a path passed to a script
# cat getpath
#!/usr/bin/sh
pwd
echo ${1}
WHERE=`perl -le 'use Cwd qw(realpath);print realpath $ARGV[0];' $1`
echo ${WHERE}
For example:
# cd /var/tmp/dummydir && ./getpath ../bin
/var/tmp/dummydir
../bin
/var/tmp/bin
Does that help? This is based on the realpath() C library function.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-19-2006 07:59 AM
тАО01-19-2006 07:59 AM
Re: Expand a path passed to a script
Again, the issue isn't manipulating directories within the shell, it's being able to report the absolute path, even if the user specified a relative one.
The answer is straight-forward if the user actually passes a directory but if they pass a filename, it gets much trickier as you can see from the brute force method I uploaded.
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-19-2006 08:05 AM
тАО01-19-2006 08:05 AM
Re: Expand a path passed to a script
Your answer looks like it's on the right track but I get the following error when i attempt to run it:
Can't locate Cwd.pm in @INC (@INC contains: /opt/perl5/lib/5.00502/PA-RISC1.1 /opt/perl5/lib/5.00502 /opt/perl5/lib/site_perl/5.005/PA-RISC1.1 /opt/perl5/lib/site_perl/5.005 .) at -e line 1.
BEGIN failed--compilation aborted at -e line 1.
Any thoughts?
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-19-2006 08:08 AM
тАО01-19-2006 08:08 AM
Re: Expand a path passed to a script
Just one simple idea.
Is the commands "basename" and "dirname" of any help ?
dirname return the directory part of a path and, basename the filename.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-19-2006 08:11 AM
тАО01-19-2006 08:11 AM
Re: Expand a path passed to a script
It appears that the 'Cwd' module required isn't in your rather old version of perl.
You could download the module from CPAN:
http://search.cpan.org/~kwilliams/PathTools-3.15/Cwd.pm
...or better, yet, upgrade your version of perl:
http://h20293.www2.hp.com/portal/swdepot/displayProductInfo.do?productNumber=PERL
Regards!
...JRF...