0

I have an input file of this form:

Some text here
Another text here
Something else here

Now I want to write a linux script picks one line at a time from input file and creates a separate file which stores just the line received. After this I want to pass this file to a program (for which I have just binary file). Is it possible to write such a linux script. I am used to programming in C++ I know it is possible there. But I want to know if something like this is possible using linux script. Basically I intend to do the following:

   read inputfile.txt line by line
       store line read in inputFileInside.txt
       ./myprogram paramater1 inputFileInside.txt //run my C++ binary file which I usually run as (root$./myprogram parameter1 inputFileInside.txt)
      sudo sh -c \"sync; echo 3 > /proc/sys/vm/drop_caches\"
 exit when the input file has been read
Alice Everett
  • 375
  • 1
  • 7
  • 15
  • You can break up "reading a file line by line and store the line in a text file" into 1. [reading a file line by line](http://stackoverflow.com/questions/10929453/bash-scripting-read-file-line-by-line), and 2. [writing text to a file](http://stackoverflow.com/questions/11162406/open-and-write-data-on-text-file-by-bash-shell-scripting). These are easy to find existing solutions for individually, and can be combined to do what you want. – that other guy Nov 03 '14 at 06:22
  • It seems like you have an answer, I was wondering though if what you are trying to do it not execute a program based on the input of a line in a file. Is the textfile being created not just a side effect? – Leon Nov 03 '14 at 06:45

2 Answers2

1

you can read line by line like this using for loop

while read x
   do
     echo $x > inputFileInside.txt;
     # do whatever you want
   done < inputfile.txt

this may hep you to loop, $x is line read one by one till it reach end of file

while read x
   do
     echo $x > $2;
     ./myprogram paramater1 
      #your other command
   done < $1;

save the above file as any name like prog.sh, then give execute permission and run ur program with argument

chmod u+x prog.sh
./prog.sh inputfile.txt inputFileInside.txt

here $1 is inputfile.txt and $2 is inputFileInside.txt

Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • Thanks a lot. Actually I dont know how to run the program like root$./myprogram parameter1 inputFileInside.txt and root$sudo sh -c \"sync; echo 3 > /proc/sys/vm/drop_caches\". Can you help me a little with this too – Alice Everett Nov 03 '14 at 06:08
  • 1
    Also I want my program ./myprogram to terminate its run during each loop – Alice Everett Nov 03 '14 at 06:08
  • inputFileInside.txt you want to overwrite – Hackaholic Nov 03 '14 at 06:11
  • -1. Don't use this (`for x in \`cat\``)approach. This will pick one word per iteration, not one line. Check yourself on the terminal. Use `while read x; do ... done < file`. I will undo downvote after you fix this.. Thanks. – anishsane Nov 03 '14 at 06:17
  • There should not be any space between variable, = & the value. `IFS = '\n'` -> `IFS='\n'` & no, it will not fix the split by word problem here. – anishsane Nov 03 '14 at 06:21
  • I am sorry I am getting "-bash: syntax error near unexpected token `>'" error. Please correct me as to where am I going wrong – Alice Everett Nov 03 '14 at 06:24
  • It was easy to add a trivial answer to a trivial question. But it sounded more worth to help a new coder. :-) – anishsane Nov 03 '14 at 06:25
0
while read line; do 

    echo "$line" > inputFileInside.txt

    ./myprogram paramater1 inputFileInside.txt \
    && sudo sh -c \"sync; echo 3 > /proc/sys/vm/drop_caches\"

done < <(cat inputfile.txt)

This will:

  • input the file "inputfile.txt" line-by-line
  • each line will be assigned to $line as it is read
  • echo the line into "inputFileInside.txt" overwriting any previous information in it
  • run "./myprogram ..."
  • then only run "sudo ..." if "./myprogram ..." was successful
  • go to next line.

Make sure there is no white space (spaces) behind the end-of-line backslash (\).

jgshawkey
  • 2,034
  • 1
  • 9
  • 8