0

I have a file abc.out and I need to read it and export the content to the named pipe. My shell script as below:

#!/bin/ksh -x

cd /home/robert/

if [[ -s abc.out ]]; then
  sleep 2
  for i in `cat abc.out`
  do
    mv -f $i ./abc_archive/x$i
    print `pwd`/abc_archive/x$i > /home/robert/pipe_abc
    sleep 1
  done
fi

The content of abc.out file as below:

2001010000
2001010001
2001010002

But when I run the script with the command sh abc.spt, error prompted as below:

mv: cannot stat ‘2001010000\r’: No such file or directory

How can I fix this issue, many thanks!

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Remove the interfering `\r` symbols ie `CR` from the input file. – underscore_d Jun 01 '20 at 09:28
  • 2
    Your file `abc.out` has dos line endings. You should __not__ read lines with `for i in cat`, there's [how to read a file line by line in bash](https://mywiki.wooledge.org/BashFAQ/001) but the same applies to ksh. – KamilCuk Jun 01 '20 at 09:28
  • I have modified the script as below: if [[ -s bcfin.out ]]; then sleep 2 while IFS= read -r line || [[ -n "$line" ]]; do mv -f $line ./bcf_archive/x$line print `pwd`/bcf_archive/x$line > /home/cdr/robert/test_awsget_oracle/pipe_dvf sleep 1 done < bcfin.out fi but same error occur... – Robert Leung Jun 02 '20 at 08:23

1 Answers1

0

One option would be to pipe output through either sed or tr, to remove carriage returns: sed -e 's/[\r\n]//g' or tr -d '\r' if you only want to remove \r

alfheim
  • 107
  • 5