Search code examples
linuxshellkshsu

Unix Script Looping cat file while read line "No such file or directory" error


I have a script the reads a parameter file, and is going to do some actions with the values of each line in that script. My input file has spaces as separators.

The weird thing is, it works on an old version of Linux but not on a newer version.

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

Error: bash: cat /var/opt/OV/tmp/HPOV_gg.log: No such file or directory

The error has obv something to do with the new Linux version, parsing the cat command in a different way.

How can I fix this? Or can I rewrite my script to let it work on this new Linux version.


Solution

  • It's better to use sudo to execute commands as root. No quotes are needed, and sudo access can be controlled in a fine grained manner via its configuration file.

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

    Just so you know, you could fix your su command by writing su root -c "cat file". Commands need to be passed via the -c option. But still, sudo is better.