0

This is a little funny one, at least to me. I have a file on which I am running awk and cut command in a loop being fed by head command. The command is:

head -n X all.du.K.txt | while read line ; do NON_APP=$(echo ${line} | awk '{print $1}' | cut -d"," -f2) ; APP=$(echo ${line} | awk '{print $2}' | cut -d"," -f2) ; PATH=$(echo ${line} | awk '{print $3}') ; echo ${PATH},${NON_APP},${APP} ; done >> final.csv

If X is 1, it works, but if X is 2 or any other number, I get the following error:

head -n 2 all.du.K.txt | while read line ; do NON_APP=$(echo ${line} | awk '{print $1}' | cut -d"," -f2) ; APP=$(echo ${line} | awk '{print $2}' | cut -d"," -f2) ; PATH=$(echo ${line} | awk '{print $3}') ; echo ${PATH},${NON_APP},${APP} ; done >> final.csv

Command 'cut' is available in the following places

  • /bin/cut
  • /usr/bin/cut The command could not be located because '/bin:/usr/bin' is not included in the PATH environment variable. cut: command not found Command 'awk' is available in the following places
  • /bin/awk
  • /usr/bin/awk The command could not be located because '/bin:/usr/bin' is not included in the PATH environment variable. awk: command not found Command 'awk' is available in the following places
  • /bin/awk
  • /usr/bin/awk The command could not be located because '/usr/bin:/bin' is not included in the PATH environment variable. awk: command not found Command 'cut' is available in the following places
  • /bin/cut
  • /usr/bin/cut The command could not be located because '/usr/bin:/bin' is not included in the PATH environment variable. cut: command not found Command 'awk' is available in the following places
  • /bin/awk
  • /usr/bin/awk The command could not be located because '/usr/bin:/bin' is not included in the PATH environment variable. awk: command not found

My $PATH env is as follows:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

If run the above loop with set -x on, I get the following output, just for your all's reference:

  • head -n 2 all.du.K.txt
  • read line ++ echo /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/e7/e0e72896f2d07d134de6efb2d6541bed7e297e17f010f67f30247abc56d92c6c,5375K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/e7/e0e72896f2d07d134de6efb2d6541bed7e297e17f010f67f30247abc56d92c6c,17086K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/e7/e0e72896f2d07d134de6efb2d6541bed7e297e17f010f67f30247abc56d92c6c ++ awk '{print $1}' ++ cut -d, -f2
  • NON_APP=5375K ++ echo /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/e7/e0e72896f2d07d134de6efb2d6541bed7e297e17f010f67f30247abc56d92c6c,5375K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/e7/e0e72896f2d07d134de6efb2d6541bed7e297e17f010f67f30247abc56d92c6c,17086K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/e7/e0e72896f2d07d134de6efb2d6541bed7e297e17f010f67f30247abc56d92c6c ++ awk '{print $2}' ++ cut -d, -f2
  • APP=17086K ++ echo /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/e7/e0e72896f2d07d134de6efb2d6541bed7e297e17f010f67f30247abc56d92c6c,5375K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/e7/e0e72896f2d07d134de6efb2d6541bed7e297e17f010f67f30247abc56d92c6c,17086K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/e7/e0e72896f2d07d134de6efb2d6541bed7e297e17f010f67f30247abc56d92c6c ++ awk '{print $3}'
  • PATH=/var/opt/gitlab/gitlab-rails/shared/artifacts/e0/e7/e0e72896f2d07d134de6efb2d6541bed7e297e17f010f67f30247abc56d92c6c
  • echo /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/e7/e0e72896f2d07d134de6efb2d6541bed7e297e17f010f67f30247abc56d92c6c,5375K,17086K
  • read line ++ echo /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/38/e03865e0382175da3126eb126534c2d3c9b4cf69483fa8a167d05c070360edc4,145K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/38/e03865e0382175da3126eb126534c2d3c9b4cf69483fa8a167d05c070360edc4,191K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/38/e03865e0382175da3126eb126534c2d3c9b4cf69483fa8a167d05c070360edc4 ++ awk '{print $1}' ++ '[' -x /usr/lib/command-not-found ']' ++ cut -d, -f2 ++ /usr/lib/command-not-found -- awk ++ '[' -x /usr/lib/command-not-found ']' ++ /usr/lib/command-not-found -- cut Command 'cut' is available in the following places
  • /bin/cut
  • /usr/bin/cut The command could not be located because '/usr/bin:/bin' is not included in the PATH environment variable. cut: command not found Command 'awk' is available in the following places
  • /bin/awk
  • /usr/bin/awk The command could not be located because '/usr/bin:/bin' is not included in the PATH environment variable. awk: command not found ++ return 127 ++ return 127
  • NON_APP= ++ echo /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/38/e03865e0382175da3126eb126534c2d3c9b4cf69483fa8a167d05c070360edc4,145K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/38/e03865e0382175da3126eb126534c2d3c9b4cf69483fa8a167d05c070360edc4,191K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/38/e03865e0382175da3126eb126534c2d3c9b4cf69483fa8a167d05c070360edc4 ++ awk '{print $2}' ++ '[' -x /usr/lib/command-not-found ']' ++ /usr/lib/command-not-found -- awk ++ cut -d, -f2 ++ '[' -x /usr/lib/command-not-found ']' ++ /usr/lib/command-not-found -- cut Command 'cut' is available in the following places
  • /bin/cut
  • /usr/bin/cut The command could not be located because '/bin:/usr/bin' is not included in the PATH environment variable. cut: command not found Command 'awk' is available in the following places
  • /bin/awk
  • /usr/bin/awk The command could not be located because '/usr/bin:/bin' is not included in the PATH environment variable. awk: command not found ++ return 127 ++ return 127
  • APP= ++ echo /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/38/e03865e0382175da3126eb126534c2d3c9b4cf69483fa8a167d05c070360edc4,145K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/38/e03865e0382175da3126eb126534c2d3c9b4cf69483fa8a167d05c070360edc4,191K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/38/e03865e0382175da3126eb126534c2d3c9b4cf69483fa8a167d05c070360edc4 ++ awk '{print $3}' ++ '[' -x /usr/lib/command-not-found ']' ++ /usr/lib/command-not-found -- awk Command 'awk' is available in the following places
  • /bin/awk
  • /usr/bin/awk The command could not be located because '/usr/bin:/bin' is not included in the PATH environment variable. awk: command not found ++ return 127
  • PATH=
  • echo ,,
  • read line

Sample from my input file:

/var/opt/gitlab/gitlab-rails/shared/artifacts/e0/e7/e0e72896f2d07d134de6efb2d6541bed7e297e17f010f67f30247abc56d92c6c,5375K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/e7/e0e72896f2d07d134de6efb2d6541bed7e297e17f010f67f30247abc56d92c6c,17086K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/e7/e0e72896f2d07d134de6efb2d6541bed7e297e17f010f67f30247abc56d92c6c /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/38/e03865e0382175da3126eb126534c2d3c9b4cf69483fa8a167d05c070360edc4,145K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/38/e03865e0382175da3126eb126534c2d3c9b4cf69483fa8a167d05c070360edc4,191K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/38/e03865e0382175da3126eb126534c2d3c9b4cf69483fa8a167d05c070360edc4 /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/f2/e0f23d7875aff9c6b4ef20f27e7d38c4e312811ae627319267f566a4dc244636,168K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/f2/e0f23d7875aff9c6b4ef20f27e7d38c4e312811ae627319267f566a4dc244636,555K /var/opt/gitlab/gitlab-rails/shared/artifacts/e0/f2/e0f23d7875aff9c6b4ef20f27e7d38c4e312811ae627319267f566a4dc244636

Any idea what the Fun is happening here? Bash version is "GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)"

1
  • 1
    BTW you don't need all those awk's and cut's; shell can do the whole job more simply head ... | while read non app name; do echo ${non#*,},${app#*,},$name; done >outfile Commented Dec 24, 2024 at 4:55

1 Answer 1

2

You're overwriting $PATH in a loop:

PATH=$(echo ${line} | awk '{print $3}') 

You should use a different name for this variable, for example APP_PATH.

1
  • Ahh, awesomely stupid of me. Thanks for pointing out. Commented Dec 24, 2024 at 0:30

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.