Skip to content

Instantly share code, notes, and snippets.

@mamemomonga
Last active March 31, 2020 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamemomonga/c288c4b6ff4d917be8f8b61a895756ef to your computer and use it in GitHub Desktop.
Save mamemomonga/c288c4b6ff4d917be8f8b61a895756ef to your computer and use it in GitHub Desktop.
basedir

スクリプトの設置された位置から一つ下の階層を参照する

スクリプト内部で相対パスで参照し、かつ開始地のカレントディレクトリが不定な場合に便利である。

  • 01_basedir.sh Bashの例
  • 02_basedir.sh Perlの例
#!/bin/bash
set -eu
BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"
echo "BASEDIR: $BASEDIR"
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use Cwd;
use File::Basename;
our $BASEDIR=Cwd::abs_path(dirname($0)."/../");
say "BASEDIR:$BASEDIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment