更改以编程方式调用bash的当前工作目录

时间:2013-09-03 04:15:23

标签: linux macos bash shell

我有一个文件,其中包含格式为

的键/值对
TAG PATH

。现在,我想编写一个程序,从命令行中读取 TAG ,并将调用 bash 的当前工作目录更改为相应的 PATH

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您可能会考虑类似的事情(可能在您的bash function中)

while read tagname dirname ; do
   pushd $dirname ;
   dosomethingwith $tagname
   popd
done < yourinputfile.txt

请参阅this question(关于bash中的read)并阅读advanced bash scripting guide

GNU awk可能是更好的工具。

请注意,您只能使用chdir(2)系统调用(由cdpushdpopd bash builtins调用来更改当前进程的当前目录)。无法更改某些其他进程的目录(例如,父进程或调用其中一个进程)。 pushdpopd内置版也会更新bash directory stack

无法更改调用shell的当前目录(在终端中运行的父进程)。但是你可以在那里定义自己的bash函数。阅读Advanced Linux Programming以了解有关Unix processes

的更多信息
相关问题