bashunixawksystempwd

awk change working directory within a script


I want change a working directory in an awk script:

  ...
  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)
  ...

Does somebody know why system does no effect in my case?


Solution

  • system will launch a sub-process, and a sub-process cannot alter the working directory of its parent.

    A quick Google search reveals the following section of the GNU AWK documentation: http://www.gnu.org/software/gawk/manual/html_node/Internal-File-Description.html#Internal-File-Description

    It seems to imply that standard AWK does not have a method of changing the working directory. (And in GNU AWK you can do it with chdir as you see.)