![]() ![]() ![]() GNU-based systems have a realpath command line tool, which does exactly this. There are quite a few solutions to this, many of which can be found in this Stack Overflow post. We need a tool that resolves symbolic links. (Remember to delete this symbolic link from /usr/local/bin/ when you are done with this.) The $0 argument points to the symbolic link instead of the actual script and hence dirname $0 points to /usr/local/bin which is not where the resources for our script are. When you enter just script_path_test.sh the shell finds the symbolic link in /usr/local/bin and runs it. This puts our script in the default PATH (for an interactive shell) and we can now execute it without needing to type the path to it: > sudo ln -s ~/Desktop/script_path_test.sh /usr/local/bin/script_path_test.sh To save on typing the path to the script all the time, we add a symbolic link to it to /usr/local/bin. While some of these paths look a little odd, they all return the expected values.īut now imagine we like our script so much, that we use it a lot. Save this script as script_path_test.sh in your Desktop folder. This script shows the values of the current working directory, the $0 argument, and the value returned by dirname $0. Let’s create a script that will help us visualize this: #!/bin/sh So, there are a lot of pieces that are really confusing here. Note: Learn the details about PATH in my new book: “ macOS Terminal and Shell“ ![]() (Unless your current directory happens to be in the PATH.) When you just type my_script.sh the shell will look in the PATH directories, not find your script, and error out. Your script is not in the PATH, but the command contains a slash and tells the shell exactly where to look (in the current directory). my_script.sh to run a script you are working on. This is also the reason you have to type. This is the reason you can type sw_vers instead of having to write /usr/bin/sw_vers. When the shell ‘sees’ a slash / in the command, it will assume this is the path to the script or executable and use that.īut when there is no slash / in the command, the shell will use the directories listed in the PATH environment variable to look for the command. buildPkg.sh or ~/Projects/Tools/my_script.sh. In most situations the syntax used to call your script will be a relative or absolute path to the script. This is all good and it will still find the resources and create the pkg file in its project folder. The $0 argument will be Projects/BoringDesktop/buildBoringDesktopPkg.sh the dirname will return Projects/BoringDesktop/. like > Projects/BoringDesktop/buildBoringDesktopPkg.sh When you launch the build script with a different working directory, i.e. which is the current working directory, but also the parent directory of the script. When we do dirname $0 after launching the script with. This will return the path to the directory containing the item you give it. The dirname command removes the last element from a path. The $0 variable is the first element of the command line as it is entered in to the prompt and contains the command itself. To understand when it might fail, we will have to unravel it from the inside. It will work in most situations, but not all. This is a useful but somewhat tricky line. In that package build script I have the following line: projectfolder=$(dirname "$0") One example is this script from my book ‘ Packaging for Apple Administrators‘ which builds an installer package for a desktop picture from resources in the same folder. Some scripts just use the current working directory, but since you can launch scripts from outside the current working directory, this is quite unreliable. In other words: the script sits in some project folder, and you want to get the path to that project folder. I use this frequently in scripts that build pkgs, because I want the script to access or create files relative to the script or its enclosing folder. In shell scripting, there are many situations where you want to know the path to the script file itself. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |