Reputation: 31
I would like to make a .sh executable that inputs a terminal command, and saves the output to a .txt file.
For example, take the output of i2cdump
and save to a file. The terminal commands for this are
i2cdump -r -y 0x0-6 0 0x68
"outputs the specified bytes 0x0-6 to terminal window"
How can I use a .sh executable to do this automatically, and save the output to a file stored in /dir/
?
Some pseudo code I have for myfile.sh
:
#!/bin/bash
output=$(i2cdump -r -y 0x0-6 0 0x68)
FILE * fp;
// write output to file
// save to directory
close(fp)
Upvotes: 2
Views: 2142
Reputation: 1093
Any command can have > appended to it to redirect the output. As in:
echo "foo" > /path/to/file
Note, there are two things you should know: 1 ">" is overwriting a file while >> is appending. As in:
echo "foo" > /path/to/file
file content will be:
foo
while
echo "foo" > /path/to/file
echo "foo2" >> /path/to/file
file content will be:
foo
foo2
And also, if you want to redirect errors you can use the 2 operand. As in:
cat /path/to/non-existing-file 2> /outputfile
Will write all the operation's error into outputfile
.
The same >
and >>
logic applies.
Upvotes: 1
Reputation: 166
You can using shell redirection like so:
echo "Hello world" > greetings.txt
Or to suit your needs:
#!/bin/bash
i2cdump -r -y 0x0-6 0 0x68 > output.txt
# closing is automatic at the end of redirecting.
General information about shell redirection of standart output stream in Bash manual: Redirections
Upvotes: 2