Inputting multiple files into one command

I am trying to read 30 files into a command. The first file contains 10 lines and goes into this part of the command as "x"

/tmp/filearrange.sh $x

The group of files (20 files, I call them variable $i) need to be the second argument in the command and they need to be read so that they are paired with the other command.

So each line is read one at a time, example

invfile contents (variable x)     20 files (these files are separate files) ($filex)
inventory-usage-4                  inventory.txt
inventory-usage-3                  shelflist.txt
inventory-usage-4                  stocklist.txt
etc                                           etc,  up to 20 files

So

/tmp/filearrange.sh $x  -add $filex

looks like this (example)

/tmp/filearrange.sh inventory-usage-4 -add inventory.txt
/tmp/filearrange.sh inventory-usage-3 -add shelflist.txt

What I have tried so far is to use paste but it takes only 12 lines. Then I tried filedescriptors, but it appears I would need to use as input a variable to the file descriptor like this:

exec 3<$filex
where filex= `cat /tmp/*.txt`

and I can't get this working.

while read i && read x<&3
do
  /tmp/filearrange.sh "$i" -add -f "$filex"
done < /tmp/ exec 3<$filex

I'm really confused about this in bash. Any help is appreciated.

I don't understand what you're trying to do.

If invfile contains:

inv-1
inv-2

and your "group of files" is files named g1 , g2 , and g3 , what arguments do you want to pass to your bash script and exactly how many times should that script invoke /tmp/filearrange.sh ?

And for each of those invocations, exactly what arguments should be passed to /tmp/filearrange.sh (after variable expansion)?

I need to have each line of the first file become the first argument for the command:

/tmp/filearrange.sh  invfile_line1  -add -f 

The second group of 20 files is the problem. I need to have each one of those files plug into the -add -f portion of the command. Like this:

/tmp/filearrange.sh  line1_of_invfile -add -f inventory.txt 

Then the script iterates like this:

/tmp/filearrange.sh  line2_of_invfile -add -f shelflist.txt

and then iterates again like this:

/tmp/filearrange.sh  line3_of_invfile -add -f stocklist.txt

and so on. So the script should iterate through invfile and then use the .txt files as the input for the -add -f argument in the command.

I have tried read -r but as I need more than one file for the second argument of the command this doesn't work.

Stop saying what you have tried and clearly explain what you want!

You have said that there are 10 lines in the 1st file and there are 20 other files. You have not shown what happens after the 10 lines have been read from the 1st file. The and so on gives no indication of whether the last 10 files are ignored, the 1st 10 files are repeated, or something completely different is desired.

I repeat: In a simplified example, if invfile contains:

inv1
inv2

and you invoke your script with the arguments:

yourscript invfile g1 g2 g3

I'm guessing that you want to invoke:

/tmp/filearrange.sh inv1 -add -f g1
    and
/tmp/filearrange.sh inv2 -add -f g2

but I have no idea what you want to have happen with g3.

With this simplified example, please show us exactly what other invocations of /tmp/filearrange.sh should occur or, if I guessed wrong on the 1st two invocations, show us all of the invocations that your script should produce!

Here is what it should look like:

The invfile line1 means line 1 of invfile, and the g1 means file1, the g2 means file2. So for each line of invfile, there is a corresponding separate file that is the next argument for the command ( the -add -f processes each file).
Let me know if it still doesn't make sense.

/tmp/filearrange.sh invfile line1 -add -f g1
    
/tmp/filearrange.sh invfile line2 -add -f g2

/tmp/filearrange.sh invfile line3 -add -f g3

/tmp/filearrange.sh invfile line4 -add -f g4

/tmp/filearrange.sh invfile line5 -add -f g5

/tmp/filearrange.sh invfile line6 -add -f g6

/tmp/filearrange.sh invfile line7 -add -f g7

/tmp/filearrange.sh invfile line8 -add -f g8

/tmp/filearrange.sh invfile line9 -add -f g9

/tmp/filearrange.sh invfile line10 -add -f g10

/tmp/filearrange.sh invfile line11 -add -f g12

/tmp/filearrange.sh invfile line12 -add -f g13

/tmp/filearrange.sh invfile line13 -add -f g14

It doesn't make sense!

You said there are 10 lines in invfile. But you invoke /tmp/filearrange.sh with 13 of those 10 lines??? Please explain what is supposed to happen!

You said there are 20 files in your group of files (here called g1 through g20 for convenience), but you don't show what happens to g15 through g20 and you don't explain why g11 wasn't used with invfile line 11. Please explain how g* files are paired with lines, what happens if there aren't enough lines, what happens if there aren't enough g* files, and explain (in English) what is supposed to happen to g* files that don't have a matching line in invfile!

And, you have explained that invfile contains the 1st argument to be passed to /tmp/filearrange.sh, but you have yet to explain how the group of 20 files are passed to your script. Are the names of those files given on the command line to your bash script or are they in another file whose name is passed to your bash script? If their names are in a file, what is the name of that file?

I can't help you write a shell script if I don't understand what that script is supposed to do and how the script is going to be called.

from what i could understand your need...check something like this works out for you

#!/bin/bash

count=0
for i in $(cat invfile)
do
  count=$((count+1))
  filex=$(cat /tmp/*.txt  | sed -n "$count"p)
  /tmp/filearrange.sh $i -add -f $filex
done
1 Like

rajamadhavan:

That worked just great! I had something similar but without the counter, it did not work at all. I will have to check how the counter figures into the script!

I have to second Don Cragun in that I don't see what you want to do with files 11 ... 20 when your input file has just 10 lines.
And, the order in which files are retrieved by rajamadhavan's proposal seems undetermined and depends heavily on the filenames given, so there's no predetermined relation between lines and files.
You may want to reconsider your request or at least reformulate it.

1 Like

Agree. Its only under the assumption that the invfile has lesser or equal # of lines than the second set (which seems to be his case).