【问题标题】:Unix Script Looping cat file while read line "No such file or directory" errorUnix Script Looping cat file while read line "No such file or directory" 错误
【发布时间】:2016-04-22 14:28:44
【问题描述】:

我有一个读取参数文件的脚本,并将对该脚本中每一行的值执行一些操作。我的输入文件有空格作为分隔符。

奇怪的是,它适用于旧版本的 Linux,但不适用于新版本。

#! /bin/ksh
su root "cat /var/opt/OV/tmp/HPOV_gg.log" | while read Line
do
   echo "${Line}"
done

错误:bash: cat /var/opt/OV/tmp/HPOV_gg.log: 没有这样的文件或 目录

该错误与新的 Linux 版本有关,以不同的方式解析 cat 命令。

我该如何解决这个问题?或者我可以重写我的脚本让它在这个新的 Linux 版本上运行。

【问题讨论】:

  • 您能否编辑您的问题以包含su root "ls -l /var/opt/OV/tmp/HPOV_gg.log" 的输出?
  • [elp000161(root):/root] su root "ls -l /var/opt/OV/tmp/HPOV_gg.log" bash: ls -l /var/opt/OV/tmp/ HPOV_gg.log:没有这样的文件或目录这是问题所在。我像 John 建议的那样添加了一个 -c 参数,现在它可以工作了。

标签: linux shell ksh su


【解决方案1】:

最好使用 sudo 以 root 身份执行命令。不需要引号,并且可以通过其配置文件以细粒度的方式控制 sudo 访问。

sudo cat /var/opt/OV/tmp/HPOV_gg.log | while ...

您知道,您可以通过编写 su root -c "cat file" 来修复您的 su 命令。命令需要通过-c 选项传递。但是,sudo 更好。

【讨论】:

    猜你喜欢
    • 2016-11-01
    • 1970-01-01
    • 2023-04-03
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-16
    • 1970-01-01
    相关资源
    最近更新 更多