Comment puis-je obtenir le répertoire où un script Bash est situé dans le script lui-même

Mots clés : bashdirectorybash

meilleur 5 Réponses Comment puis-je obtenir le répertoire où un script Bash est situé dans le script lui-même

vote vote

90

#!/usr/bin/env bash  SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]:-$0}"; )" &> /dev/null && pwd 2> /dev/null; )"; 
#!/usr/bin/env bash  SOURCE="${BASH_SOURCE[0]:-$0}"; while [ -L "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink   DIR="$( cd -P "$( dirname -- "$SOURCE"; )" &> /dev/null && pwd 2> /dev/null; )";   SOURCE="$( readlink -- "$SOURCE"; )";   [[ $SOURCE != /* ]] && SOURCE="${DIR}/${SOURCE}"; # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located done DIR="$( cd -P "$( dirname -- "$SOURCE"; )" &> /dev/null && pwd 2> /dev/null; )"; 
#!/usr/bin/env bash  SOURCE="${BASH_SOURCE[0]:-$0}"; while [ -L "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink   TARGET="$( readlink -- "$SOURCE"; )";   if [[ $TARGET == /* ]]; then     echo "SOURCE '${SOURCE}' is an absolute symlink to '${TARGET}'";     SOURCE="$TARGET";   else     DIR="$( dirname -- "$SOURCE"; )";     echo "SOURCE '${SOURCE}' is a relative symlink to '${TARGET}' (relative to '${DIR}')";     SOURCE="${DIR}/${TARGET}"; # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located   fi done echo "SOURCE is '${SOURCE}'"; RDIR="$( dirname -- "$SOURCE"; )"; DIR="$( cd -P "$( dirname -- "$SOURCE"; )" &> /dev/null && pwd 2> /dev/null )"; if [ "$DIR" != "$RDIR" ]; then   echo "DIR '${RDIR}' resolves to '${DIR}'"; fi echo "DIR is '${DIR}'"; 
SOURCE './scriptdir.sh' is a relative symlink to 'sym2/scriptdir.sh' (relative to '.') SOURCE is './sym2/scriptdir.sh' DIR './sym2' resolves to '/home/ubuntu/dotfiles/fo fo/real/real1/real2' DIR is '/home/ubuntu/dotfiles/fo fo/real/real1/real2' 
vote vote

88

#!/usr/bin/env bash  echo "The script you are running has basename $( basename -- "$0"; ), dirname $( dirname -- "$0"; )"; echo "The present working directory is $( pwd; )"; 
[matt@server1 ~]$ pwd /home/matt [matt@server1 ~]$ ./test2.sh The script you are running has basename test2.sh, dirname . The present working directory is /home/matt [matt@server1 ~]$ cd /tmp [matt@server1 tmp]$ ~/test2.sh The script you are running has basename test2.sh, dirname /home/matt The present working directory is /tmp 
vote vote

79

dirname -- "$0"; 
dirname -- "$( readlink -f -- "$0"; )"; 
#!/usr/bin/env bash  echo "pwd: `pwd`" echo "\$0: $0" echo "basename: `basename -- "$0"`" echo "dirname: `dirname -- "$0"`" echo "dirname/readlink: $( dirname -- "$( readlink -f -- "$0"; )"; )" 
>>>$ ./whatdir.sh pwd: /Users/phatblat $0: ./whatdir.sh basename: whatdir.sh dirname: . dirname/readlink: /Users/phatblat 
>>>$ /Users/phatblat/whatdir.sh pwd: /Users/phatblat $0: /Users/phatblat/whatdir.sh basename: whatdir.sh dirname: /Users/phatblat dirname/readlink: /Users/phatblat 
>>>$ cd /tmp >>>$ ~/whatdir.sh pwd: /tmp $0: /Users/phatblat/whatdir.sh basename: whatdir.sh dirname: /Users/phatblat dirname/readlink: /Users/phatblat 
>>>$ ln -s ~/whatdir.sh whatdirlink.sh >>>$ ./whatdirlink.sh pwd: /tmp $0: ./whatdirlink.sh basename: whatdirlink.sh dirname: . dirname/readlink: /Users/phatblat 
>>>$ cd /tmp >>>$ . ~/whatdir.sh   pwd: /tmp $0: bash basename: bash dirname: . dirname/readlink: /tmp 
vote vote

63

pushd . > '/dev/null'; SCRIPT_PATH="${BASH_SOURCE[0]:-$0}";  while [ -h "$SCRIPT_PATH" ]; do     cd "$( dirname -- "$SCRIPT_PATH"; )";     SCRIPT_PATH="$( readlink -f -- "$SCRIPT_PATH"; )"; done  cd "$( dirname -- "$SCRIPT_PATH"; )" > '/dev/null'; SCRIPT_PATH="$( pwd; )"; popd  > '/dev/null'; 
pushd . > '/dev/null'; SCRIPT_PATH="${BASH_SOURCE[0]:-$0}";  while [ -h "$SCRIPT_PATH" ]; do     cd "$( dirname -- "$SCRIPT_PATH"; )";     SCRIPT_PATH="$( readlink -f -- "$SCRIPT_PATH"; )"; done  cd "$( dirname -- "$SCRIPT_PATH"; )" > '/dev/null'; SCRIPT_PATH="$( pwd; )"; popd  > '/dev/null'; 
vote vote

58

#!/usr/bin/env bash  scriptdir="$( dirname -- "$BASH_SOURCE"; )"; 

Questions similaires