New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement readlink -f: a (portable) way to find resources relative to the current script, even if invoked through a symlink #96
Comments
So I think the key to making this portable is the I hadn't used
It might be nice to provide a shorter osh/oil-specific variable for this, but I think it should be defined in terms of this portable snippet (assuming it's correct). |
Sadly Can GNU's |
OK right, it has to dereference and then
which is a mouthful... and starts three processes (used to be 4 before we had the But I think that is the best way to make something portable between bash and osh? I think Does that sound reasonable? |
@andychu So where
Still two processes, but no |
Ah OK, that makes sense. I think I started using the I will change the title since I think implementing
|
If it's going to be a builtin, maybe we should implement a version of |
OK I'm not familiar with
If I do I don't have a strong opinion here. I said |
Well, any script that uses This blog claims that BSD Coreutils are available for Mac OS X, so maybe availability isn't too much of an issue after all? https://www.topbug.net/blog/2013/04/14/install-and-use-gnu-command-line-tools-in-mac-os-x/ I think Oil should definitely provide a built-in way to dereference symlinks, because that seems like a pretty common operation that should be simple; but I suppose it may not be as important for |
Yeah, in general I want to defer to external utilities unless there's a good reason to fold things in. find/xargs is the canonical example because And I think that being able to find resources relative to a script's realpath is a pretty core piece of functionality. Logically it does seem to belong within the shell. You can't "source" properly without that. But there is the issue of confusion with external utilities. We could provide it under yet another name, although I'm not sure that solution is deal either. It could also be opt in somehow, with a The advantage of a separate name is not having to emulate all of minimal special case hack:
|
Hmm. One advantage of providing something with a new name is that none of the names so far are actually "right" in my opinion. What's really happening is that the path is being canonicalized. So maybe creating a builtin with a name based on that would be desirable.
Perhaps on startup the shell could check if
|
Hm actually
So that is the logical way to do it for readlink.
And the user/sys admin would have the option of putting that in their There are many names that are subpoptimal in shell, but |
But you're okay with setting up a fallback when As for the name |
I'm not sure how much to leave up to the "system packager" (e.g. Debian) vs. something builtin. Letting the sys admin decide whether to put I haven't thought about a new name -- we can cross that bridge when we get to it :) OSH doesn't even work on OS X yet (due to my build system hacks), and Oil still doesn't exist :-( |
Oh, oh, I see, that makes sense--so |
Since we more or less decided what OSH should do, I'm tagging this "help wanted". (I've gotten more than one request for small tasks.) |
How much of the interfaces for |
I think it should just be the bare minimum to find resources relative to the current script. So On Linux, nobody would use this because they'll just use coreutils. On Mac, there will be the extra step of If a Mac user wants more flags for Does that make sense? If you're interested in doing it, then you can look for I guess I don't have a great test setup for these new binaries. You could put something in ( |
@BatmanAoD It sounded like you were interested in doing this -- if so, are you still interested? I have a few people looking for tasks so I thought I might suggest this. |
I'm not terribly interested in this particular task; it does seem like a good one to suggest for others. Thanks. |
Addresses issue #96. It uses the Python function os.path.realpath(), which we discovered has different semantics than C's realpath(). Run demo with: $ demo/readlink-demo.sh all Run failing "gold" test with: $ test/gold.sh readlink-case - Changed the gold.sh test framework to add $REPO_ROOT/bin to the $PATH so we can test Oil's readlink against the one on the system. Co-authored-by: Nicolas Hahn <nicolas@stonespring.org>
Suggested by skissane through e-mail:
https://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within
On Linux I use this:
IIRC this doesn't work correctly with symlinks -- you need
readlink
too. Howeverreadlink
isn't portable to Mac OS X.We could either build in something like
readlink
(which isn't too far out of bounds for shell) or provide a simpler, portable solution, e.g.$OIL_SCRIPT_DIR
(and perhaps$OIL_SCRIPT_PATH
)Similar to
$BASH_SOURCE
.The text was updated successfully, but these errors were encountered: