shell脚本,在文件系统中去N个文件夹 [英] Shell Script that goes up N folders in the file system
问题描述
我刚刚发现的this非常有用的shell脚本在这里SO但遗憾的是它不工作在Mac OS X 10.5。
I just found this very usefull shell script here on SO but unfortunately it's not working on Mac OS X 10.5.
这是有问题的脚本(复制它的便利):
This is the script in question(copied it for convenience):
#!/bin/bash
LIMIT=$1
P=$PWD
for ((i=1; i <= LIMIT; i++))
do
P=$P/..
done
cd $P
我试图回声$ P
的尽头,它的返回了正确的道路,但仍 CD $ P
似乎并不奏效。
I tried to echo $P
at the very end and it's returning the right path, but still cd $P
doesn't seem to be working.
于是,我就手动输入 P =一些/路径
和 CD $ P
终端和它的工作。
So I tried to manually enter P="some/path"
and cd $P
in the terminal and it worked.
我不知道为什么在同一命令中没有脚本工作。难道是一个安全的事情吗?
I don't get why the same command isn't working in the script. Could it be a security thing?
有什么建议?
推荐答案
我已经在Linux上同样的问题,其实,如果我的理解正确的是什么,我经过一番搜索发现,这是发生了什么:
I've had the same issue on Linux, actually, if I understood correctly what I've found after some searching, this is what happens:
该命令在子shell中推出,并且在子shell路径得到改变,你看不到变化,因为当脚本完成,然后再回到起始(父)的外壳。
The command is launched in a subshell, and in that subshell the path gets changed, you don't see the change because when the script finishes you get back to the starting (parent) shell.
我解决了这个通过把有用的脚本在我的.bashrc作为函数,像这样的:
I solved this by putting that useful script in my .bashrc as a function, like this:
up(){
#code goes here
}
另一个选择是每次启动它的时间源脚本,但我preFER第一位的。
Another option is to source the script every time you launch it but I prefer the first one.
这篇关于shell脚本,在文件系统中去N个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!