IO redirection in Linux - linux

I'm executing a c file and writing to the output to a file in the following format.
./test >> foo.csv
12245933 1
46909645052845 2
46909767324372 3
However, when I run it without writing to a file, the output is the following.
./test
12245933 1
46909645052845 2
134517460 3
Note the information i'm dumping is a uint64.
what is wrong? what is the mistake i'm doing?
am i using the wrong io redirection?

Related

redirecting outputs of multiple c file to a single file in terminal

I have a c program. I compiled it using gcc. After running the executable file. I saved the output to a separate file.
$ ./a.out > outputs
Then I compiled another program and ran it. I directed to output to the same output file where it erased the old content and wrote the new content. How do i direct all the outputs to the same file with out erasing the previous content.
output redirection > in the ./a.out > outputs will create a new file(outputs) every time. Instead of that use
./a.out >> outputs
>> will append new data to old one every time.

Some strange output on my command in linux shell

I've written the following in the command:
$ cat /bin/ls > blah
$ cat blah blah blah > bbb
$ chmod u+x bbb
$ ./bbb
And it printed all the file names in the current working directory.
My question is why? and why not 3 times?
Because the Linux executable file format (ELF) is not a script that you can copy-paste three times in a row to get the same result. To be more precise, the header contains a single entry point (think of it as the address of where int main() has been stored), which is where the instructions are read from. Once you reach the final return 0; or whatever, the program stops, even if there is more (nicely structured) binary garbage following in the binary file.
TL;DR: Don't forget - /bin/ls is a compiled binary and not a shell script.

How to write a Linux script to run multiple files from a single executable?

I have an executable which works on a file & produces another file. I need to provide the first entry of the file as an argument of the executable. Suppose the executable name is "myexec" and I am trying to run a file "myfile.extension"
"myfile.extension" has a format like this:
7 4 9 1 4 11 9 2 33 4 7 1 22 4 55 ...
While running the executable, I have to type the following:
myexec 7 myfile.extension
and it produces a file named myfile.extension.7
My question is that how can I write a script that will do this for a bunch of files in a directory?
Here's a bash script that you can execute in the directory with the files. It assumes the first word in the file is the argument:
for f in *
do
i=$(awk 'NR==1{print $1;exit}' $f)
myexec $i myfile.extension
done
Edit: Using awk instead of cut | head. Mentioned by brianadams in the comments.

how to redirect `cat` to simulate user input in linux

I began a project. In the instruction, it is written that we could test our program with this command line :
cat test.txt > test.py
But I have no output.
As I understood, it is supposed to give me an output.
test.txt file looks like :
1
3
4
2
5
6
7
1
1
8
9
3
4
5
1
-1
And the test.py file looks like :
for i in range(16):
var=raw_input("type something : ")
print var
I was excepting this command line to redirect the content of the test.txt file to the test.py file while it was running.
I have already read the documentation about the cat command.
Could you help me please ?
In other words, how the cat command is supposed to simulate the user ? I think I have to change something in my python file.
Thank in advance,
Mff
The problem here is that you want cat test.txt | test.py rather than >. | sends the output of one command (cat test.txt) to the input of the other (test.py) whereas > sends the output to a file (which probably means you've overwritten test.py with the contents of test.txt).

Shell script to call external program which has user-interface

I have an external program, say a.out, which while running asks for an input parameter, i.e.,
./a.out
Please select either 1 or 2:
this will do something
this will do something else
Then when I enter '1', it will do its job. I don't have the code itself but just binary so can't change it.
I want to write a shell script which runs a.out and also inserts '1' in.
I tried many things including silly things like:
./a.out 1
./a.out << 1
./a.out < 1
etc.
but don't work.
Could you please let me know if there is any way to write such as shell script?
Thanks,
dbm368
I think you just need a pipe. For example:
echo 1 | ./a.out
In general terms a pipe takes whatever the program on the left writes to stdout and redirects to the stdin of the program on the right.

Resources