This question already has answers here:
Problem redirecting a C program output in bash
(5 answers)
Closed 9 years ago.
So this is probably a stupid question, but I can't see what I'm doing wrong.
I am running a program that produces output when called like ./ar. The output looks like:
-0.00781 0.02344 0.98828
-0.01172 0.02734 0.98828
-0.01562 0.02344 0.98047
-0.00781 0.02344 1.00000
-0.00391 0.02344 0.98438
A new line of output is written every second.
When I call the code like this ./ar > log and kill the program using ctrl-c after a few seconds, the file log is empty.
I am running this code on an embedded system. The system has a writeable partition which is the partition that I am running in, and I have write access as I am logged in as root.
The reason is the lazy writing concept of UNIX system.
Are you sure you are looking at standard output in you call ./ar? It might be standard error.
So, try ./ar >log 2>err to have 2 files, one for stdout and one for stderr.
Or use ./ar 2>&1 >log to get one file for both streams.
Related
This question already has answers here:
Detect if stdin is a terminal or pipe?
(6 answers)
Closed 5 years ago.
I'm currently working on my school project and wondered if there is any way to tell if user has written <textfile.txt to the terminal.
Like this: ./project <textfile.txt
My project reads all the lines from the text file to stdin and via scanf works with them. But when I don't use the <textfile.txt "thing", it starts without the data. Instead, I'd like it to write an error and stop the program.
Thanks a lot.
You might use isatty(3) to detect if your stdin is a terminal.
You could use fstat(2) on STDIN_FILENO, e.g. to detect redirection or pipelines.
Or (on Linux) readlink(2) /proc/self/fd/0 , see proc(5)
I recommend accepting some program argument to override such an auto-detection. Read this.
Be aware that redirection and globbing is done by the shell, see this.
On unix systems you can check whether stdin refers to a terminal:
if (isatty(0)) {
fprintf(stderr, "input was not redirected\n");
exit(EXIT_FAILURE);
}
I know how to get the stdout into a file using dup/dup2 system calls, but how do I get the entire output that would be normally shown on my terminal(including the prompt that says my username along with the $ symbol and the current working directory) to a file?
Yes you can, but this may be difficult in many details (depending on your expert level). For the shell to behave normally (I would mean exactly as in a terminal), then it needs to interact with a terminal (special system object). So you need to create a program that behave like a terminal, this what pseudo-terminals devices (/dev) are intended for. Read documentation about this to implement it but roughly, your application should behave like the user so should be connected to the slave side of the pseudo-terminal, and the shell to the master side of the pseudo-terminal. Then you can easily log real inputs made by the user and catch outputs made by the shell.
Can't comment cause of low reputation.
I would say there is no way to do that inside a code in C. Instead, you could use bash for example to redirect everything to a file, and leave the code in C as it is.
In this way you have all the info you want to save: prompt, current directory, call to the program (including flags), and of course the output of the program.
Well, you can do:
-For bash prompt PS1: Echo expanded PS1 (in case you want it expanded, if not there is a simple way to do it just echong PS1)
- For executed command: https://unix.stackexchange.com/questions/169259/how-to-capture-command-line-input-into-logfile-and-execute-it-at-the-same-time
- Standard output and error output: Redirect stderr and stdout in a Bash script
And that's all you want to capture, I think.
Look up the script command in Unix systems. If you want to capture all keyboard and std in/out for a command, use the script executable. If you want to see how it's done, look up the source.
This question already has answers here:
Is there a way to redirect syslog messages to stdout?
(2 answers)
Closed 8 years ago.
I m using syslog in myprograme to generate log messages.
Is there a way to send the syslog output of my program to stdout ?
I do not want to use the tail command to see my program log, I would like to see it directly on the console
You'll want to edit your /etc/syslog.conf file.
depending on exactly what facility you're sending to syslogd, you'll need to add a line something like this:
<facility>.debug /dev/console
be sure to check out man 5 syslog.conf for all the details..
To continuously clone file output to a console/shell use the following command in that console/shell:
tail -f <logfile> &
-f makes tail continue printing whatever gets written to the file
& puts the process in the background so you can do other stuff in the window. Omit the & if you want the console to block until you press ctrl+c.
This question already has answers here:
Reading a text file backwards in C
(5 answers)
Closed 8 years ago.
I am trying to implement a shell in C. Here's the thing. I want to make a history friendly function where if I press up it goes to prev. command.
Now I have a file which stores the history, say history.txt. When I execute a command, I would append the command to the text. And resets an offset of some sort to the last line of the file.
I need a way to find the last line and move up a line one by one on command. AND move up one by one on command.
Right now, an idea I have is to fgets() till -1 or something?
Any ideas for how I should start?
edit: I can think of a solution using an Array. But is there a way where I use little to no space?
Don't bother reading history from the file when you need to run the previous command. Just store the previous commands in memory. Write them to disk on exit, and load them on startup. That's sort of how real shells work.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How to invoke another terminal for output programmatically in C in Linux
I am programming a client-server application and I want to create a debug window.
On the server side I want to print the incoming and outgoing communication on a separate terminal.
I am able to spawn a terminal through gnome-terminal but how to write on it and not on other terminals.
Unless you for some reason really need to print to a terminal, I wouldn't bother, not just for a debug printout.
I would have the server print to a log file (remembering to flush it appropriately often) and then use tail -f in another terminal to follow it. This has the added benefit of giving me a record of what the server debug-printed that I can examine at leisure.
Combining idea of #ibid idea to what you want. Write to log file and than execute:
xterm -e tail "-f" log_file
This will span xterm , which executes "tai -f log file" command.
The "correct" answer to this question is that you can write to /dev/ttyNUM... if you know the right tty number.
But that's only technical correctness, you should do something else. What you're trying to do is wrong.