

























这是一个创建于 921 天前的主题,其中的信息可能已经有所发展或是发生改变。
Version 1:
getProfile() {
CURRENT_PATH=$(pwd)
oldIFS="$IFS" # Save the old value of IFS
IFS="/" # Set IFS to the new delimiter
# Split the string into an array of substrings
read -ra array <<<"$CURRENT_PATH"
IFS=oldIFS
PROFILE=${array[-2]}
echo "${PROFILE}"
}
Version 2:
get_profile() {
script_dir="$(dirname "$0")"
# https://stackoverflow.com/questions/71224944/how-to-get-the-name-of-the-grandparent-directory
profile=$(basename -- "$(realpath -m -- "$script_dir/..")")
echo "${profile}"
}
1 ysc3839 2023 年 12 月 5 日 via Android2 ,因为不依赖 bash array 特性,不需要先分割再合并,同时印象中 IFS 分割坑很多,个人会尽量避免使用。除非不允许启动新进程,否则选第二种更好。 |
2 james122333 2023 年 12 月 5 日 via Android都很差 第一个稍微好了点 IFS 本身没问题 |
3 james122333 2023 年 12 月 5 日 via Android而且都不知道 get_profile 在做什么 也许更详细会有更好的写法 |
4 james122333 2023 年 12 月 5 日 via Android个人觉得这函数有点鸡肋 |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。