In C how do you redirect stdin/stdout/stderr to files when making an execvp() or similar call?

The right way to do it is to replace the file descriptors STDIN_FILENO, STDOUT_FILENO and STDERR_FILENO with the opened files using dup2(). You should also then close the original files in the child process: else if (pid == 0) { dup2(fileno(someopenfile), STDIN_FILENO); dup2(fileno(someotherfile), STDOUT_FILENO); dup2(fileno(somethirdopenfile), STDERR_FILENO); fclose(someopenfile); fclose(someotheropenfile); fclose(somethirdopenfile); execvp(args[0], args); // handle error … … Read more

What’s the better (cleaner) way to ignore output in PowerShell? [closed]

I just did some tests of the four options that I know about. Measure-Command {$(1..1000) | Out-Null} TotalMilliseconds : 76.211 Measure-Command {[Void]$(1..1000)} TotalMilliseconds : 0.217 Measure-Command {$(1..1000) > $null} TotalMilliseconds : 0.2478 Measure-Command {$null = $(1..1000)} TotalMilliseconds : 0.2122 ## Control, times vary from 0.21 to 0.24 Measure-Command {$(1..1000)} TotalMilliseconds : 0.2141 So I would … Read more

How to pass password to scp?

Use sshpass: sshpass -p “password” scp -r [email protected]:/some/remote/path /some/local/path or so the password does not show in the bash history sshpass -f “/path/to/passwordfile” scp -r [email protected]:/some/remote/path /some/local/path The above copies contents of path from the remote host to your local. Install : ubuntu/debian apt install sshpass centos/fedora yum install sshpass mac w/ macports port install … Read more

How do I use sudo to redirect output to a location I don’t have permission to write to? [closed]

Your command does not work because the redirection is performed by your shell which does not have the permission to write to /root/test.out. The redirection of the output is not performed by sudo. There are multiple solutions: Run a shell with sudo and give the command to it by using the -c option: sudo sh … Read more

How to redirect and append both standard output and standard error to a file with Bash

cmd >>file.txt 2>&1 Bash executes the redirects from left to right as follows: >>file.txt: Open file.txt in append mode and redirect stdout there. 2>&1: Redirect stderr to “where stdout is currently going”. In this case, that is a file opened in append mode. In other words, the &1 reuses the file descriptor which stdout currently … Read more