LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   loading args from file with multiple lines (https://www.linuxquestions.org/questions/linux-newbie-8/loading-args-from-file-with-multiple-lines-4175490664/)

tripialos 01-09-2014 04:45 AM

loading args from file with multiple lines
 
Greetings

Happy new year

I have a file "myfile.txt" which has the following content and this content is used as arguments for my script:

10 1203
88 1010
76 765
1 122

I made a script which performs some calculations and i run it as follows:

myscript.sh 10 1203


How can i load each line from the file to the script. In other words, i want the scrip to perform the calculations for each line in the file.


Thanks

lpwevers 01-09-2014 04:56 AM

Hi,

Something like this should do the trick I guess:
Code:

cat myfile.txt | while read F
do
  myscript.sh $F
done


tripialos 01-09-2014 05:05 AM

Quote:

Originally Posted by lpwevers (Post 5094803)
Hi,

Something like this should do the trick I guess:
Code:

cat myfile.txt | while read F
do
  myscript.sh $F
done


Thanks for the replay!

Yes id does the trick :-D

Is it possible to embed this functionality within the script myscript.sh

For example:
Code:

While reading lines from myfile.txt
load the line as arguments and run the calculations
once lines from file finished
exit


lpwevers 01-09-2014 05:15 AM

Quote:

Originally Posted by tripialos (Post 5094808)
Thanks for the replay!

Yes id does the trick :-D

Is it possible to embed this functionality within the script myscript.sh

For example:
Code:

While reading lines from myfile.txt
load the line as arguments and run the calculations
once lines from file finished
exit


Hi,

Glad it worked. You can embed the loop in your script:
Code:

# myscript.sh
ARGFILE=$1

cat $ARGFILE | while read F
do
  ARG1=`echo $F | awk '{ print $1 }'`
  ARG2=`echo $F | awk '{ print $2 }'`
  ...
  <command> $ARG1 $ARG2
  <blah> $ARG1
  ...
done

Now you pass the name of the file with the argments on the command line so your call will be something like this:
Code:

./myscript.sh myfile.txt
Then, within the script, the values are stored in $ARG1 and $ARG2 so you can use them in your calculations.

Louis

druuna 01-09-2014 05:25 AM

@tripialos: If there is just one myfile.txt (or alike) you can simplify the above:
Code:

#!/bin/bash

while read FIRST_ITEM SECOND_ITEM
do
  echo "First item is  : $FIRST_ITEM"
  echo "Second item is : $SECOND_ITEM"
  # do something with the current line of input here
done < myfile.txt

The above script can be run as ./scriptname

If you have to be able to use different input files you can do this:
Code:

#!/bin/bash

while read FIRST_ITEM SECOND_ITEM
do
  echo "First item is  : $FIRST_ITEM"
  echo "Second item is : $SECOND_ITEM"
  # do something with the current line of input here
done < $1

The above can be run as ./scriptname input.file

tripialos 01-09-2014 06:56 AM

sorted with both solutions!!!!

Thanks guys!!


All times are GMT -5. The time now is 11:55 PM.