0

When writing a script, I'd like to know where lies the current script, in order to locate other files.

With a regular Scala script, I know how to do so, but with an Ammonite script I don't.

david.perez
  • 6,090
  • 4
  • 34
  • 57

2 Answers2

2

it's been a long time since the question but you can get directory for script file like this in current Ammonite:

val src: String = sourcecode.File()
val rootDir: Path = os.Path(src) / os.up
Windymelt
  • 139
  • 8
0

Instead of the standard bang line I normally use:

#!/usr/bin/env amm

I changed my script to:

#!/bin/bash
    exec amm "$0" `dirname "$0"` "$@"
!#

@main
def main(dir: String) {
    print(dir)
}

The dir argument receives the path where the script lies. It can be absolute or relative.

If we always desire an absolute path:

#!/bin/bash
    exec amm "$0" $(cd `dirname "${BASH_SOURCE[0]}"` && pwd) "$@"
!#
david.perez
  • 6,090
  • 4
  • 34
  • 57