0

I'm trying to create a shortcut that will run a .sh file deep in some directory without having to navigating to the absolute path.

I have tried using aliases, which did not work as. Using soft/hard links, they did not work too as there are certain dependencies in that directory which the shell script reference to.

Anyone has any idea how to ./ a file without having to traverse down that absolute path?

The alias I have tried is :

alias minecraft='/var/www/owncloud/data/admin/files/Spigot/start.sh'

I have tried the Path variant by adding PATH=/var/www/owncloud/data/admin/files/Spigot:"$PATH" into ~/.bashrc.

Which then brings the message :

Error: Unable to access jarfile spigot.jar

I'm using a cli version of Ubuntu if it helps, so no Nautilus.

muru
  • 197,895
  • 55
  • 485
  • 740
  • Add the absolute path to that directory to PATH environmental variable....BTW whats the problem you are having with alias? – heemayl Feb 25 '15 at 15:38
  • we can not really help much without more information. post your alias. post the commands you ran to make links. post the path to you .sh . Post your $PATH – Panther Feb 25 '15 at 15:40
  • You could run it with the absolute path /full/path/to/my/script.sh entered as command for the link/launcher. Then add a line cd /full/path/to/my/ at the beginning of your script (after #! /bin/bash) to make sure that all relative paths are looked up from the directory the script is located in. – Byte Commander Feb 25 '15 at 15:41
  • 1
    See my answer (wherein alternatives using cd are also given). – muru Feb 25 '15 at 16:12
  • 1
    In a launcher, set the Exec= line to: Exec=sh -c "cd /path/to/scriptfolder&&./script.sh" (Ah, @muru indeed a perfect dupe) – Jacob Vlijm Feb 25 '15 at 16:12
  • Hi I have edited the quesiton to contain more information. – user4985 Feb 25 '15 at 17:17
  • You didn't setup your alias correctly, see my updated answer – kos Feb 25 '15 at 17:46

3 Answers3

1

You didn't setup your alias correctly, you should setup it like so: alias minecraft='sh /var/www/owncloud/data/admin/files/Spigot/start.sh'

kos
  • 35,891
1

You are getting the

Error: Unable to access jarfile spigot.jar

Errormessage because the script are using your bash environment working directory when it is executing. The script is probably executing java -jar spigot.jar where spigot.jar file must reside in the same directory. Since youre scripts environment is pointing at another directory the file cannot be found.

To fix this, edit your /var/www/owncloud/data/admin/files/Spigot/start.sh file and add this line just above the command where it is executing java.

cd "$( dirname "${BASH_SOURCE[0]}" )"

This command changes the current executing directory for the script to the directory it resides.

The result should be something like:

 #!/bin/bash

 cd "$( dirname "${BASH_SOURCE[0]}" )"
 java -jar spigot.jar

Then you can use the alias you mentioned in your question, or probably along with many of the other answers that is here.

Good luck!

stalet
  • 589
  • 4
  • 13
  • This worked for me. I wasted a bunch of hours trying to create a shortcut for a java app. Turned out that the relative paths in run.sh were not being resolved correctly when the app was run using a launcher. – Ejaz Aug 17 '16 at 11:33
0

To get rid of the "./" expression for your script, You must have to set the custom path for your scripts in PATH variable. You can do it either using the export Command ( which will set up the variable under current running environment) or as below (for permanent solution)

To do so; Edit the .bashrc file in your home directory with your favorite Editor

vi .bashrc

at the end of the file you can paste the following code.

#custom variable
export PATH=$PATH:/$HOME/bin:/var/www/owncloud/data/admin/files/Spigot/

Above command sets up the additonal paths for your script file location, in this case your .sh script in Spigot folder.

One thing you must remember while exporting the path is either you have to copy the contents of existing PATH variable into a file or in a text file using other editor you can do echo $PATH >> variables.txt

Reason why I'm saying so is if in case you lost the variable path, you wont be able to run other commands at all with aliases.

Finally: type exec bash

I have tested it hope it works fine for you as well. You should also be able to run the script without typing the full path. It should work as start.sh

Good Luck!

  • Thanks for the answer, however I'm running into the error "Error: Unable to access jarfile spigot.jar". Any idea why this might happen? This occurs even using other methods like symlinks and functions. – user4985 Feb 25 '15 at 17:24
  • looks like the .jar file is a library file and you have to export it to the variable called LD_LIBRARY_PATH, not sure if this is the answer to the question,but give it a try – heavyguidence Feb 25 '15 at 17:27
  • But your original problem should be fixed. – heavyguidence Feb 25 '15 at 17:33