gpt4 book ai didi

linux - 字符串操作 : remove last component of path

转载 作者:太空狗 更新时间:2023-10-29 11:08:36 25 4
gpt4 key购买 nike

这是我的代码

real_path="/u1/h7/user/469/code/hw4/b.sh"
while [ "$real_path" != "/" ]
do
echo $real_path
real_path=${real_path%/*}
done
echo $real_path #just to print the root '/'

我希望我的脚本打印以下行,但 real_path=${real_path%/*} 似乎不起作用,有任何建议或其他方式来实现吗?

/u1/h7/user/469/code/hw4/b.sh
/u1/h7/user/469/code/hw4
/u1/h7/user/469/code
/u1/h7/user/469
/u1/h7/user
/u1/h7
/u1
/

最佳答案

您可以使用目录名:

#! /bin/bash

real_path="/u1/h7/user/469/code/hw4/b.sh"
while [ "$real_path" != "/" ];do
echo "$real_path"
real_path=$(dirname "$real_path")
done
echo "$real_path"

输出:

/u1/h7/user/469/code/hw4/b.sh
/u1/h7/user/469/code/hw4
/u1/h7/user/469/code
/u1/h7/user/469
/u1/h7/user
/u1/h7
/u1
/

关于linux - 字符串操作 : remove last component of path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28462704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com