【问题标题】:Bash Script - Command not recognized in while loop when getting input from file and parsing in itBash 脚本 - 从文件获取输入并在其中解析时,while 循环中无法识别命令
【发布时间】:2023-01-12 17:06:12
【问题描述】:

我从文件中获取输入并将其解析为 2 个变量。但是当我运行命令时,我猜空间或 smt else 有问题。该命令在我手动运行时有效。

我检查了很多条目,但找不到方法。可能是什么问题。

while read p; do
    echo "$p"
    CRT= echo -n "$p" | awk -F '/' '{print $6}'
    echo -n "$CRT"
    kubectl cp ns-mv/gen-0:$p /tmp/$CRT
done < test.txt

这是 text.txt

[master]$ cat test.txt
/opt/gen/AughGEN/OutCSY/CRT-1154.trt
[master]$

所以基本上我想要的是

kubectl cp ns-mv/gen-0:/opt/gen/AughGEN/OutCSY/CRT-1154.trt /tmp/CRT-1154.trt作为命令

输出

[master]$ bash test.sh
/opt/gen/AughGEN/OutCSY/CRT-1154.trt
CRT-1154.trt
tar: /opt/gen/AughGEN/OutCSY/CRT-1154.trt\r: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

【问题讨论】:

    标签: linux bash


    【解决方案1】:

    该命令不识别空格,您可以使用双引号而不是单引号将 awk 命令括起来,例如:

    while read p; do
        echo "$p"
        CRT=$(echo -n "$p" | awk -F '/' '{print $6}')
        echo -n "$CRT"
        kubectl cp ns-mv/gen-0:$p /tmp/$CRT
    done < test.txt
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-04
      • 2019-12-19
      • 2015-09-10
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      相关资源
      最近更新 更多