0
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

bash > カレントディレクトリとスクリプトのディレクトリ(絶対 / 相対)の取得

Last updated at Posted at 2017-03-24
動作環境
Xeon E5-2620 v4 (8コア) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 とその-devel
mpich.x86_64 3.1-5.el6とその-devel
gcc version 4.4.7 (とgfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.1を使用。
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
Python 3.6.0 on virtualenv

bashにて以下の情報を取得したい。

  • スクリプトのあるディレクトリ (相対パス)
  • スクリプトのあるディレクトリ (絶対パス)
  • 実行ディレクトリ

参考 http://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within

../SCRIPT_DIRCTORY/find_path_170324_exec
# !/usr/bin/env bash

# relative path
$(dirname $0)
# full path
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo $DIR
# current directory
pwd
フォルダ構造
├── DATA_DIRECTORY
└── SCRIPT_DIRCTORY

DATA_DIRECTORYにおいて以下のように実行してみた。

実行
$ bash ../SCRIPT_DIRECTORY/find_path_170324_exec 
../SCRIPT_DIRECTORY
/home/xxx/WORK/BASH_SCRIPT_170324/SCRIPT_DIRECTORY
/home/xxx/WORK/BASH_SCRIPT_170324/DATA_DIRECTORY

full pathの取得については

It will work as long as the last component of the path used to find the script is not a symlink (directory links are OK).

とあるようにsymlinkではうまく動かない。

0
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?