Muddassir Rahman
Muddassir Rahman

Reputation: 956

cannot open pipe too many open files

I am getting error,

fatal: cannot open pipe (Too many open files)

#!/bin/bash
output="Out.txt"
trans="DEBIT_TRANSACTION_"
ls *.txt | while read line
do
 subName="$(cut -d'.' -f1 <<<"$line")"

awk -F"|" -v var="10|" 'NF!=15;  NF==15 && /^[^[:space:]]/{ "echo -n "$6" | tail -c 3" | getline terminalCountry;
    if($6 =="") terminalCountry="IND";
  $1=var$1;$6=$6"|"terminalCountry; print $0;
}' OFS="|" "$line" > /home/lradmin/script/cboiCC/cboicTxnScrip/OUTPUT/"$subName$output"

done

Upvotes: 1

Views: 2764

Answers (2)

Ed Morton
Ed Morton

Reputation: 203995

If this:

"echo -n "$6" | tail -c 3" | getline terminalCountry

were a reasonable thing to do then the syntax for doing it would be:

cmd = "echo -n \047" $6 "\047 | tail -c 3"
terminalCountry = ( (cmd | getline line) > 0 ? line : "IND" )
close(cmd)

but it's not a reasonable thing to do. See http://awk.freeshell.org/AllAboutGetline for everything you need to know about using getline.

In this case you appear to be just trying to get the last 3 chars from $6 and that'd just be:

terminalCountry = substr($6,length($6)-3)

Upvotes: 2

kvantour
kvantour

Reputation: 26491

The issue you are having is that you are not closing your command which you pipe to your getline. You write:

"echo -n "$6" | tail -c 3" | getline terminalCountry

Awk does the following with this:

If the same file name or the same shell command is used with getline more than once during the execution of an awk program, the file is opened (or the command is executed) the first time only. At that time, the first record of input is read from that file or command. The next time the same file or command is used with getline, another record is read from it, and so on.

This implies if you have various $6 which are identical, your command will work only correctly the first time. Furthermore, it will have opened a "file" where the command writes its output too. If you have many many records, it will continuously open files and never close them leading to the error.

For a correct working order, you should close the "file" again. That is to say, you should write:

command="echo -n \047" $6 "\047 | tail -c 3"
command | getline terminalCountry
close(command)

But it feels a bit like overkill here, you might just be interested in:

terminalCountry=substr($6,length($6)-3)

Interesting reads:

Upvotes: 2

Related Questions