Emergency !! Need to fix this error!

Pls help me with the below script.
Its returning an error No such file or Directory.

#!/bin/ksh
cd /enip/enipapp/cbp/AOC2511201
for file in `cat filename.txt | head -1 | tr -d '\r'`
do

        for i in `cat '$file' | tr -d '\r'`
        do
                echo "-----script start `date` for $file----" >>log.txt
                echo "$i" | mdsql
                echo "-----script end `date` for $file----" >>log.txt
        done
        sleep 60
done

There is an issue with this line:

for i in `cat '$file' | tr -d '\r'`

Because $file is between single quote characters it will never get expanded. It should be between double quote characters.

1 Like

Thanks Methyl. let me try. But the issue now is when I pass through the script, lines are getting splitted like below... but when i cat that file, its in proper format.. Pls help me...

Why do you need "cat"?

Try this:

#!/bin/ksh
cd /enip/enipapp/cbp/AOC2511201
for file in `head -1 filename.txt | tr -d '\r'`
do
	for i in `tr -d '\r' "${file}"`
	do
		echo "-----script start `date` for $file----" >>log.txt
		echo "$i" | mdsql
		echo "-----script end `date` for $file----" >>log.txt
	done
	sleep 60
done

Good reading: Useless Use of Cat Award

About it splitting, we need to know what you have in the files you use in the script!
-----> filename.txt
-----> And the content of the ${file}

I hope it helps!

Thanks a lot filip. filename.txt consists of file names .
$file consists of below

---------- Post updated at 11:28 PM ---------- Previous update was at 11:27 PM ----------

In $file there are 5000 update commands together.

If I understood your post, what is below probably works:

#!/bin/ksh
cd /enip/enipapp/cbp/AOC2511201
for file in `head -1 filename.txt | tr -d '\r'`
do
	while read updateDML
	do
		echo "-----script start `date` for $file----" >>log.txt
		echo "${updateDML}" | mdsql
		echo "-----script end `date` for $file----" >>log.txt
	done < "${file}"
	sleep 60
done

This line will only give you the first line of filename.txt. Are you intending to process every filename in filename.txt ?
Just out of interest, what is "mdsql" ? It seems very inefficient to start a process for every line of SQL. Can the process read the entire program directly.

One simple mistake cost me one hour....

thanks a lot felipe and methyi...... Proud to be a member of this site..........

---------- Post updated at 12:18 AM ---------- Previous update was at 12:09 AM ----------

No I didn't mean the head command... I missed tr command in the highlighted line. mdsql is a tool similar to dbaccess...

Well I'll say it if nobody else will!
You are clearly processing text files which were prepared on a Microsoft platform and contain the MSDOS line terminator <carriage-return><line-feed> rather than a standard unix text file which contains just the line terminator <line-feed>.
You will make your whole life easier if you convert the files from MSDOS format to UNIX format first. There are standard utilities with unix but the name varies with the unix version. Usually "dos2ux" or "dos2unix". See your "man" pages or tell us what Operating System you have.

Ps. I'm still concerned that you will be invoking "mdsql" once for every line of SQL. This appears to be grossly inefficient.

Yes Methyl........ Need to find an alternative to use mdsql only once....... By the way I prepare the text files in unix platform only........