Redirecting from a .txt file to an executable file in Linux - linux

I have a text file with all the numbers from 1 to 1000 (numbers.txt) and I have an executable file (ex2-1) that when it gets all the numbers from 1 to 1000 (it gets the numbers as input one by one) it print "Done!".
When you run the file you see:
please insert 1:
If you enter 1 it shows the same but with 2 and if not it will print "wrong input" and close.
I know how to read from a text file line by line:
#!/bin/bash
filename='numbers.txt'
while read line; do
echo "$line" #echo is just to show where the number is being saved
done < $filename
But is there any way to redirect so that instead of being printed to the screen it will go to the executable file?

You can run all these commands in a subshell and then redirect its output through a pipe to a process corresponding to a running instance of the executable file ex2-1:
(filename='numbers.txt'; while read line; do echo "$line"; done < "$filename") | ex2-1
However, as you read the file line by line, you could simply run cat on the file numbers.txt instead:
cat numbers.txt | ex2-1
or even more concise and with just a single process:
ex2-1 < numbers.txt

Related

how to read a file line by line and each line as an argument input to a .exe file and capture the output to another file in linux

I try to write a shell script that reads a .txt file line by line, and each line will as the input to my .exe file and I also want to capture the output of the .exe file and export it to another .txt file. my code likes that, but it doesn't work. when I try input manually like "./caculate.exe "1" the program also do not take 1 as an input, still ask me to input manually again.
#!/bin/bash
while IFS= read -r LINE; do
./caculate.exe "$LINE"
done < data.txt > f.txt
If calculate.exe normally gets its input from standard input, you need to pipe the variable to it, not use an argument.
#!/bin/bash
while IFS= read -r LINE
do
printf "%s\n" "$line" | ./calculate.exe
done < data.txt > f.txt

error reading input file: Key has expired

I am currently making a bash script. The purpose of this script is not important. However, I have a piece of code that is generating an error. The error is as follows:
./script.bs: line 175: read: read error: 0: Key has expired
./script.bs: error reading input file: Key has expired
I have the code below for lines 175-189.
This specific piece of code does the following:
-Reads a txt file, that has a list of targeted files.
-For each targeted file, each line is read. And if that line is contained in $NumbersFile, it will do nothing. If that line is NOT contained in $NumbersFile, it will add that line to NumbersFile.
This general piece of code is working, and added 65810 lines of content to $NumbersFile. It then however got the error I stated above.
I'd like to add that the while loop on line 175 (where the error is happening) is supposed to read about 70'000 lines from the given file.
How do I fix this error so that my script may finish running without a key expired error?
NumbersFile="numbers.txt";
while read line; do
while read gramline; do
has="0";
if grep -Fq -- "$gramline" "$NumbersFile"; then
has="1";
fi
if [ "$has" -eq "0" ]; then
echo "$gramline" >> $NumbersFile;
fi
done < "$line";
done < "targetsfile.txt";
If my comment is accurate, perhaps this might be faster:
{ cat targetsfile.txt; xargs cat < targetsfile.txt; } | sort -u > numbers.txt
Or as clarified:
xargs cat < targetsfile.txt | sort -u > numbers.txt
Notes:
the braces are simply to group the cat and xargs commands so that the combined output can be piped into sort. Documented in the manual at 3.2.4.3 Grouping Commands
The first cat outputs the contents of the "targetsfile.txt" file
the xargs cat < targetsfile.txt construct will execute the cat command for every file listed in the targets file. It's a very concise and efficient way to execute
while IFS= read -r line; do cat "$line"; done < targetsfile.txt

linux reading file line by line and passing to another program

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
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
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 (\).

Using while/read/do to pass the content of file as the argument of a command

I'm really new to Linux scripting. I am sure this is simple, but I cannot figure it out.
As part of a script, I am trying to pass the content of a file as arguments of a command in a script:
while read i
do $COMMAND $i
done < file.lst
I want to pass every line of the file.lst as the argument of the command except the very first line of the file. How to I do this?
EDIT:
Here is the section of the script:
while read i
do cp --recursive --preserve=all $i $DIR
done < $DIR/file.lst
while read -r i
do
"$COMMAND" "$i"
done < <(sed -n '2,$p' file.lst)
This solutions does not use a while so I am not entirely sure if it solves your problem, but based on your code sample. you can do the following
tail -n +2 input | xargs echo
This will read all lines from input starting at line 2 and execute echo using the value of the line
the file input contains:
skip
1
2
3
executing that command gives
1
2
3
Just substitute input for the file you want and echo for the command you want
Add an extra read to consume the first line before the while loop begins.
{
read -r;
while read -r i; do
"$COMMAND" "$i"
done
} < file.lst

Shell script to read two values from file and insert them into a command

I have a text file, say, input.txt and I want to run a command and write the output to another text file, say, output.txt. I need to read values from input.txt, each value is in a line, then I need to insert them in the command then write the result in output.txt file. I tried the following and it works fine with me:
for i in `cat input.txt`; do command -m $i -b 100; echo $i; >> output.txt; done
Now, I need to make some improvements over this but I have little experience in Linux so I need some help.
What I need to do is:
1) Before each command result, I want to insert the value of i separated by comma. For example:
i1,result1
i2,result2
i3,result3
2) I need to change the second fixed value that I used in my command from a fixed value (100) to a value read from input.txt. So, the new input file which contains two values, say, newinput.txt is as the following:
i1,value1
i2,value2
i3,value3
Try this, in bash:
IFS=','
while read i val; do
echo -n "$i,"
command $i $val
done < input.txt > output.txt

Resources