可以使用$0变量获取当前脚本的路径和名称,然后使用字符串处理函数来截取出目录路径。示例脚本如下:

#!/bin/sh

# get the absolute path and name of the script
SCRIPT=$(readlink -f "$0")

# extract the directory path
DIR=${SCRIPT%/*}

echo "The script is located in $DIR"

这个脚本使用了readlink命令来获取当前脚本的绝对路径和名称,然后使用字符串处理函数${SCRIPT%/*}来截取出目录部分。如果不使用readlink命令,可以使用相对路径来获取脚本的绝对路径。示例脚本如下:

#!/bin/sh

# get the relative path of the script
REL_PATH=$(dirname "$0")

# get the absolute path of the script
DIR=$(cd "$REL_PATH" && pwd)

echo "The script is located in $DIR"

这个脚本使用了dirname命令来获取当前脚本的相对路径,然后使用cd和pwd命令来将相对路径转换为绝对路径


原文地址: https://www.cveoy.top/t/topic/dbih 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录