1

I have the following command which is executed as part of a script executing on a remote box via an ssh connection:

cd /var/spool/some_directory ; tar -cf file_name.tar file_1.txt file_2.txt file_3.txt ; echo $(du -m -- file_name.tar | awk '{print $1}')

I have Centos 7.6 boxes where this executes perfectly. However I also have legacy Centos 6.7 boxes where the following errors are encountered:

du: cannot access `file_name.tar': No such file or directory
tar: echo: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

When I ls the directory I do see the expected file_name.tar file is generated. Running echo $(du -m -- file_name.tar | awk '{print $1}') independently from the above command then works as expected - its like that final command is running before the rest of it is complete.

Is there something I can do here to force these commands to run in the expected order?

  • Quote your arguments. `shellcheck` is your friend when it comes to different shells parsing your commandline differently. – anx Oct 06 '20 at 12:20

1 Answers1

1

The echo $() is basically a no-op. Try

cd /var/spool/some_directory ; tar -cf file_name.tar file_1.txt file_2.txt file_3.txt ; du -m -- file_name.tar | awk '{print $1}
madcap
  • 55
  • 6