这对我来说有点搞笑。我有一个文件,我正在循环运行awk和cut命令,由head命令提供。命令是:
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
如果 X 是 1,它可以工作,但如果 X 是 2 或任何其他数字,我就会收到以下错误:
head -n 2 all.du.K.txt | 读取行时;执行 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} ; 完成 >> final.csv
命令“cut”可在以下位置使用
- /bin/cut
- /usr/bin/cut 无法找到命令,因为 '/bin:/usr/bin' 未包含在 PATH 环境变量中。cut:未找到命令 命令“awk”可在以下位置使用
- /bin/awk
- /usr/bin/awk 无法找到命令,因为 '/bin:/usr/bin' 未包含在 PATH 环境变量中。awk:未找到命令 命令“awk”可在以下位置使用
- /bin/awk
- /usr/bin/awk 无法找到命令,因为 '/usr/bin:/bin' 未包含在 PATH 环境变量中。awk:未找到命令 命令“cut”可在以下位置使用
- /bin/cut
- /usr/bin/cut 无法找到命令,因为 '/usr/bin:/bin' 未包含在 PATH 环境变量中。cut:未找到命令 命令“awk”可在以下位置使用
- /bin/awk
- /usr/bin/awk 找不到命令,因为 PATH 环境变量中不包含“/usr/bin:/bin”。awk:未找到命令
我的 $PATH 环境如下:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
如果在set -x on 的情况下运行上述循环,我会得到以下输出,仅供大家参考:
- head -n 2 all.du.K.txt
- 读取行 ++ 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'{打印$ 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'{打印$ 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'{打印$ 3}'
- PATH = / var / opt / gitlab / gitlab-rails /共享/artifacts / e0 / e7 / e0e72896f2d07d134de6efb2d6541bed7e297e17f010f67f30247abc56d92c6c
- 回显/var/opt/gitlab/gitlab-rails/shared/artifacts/e0/e7/e0e72896f2d07d134de6efb2d6541bed7e297e17f010f67f30247abc56d92c6c,5375K,17086K
- 读取行 ++ 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 命令 'cut' 可在以下位置使用
- /bin/cut
- /usr/bin/cut 无法找到命令,因为 '/usr/bin:/bin' 未包含在 PATH 环境变量中。cut:未找到命令 命令“awk”可在以下位置使用
- /bin/awk
- /usr/bin/awk 找不到命令,因为 '/usr/bin:/bin' 未包含在 PATH 环境变量中。awk:未找到命令 ++ 返回 127 ++ 返回 127
- NON_APP = ++ 回显/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 命令'cut'可在以下位置使用
- /bin/cut
- /usr/bin/cut 无法找到命令,因为 '/bin:/usr/bin' 未包含在 PATH 环境变量中。cut:未找到命令 命令“awk”可在以下位置使用
- /bin/awk
- /usr/bin/awk 找不到命令,因为 '/usr/bin:/bin' 未包含在 PATH 环境变量中。awk:未找到命令 ++ 返回 127 ++ 返回 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 命令 'awk' 可在以下位置使用
- /bin/awk
- /usr/bin/awk 找不到命令,因为 PATH 环境变量中不包含“/usr/bin:/bin”。awk:未找到命令 ++ 返回 127
- 路径=
- 回音,,
- 读行
来自我的输入文件的示例:
/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/共享/artifacts/e0/f2/e0f23d7875aff9c6b4ef20f27e7d38c4e312811ae627319267f566a4dc244636
知道这里发生了什么有趣的事情吗? Bash 版本是“GNU bash,版本 5.0.17(1)-release (x86_64-pc-linux-gnu)”
您正在循环中覆盖 $PATH :
您应该为此变量使用不同的名称,例如
APP_PATH
。