【问题标题】:awk change working directory within a scriptawk 在脚本中更改工作目录
【发布时间】:2012-09-01 02:07:52
【问题描述】:

我想在 awk 脚本中更改工作目录:

  ...
  path="./some/path"
  system("pwd") ; \
  current_dir=system("pwd") ; \
  pushd_cmd="pushd " path ; \    # a "cd" doesn't work too
  print pushd_cmd ; \
  system(pushd_cmd) ; \
  system("pwd"); \               # the same directory as before
  type="xml"
  ls_cmd = "ls *." type;         # so I can't find the files. Specifying with "./some/path/*.xml" doesn't work too (without trying to change the working directory)
  ...

有人知道为什么系统在我的情况下不起作用吗?

【问题讨论】:

    标签: bash unix awk system pwd


    【解决方案1】:

    system会启动一个子进程,子进程不能改变其父进程的工作目录。

    快速的 Google 搜索显示 GNU AWK 文档的以下部分:http://www.gnu.org/software/gawk/manual/html_node/Internal-File-Description.html#Internal-File-Description

    这似乎暗示标准 AWK 没有更改工作目录的方法。 (如您所见,在 GNU AWK 中,您可以使用 chdir 来实现。)

    【讨论】: