Double forloop?

how do I do a double forloop or any other loops like this

reads txt file a
read first line

read txt file b
read first line

then run command 

lun map $line1_from_txtA $line1_from_txtB

exit

Please use code tags for all code.
What have you tried so far?

That depends on how you want the files' lines combined; every single file1 line with all file2 lines, file1 line1 with file2 line1, other?

A question for clarification.

Assuming you have two files with 5 records, do you want output that is the first or second?:-

1 1
1 2
1 3
1 4
1 5
2 1
2 2
2 3
:
etc.

or

1 1 
2 2
3 3
4 4
5 5

it will be file1 line1 to file2 line1

while loop or for loop?

I dont know how to approach it

# Open file into FD 5
exec 5<file1
# Open file into FD 6
exec 6<file2

# Read from both files
while read -r LINE1 <&5 && read -r LINE2 <&6
do
        echo "Line1 is $LINE1"
        echo "Line2 is $LINE2"
done

# Close FD 5
exec 5<&-
# Close FD 6
exec 6<&-

This can be further improved if we knew what you were reading -- if you had tokens you needed split etc.

3 Likes

Where could i read into this FD?
Because man [fF][dD] gives me just the floppy-disk tool manpage, and i am very certain its not that.

FD is the abbreviation for file descriptor. fd 0 is stdin, fd 1 is stdout, fd2 is stderr. Others are unused by default. So Corona688 uses fd 5 and 6 to open the files, and then redirects the two read s' stdin to there.
Read e.g. man bash : redirection.
Try - if available - lsof -p$$ to see the open files and fds

1 Like

lol.. and thank you.
For fun i ran lsof on the pid of xorg - because i though bash would be too boring, but now figured i used a /usr/lib64/libdrm_radeon.so.1.0.1 while having a nvidia gpu.

Anyway, it sounds like the numbers of FD could be 'infite' (as in: integer max), but on the other hand, in the use(d range) example here, its always FD numbers less or equal than the available VT's.

So my question is, what is the max number of adress-able file descriptors?
6/7 of which the first two or three are reserverd, or more like 32K'ish?

Without having analyzed the sources: I've seen them up to 255, and in real use up to 74.

1 Like

$LINE1 might always be NULL, but $LINE might be valid... <wink>

And thanks, you have shown me how to close a file descriptor...

1 Like

man bash:"Redirections using file descriptors greater than 9 should be used with care, as they may conflict with file descriptors the shell uses internally."

1 Like

To avoid those conficts, you could use a variable name whose value would be assigned by the shell ( man bash ):

Furthermore, additional restrictions may apply when using the KornShell: the coprocess facility uses FD 4 and 5 per default to handle the inter-process communication.

I hope this helps.

bakunin

It probably needed that library to check if it's a radeon.

VT as in virtual terminal? Not really, that's unrelated, the descriptor numbers are anonymous and independent. Descriptor 0 in your program is not the same as descriptor 0 in another program, and descriptor 0 could mean anything or nothing -- a terminal, a file, a device, a FIFO, a UNIX domain socket, etc, etc, or might not even be open. When a program open()'s something, it's assigned the next available number, but after that a program can use dup2() to re-number it into whatever it pleases. This is what the shell does when you open a file into descriptor 5.

This is how input redirection works, by the way -- arrange for descriptors 0 / 1 / 2 to point somewhere before creating the new process.

$ cat /proc/sys/fs/file-max
295049

$

This is only a theoretical max. This is often limited to something saner per-user in the same way memory and processes are.