1

I have a root directory.

In there I have ./bin/

In ./bin/ I have some bash scripts like:

launch_server.sh port=3000

These scripts must be ran from ./bin, that is, the working directory must be:

home/blah/whereever/root/bin

however, I would like to do:

cd home/blah/whereever/root/
start launch_server.sh port=3000

That last line would set the working directory temporarily to ./bin/ and would execute my script found in ./bin and pass that script any arguments such as port=3000

Is this possible? What is the most convenient way I can launch my bin scripts from the root directory?

The reason is the paths need to stay relative, so I can't hardcode anything

jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • why don't you simply do `cd home/blah/whereever/root/bin`? – michelem Jun 23 '15 at 18:51
  • @Michelem because all my other things such as rails, rake etc I run from the root directory. I want to follow that convention. – jmasterx Jun 23 '15 at 18:53
  • I think @Michelem is suggesting putting the `cd` in your script where you want it to change the working directory prior to invoking the next script – Eric Renouf Jun 23 '15 at 18:55
  • `start bin/launch_server.sh port=3000`? – michelem Jun 23 '15 at 18:55
  • Yes @EricRenouf or just start it with the relative path as I suggested, I can't get the problem or probably the code lacks on something we don't know – michelem Jun 23 '15 at 18:57
  • @Michelem Where does start come from, I'm suggesting having a magical launcher script called start. – jmasterx Jun 23 '15 at 18:58
  • It works just find if I do cd ./bin; launch_server.sh port=3000; The point is to create convenience... – jmasterx Jun 23 '15 at 18:59
  • Well, the root is the root of the project don't mind where a developer has it. So if you put the `start`script file in the root and inside do `start bin/launch_server.sh port=3000` it works everywhere – michelem Jun 23 '15 at 19:01
  • @Michelem My issue is I don't have the start script. I need a script that does the function of this start script. – jmasterx Jun 23 '15 at 19:02
  • so do you need to create the `start`script to enter the bin directory and launch `launch_server.sh port=3000` I'm almost confused... – michelem Jun 23 '15 at 19:05
  • @Michelem Yes that's exactly what I need http://stackoverflow.com/questions/16988427/calling-one-bash-script-from-another-script-passing-it-arguments-with-quotes-and – jmasterx Jun 23 '15 at 19:07
  • Just my $0.015, but a program/library architecture that explicitly relies on current working directories and thus makes extensive (exclusive?) use of relative paths to accomplish things is unnecessarily fragile, and will in the future cause grief that would be completely avoidable with well-written software. Software should be designed and written to require making as few assumptions as possible (ideally - none) about the environment in which it is being run. – twalberg Jun 23 '15 at 19:12

3 Answers3

6

Here is a trick by using brackets:

cd home/blah/whereever/root/

(cd ./bin/ && sh launch_server.sh port=3000)

The solution does not change your working directory.

Will
  • 792
  • 1
  • 5
  • 22
1

1st solution:

In your ~/.bashrc:

alias launch_server="home/blah/whereever/root/bin/launch_server.sh"

Apply the changes:

source ~/.bashrc

In your launch_server.sh script, add before any other command:

cd home/blah/whereever/root/bin/

Now, simply call from anywhere:

launch_server port=3000

2nd solution:

The start script you want should probably look like:

#!/bin/bash
cd ./bin
eval "$@"

Now call:

  • bash start launch_server.sh port=3000 or
  • ./start launch_server.sh port=3000
Eugeniu Rosca
  • 5,177
  • 16
  • 45
0

If you are in your home/blah/whereever/root/ Directory you can run your file with

./bin/launch_server.sh port=3000

It's the common way to do so...

Mischa
  • 1,303
  • 12
  • 24
  • Is there any way to get this working like this, but not having to write the ./bin? eg: a launch script? – jmasterx Jun 23 '15 at 19:04
  • Where is the difference in `start launch_server.sh port=3000` and `./bin/launch_server.sh port=3000`? – Mischa Jun 23 '15 at 19:05
  • The start launcher will call cd ./bin to set the environment for the scripts which expect to be launched from ./bin – jmasterx Jun 23 '15 at 19:08
  • Do you have multiple Files in your `{...}/bin` that have to be called this way? Or just the `launch_server.sh` – Mischa Jun 23 '15 at 19:10