📚 While running a bash script using a hard symbolic link, how can it get the absolute path of its executing script?
💡 Newskategorie: Linux Tipps
🔗 Quelle: reddit.com
I created a compose.sh script in the /var/www/vhosts/compose2 directory, in which composer's composer.phar is installed. Then in my /var/www/vhosts/symfony5/project directory I created a hard symbolic link to this composer.sh script file in the /var/www/vhosts/compose2 directory. In the script file I want to get the absolute path of the script's symbolic link so I can execute composer.phar without hardcoding its absolute path into the script.
I found that $0 has ./compose.sh, which doesn't help. And the ls -la command doesn't seem to have any options that expand the compose.sh link to /var/www/vhosts/compose2/composer.sh when the ls -la command is run from my symfony5/project directory where it only sees the symbolically linked composer.sh script file. I found -H, -L, and a few --... related options, but these all show .composer.sh when the ls command is run is run from the symfony5/project directory.
Just a couple of things related to bash the compose.sh script:
I used to run the compose.phar command dirctly using a relative path, ../../compose2/composer.phar, but this complicated things a bit because most documentation assumes you've got the composer's 'home' directory in PATH, which I don't, nor want. So I have to remember to use either the relative path or absolute path to the compose2 directory, and that when the docs 'say' composer, I need to use composer.phar.
However, with my new composer.sh symbolically linked file and the composer.sh script, I can run composer from the project directory. To make all of this look even more like what's in the docs, I can take this a step closer by using an alias to change composer without the path or the sh extension to composer.sh and then execute my symbolically linked script. Yes, I realise that this last part could be done by just naming the symbolic link as compose, but I like seeing the sh extension on the file name to remind me that the link is to a script file. As to the alias, when or if I use that, I would use it to substitute the word composer typed in my commandline to composer.sh without any path, that way my use of a script to run composer is almost invisible, and the only part of all this that needs to 'know' the actual path of the composer.sh file in the compose2 directory, and so composer.phar PHP script, is the symbolic link in each project directory where composet is used.
Thanks.
[link] [comments] ...