shell脚本内,如何知道本文件在操作系统的什么位置
我写的shell,假如叫1.sh在系统中在这个位置/root/test/1.sh,且存在软链接/root/test_____1.sh -> test/1.sh
支持以下几种情况的操作
- /root/test>./1.sh
- /root>./test/1.sh
- />/root/test/1.sh
- /root/test1>../test/1.sh
- /root/test>../test_____1.sh
- /root>./test_____1.sh
- />/root/test_____1.sh
- /root/test1>../test_____1.sh
#!/bin/bash BEGIN_GREEN="\033[42;30m" BEGIN_RED="\033[41;37m" BEGIN_BLINK_RED="\033[5;41;37m" END_COLOR="\033[0m" date=$(date "+%Y.%m.%d_%H:%M:%S") # 为了软链接变成普通文件时也能执行成功: 先提取 OAM_HOME 路径, 再拼接 SHELL_HOME路径 exe_HOME=$(dirname $(readlink -sm $0)) OAM_HOME=$(readlink -sm ${exe_HOME%OAM*}/OAM/) SHELL_HOME=${OAM_HOME}/shell BBU_HOME=$(readlink -sm ${OAM_HOME}/../) REPOSITORY_RUN_HOME=$(readlink -sm ${BBU_HOME}/../) REPOSITORY_PACKAGE_HOME=$(readlink -sm ${REPOSITORY_RUN_HOME}/../repository_package/) # repository_package 可能在首次部署时未创建