2

I just bumped into a bug in redis install_server script it has a hardcoded :

DEFAULT_CONFIG="../redis.conf"

so when running this script not from its own folder (such as ./utils/install_server.sh) the script fails to find the conf file.

I'm looking for a way to reference the scripts folder without a dependency on where the script is called from.

I looked into this answer which seems to be the canonical on SO but something is failing for me:

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo $DIR

and I get:

./utils/install_server.sh: 100: ./utils/install_server.sh: Bad substitution /home/myusername/binaries/redis-2.8.3 #where I run the script from..not the folder I need

So I guess I'm doing something wrong, or this isn't the correct answer for me

I know I can add a check if the file exists and a clearer error message to the redis install script, but I rather just make this work.

I'll be glad for ideas, and I'll make a PR to redis to fix this for everyone..

Thanks!

Community
  • 1
  • 1
alonisser
  • 11,542
  • 21
  • 85
  • 139

2 Answers2

2

I do something very similar to what you have posted:

SCRIPT_DIR="$(cd $(dirname $0) && pwd)"

It seems that in your case, something is wrong with BASH_SOURCE. In my approach, I use $0 which always evaluates to the full pathname used to launch the script.

I'm not sure what the problem with BASH_SOURCE is in your script as what you posted works for me. Thus I am just offering an alternate approach.

EJK
  • 12,332
  • 3
  • 38
  • 55
1

Are you running this with bash (i.e. start the script with #!/bin/bash or run it with bash /path/to/script), or plain sh (#!/bin/sh or sh /path/to/script)? Both BASH_SOURCE and arrays ([0]) are bash extensions, and may not be available in a generic shell. In particular, the "Bad substitution" error is one I've seen from trying to use arrays in a shell that doesn't support them.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • The shebang is ```#!/bin/sh``` and I don't think I'm in liberty to change it. a work around? – alonisser Dec 28 '13 at 20:39
  • you were right, it turned out the shebang was sh and not bash – alonisser Dec 28 '13 at 21:39
  • By far the best option is to switch to bash. Using `"$0"` instead of "${BASH_SOURCE[0]}" will *sometimes* work, but it depends on exactly how the script is run. See [BashFAQ #28: "How do I determine the location of my script? I want to read some config files from the same place"](http://mywiki.wooledge.org/BashFAQ/028) for more options, and an explantation of why `$0` is unreliable. – Gordon Davisson Dec 28 '13 at 22:05
  • I checked with the redis guys, no bash there... (the aim for lowest common dependencies) – alonisser Dec 29 '13 at 10:43
  • In that case your options are pretty limited. You could try the `$0` approach, check whether it finds a redis.conf file, and if not either error out or try the $PWD approach (and if that doesn't find a valid file *then* error out). – Gordon Davisson Dec 29 '13 at 18:30