Questions tagged [io-redirection]

I/O redirection is used in shell scripting. This feature enables one to take the output of a command and redirect it as an input for another command, or a command can take the input from a file instead of from the standard input stream or from the terminal. The whole process helps connect data streams in various ways.

Filter by
Sorted by
Tagged with
0 votes
2 answers
45 views

Is it possible to execute a command before creating the file in which I want to redirect its output

I want to execute the following command: dune exec -- ocaml-print-intf file.ml And redirect its output to file.mli The issue is that I can not write dune exec -- ocaml-print-intf file.ml > file....
user avatar
  • 103
0 votes
3 answers
35 views

Is there an equivalent to /dev/stdout for file descriptors 3-255?

I know that one can often trick command line utilities that expect a file to pipe into stdout out by passing /dev/stdout as the file name. I'm wondering whether it's possible to make a tool pipe to a ...
user avatar
0 votes
1 answer
23 views

Is it possible to create a ncat loop?

If you had two computers, wanting to control one from another using only ncat, without using telnet, ssh or other networking tools and only two shells, how could i go about that? I have successfully ...
user avatar
3 votes
1 answer
98 views

"Ambigous Redirect" for redirecting text to a variable

I'm making a shell script to limit the amount of time someone spends on a program but it gives me these errors: ./time_limit: line 26: $Log: ambiguous redirect ./time_limit: line 16: [: root: unary ...
user avatar
2 votes
1 answer
553 views

Why is using "|" or "&" provide different sizes of the result file?

Here is my explanation: strings *.bin > bin.txt | sort -n bin.txt > logs1.txt that gives me logs1.txt, but it has 621 KB. strings *.bin > bin.txt & sort -n bin.txt > logs1.txt that ...
user avatar
  • 29
0 votes
1 answer
32 views

How to get color in terminal but no color in output file [duplicate]

To show output in both terminal and to a file, tee can be used like below ls --color=auto | tee opfile But now, there is no color in the terminal. If I force colors, opfile also has colors which ...
user avatar
  • 4,049
-1 votes
1 answer
20 views

Using a pipe with extra arguments at the end. Correctly directing the pipe instruction with multiple arguments

General description: I had a couple of times the same issue, that I solved by doing an step by step script. But I know that with pipe, one-liners would make my life way easier. Simply: command1 -flag ...
user avatar
4 votes
1 answer
77 views

Bash redirection weird behavior

I was messing around with trapping exit codes and redirecting stdout and stderr of a case statement when I ran into an interesting outcome that I am hoping someone can shine some light into. I am ...
user avatar
  • 339
4 votes
1 answer
165 views

feed output of command to another command as file

I can make config file of tor with cat <<EOF command as follows : cat <<EOF SocksPort 9050 ControlPort 9150 DataDirectory /usr/var/lib/tor/torrc EOF but it can save output with redirect &...
user avatar
  • 43
0 votes
1 answer
23 views

How to redirect while ignoring some of the text

Some potential duplicates may be the following: How to ignore specific lines from being redirected BUT I can't extrapolate from that to solve my problem. So, I am asking here instead. So, here is what ...
user avatar
4 votes
1 answer
186 views

Why I need to type press Enter key to finish this command? [duplicate]

I'm a new Linux user and I was doing some experiments and trying to understand Process Substitution. I believe I already have a basic understanding of it. But here is a case that I don't know why. I'm ...
user avatar
0 votes
1 answer
89 views

How to keep the output order when redirecting both stdout and stderr?

To describe the issue, I created a simple C program. root@u2004:~# cat test.c #include <stdio.h> int main(){ printf("output 1\n"); fprintf(stderr, "error 1\n"); ...
user avatar
0 votes
1 answer
31 views

stdin/stderr redirection, piping and color

I've seen a zillion permutations on this question but I think none is an exact duplicate. Can I copy only stderr (or eventually stderr and stdin) to a file, while keeping the colorized output on the ...
user avatar
1 vote
1 answer
34 views

How does Input/Output Redirection work in Linux/Unix?

Example: cat < test.txt Is the content of the file test.txt written/passed to stdin of the cat, and then the cat reads its stdin? OR Does the file test.txt itself become the stdin of the cat? ...
user avatar
0 votes
0 answers
24 views

pass output as grep pattern [duplicate]

I wish to pass each line of a file as a grep pattern to be searched in data.txt file. Thus, i need cat allsearchstrings.txt | grep <should be lines of allsearchstrings.txt> data.txt cat ...
user avatar
  • 307
-1 votes
1 answer
40 views

What actually happens when I enter the following prompt on the CLI: cat demo.txt >> cat sample.txt?

Let say we have a text file demo.txt that only contains the word 'demo' and a text file sample.txt that only contains the word 'sample'. I understand that cat demo.txt >> sample.txt would append ...
user avatar
2 votes
0 answers
22 views

bash: debug (xtrace) target of redirections

Is there a way to show redirections with bash -x? $ bash -exc 'for word in foo bat baz; do echo $word >$word; done' + for word in foo bat baz + echo foo + for word in foo bat baz + echo bat + for ...
user avatar
  • 21
0 votes
1 answer
126 views

Can redirecting stderr and stdout to one file result in an error?

I'm writing a script that takes files (filename...) as arguments, and redirects both the stdout and stderr to a file of name filename.output. In the files are commands, and I'd only like to bash once (...
user avatar
  • 3
0 votes
0 answers
52 views

"> $logfile" does not truncate, file size goes to 0 and a second later, is back to full size [duplicate]

I have a script that writes to a logfile like this: $ nohup myscript.sh > myscript.out 2>&1 & when the log file gets very large, I need to truncate it like this: > myscript.out I ...
user avatar
  • 1
0 votes
2 answers
27 views

ps aux --headers problem on openSUSE Leap 15.3 when redirect output

The following works ok on openSUSE Leap 15.3. As in the header line is repeated for each screen of output when I scroll the screen back with the scroll bar on the terminal: ps aux --headers But when ...
user avatar
1 vote
0 answers
26 views

Why does redirection cause output to be duplicated? [duplicate]

In zsh, why does echo hi >&2 | cat produce the output hi hi ? My understanding is >&2 causes dup2(STDERR_FILENO, STDOUT_FILENO) to be called. | cat causes dup2(fd, STDOUT_FILENO) to ...
user avatar
1 vote
1 answer
50 views

Redirect Output Back to File [duplicate]

I issued the following command, thinking it would take the contents of foo.txt, process it, and write it back to the original file. Instead when I opened the file, it was empty. cat foo.txt | tr "...
user avatar
  • 115
0 votes
2 answers
41 views

Why do these two commands to write text-processing results back to the input file behave so differently?

I have a file authorized_keys and want to deduplicate the content, i.e. remove duplicate entries. I found two possible solutions to achieve this: Use cat and uniq, then redirect the output to the ...
user avatar
  • 1
0 votes
0 answers
57 views

How do I capture binary output from a process different from the console output?

I'm currently able to launch a process from within Java's ProcessBuilder, which continually appends to a (binary) file output.WAV, and is continually dumping lines of console updates. That works ...
user avatar
0 votes
2 answers
36 views

Redirect bash script output of application to file keeping the application interactive

I'm running a bash script expecting occasional user input to continue processing the script. Is it possible to redirect the script output to a file while keeping the terminal output and keeping the ...
user avatar
  • 269
0 votes
0 answers
22 views

File I/O redirection help in Bash for running badblocks into gzip and monitor with zcat [duplicate]

I am running this on a 10 TB bad drive, so the log grows out of proportions in an hour or so. This was my idea, not working until I kill the badblocks: badblocks -v -b 4096 -c 98304 -w -s /dev/disk/by-...
user avatar
2 votes
0 answers
78 views

What is the purpose of a pipe after stdout redirect?

I was reading the documentation for Kaniko and saw a shell redirect-then-pipe construct that I haven't seen before: echo -e 'FROM alpine \nRUN echo "created from standard input"' > ...
user avatar
0 votes
1 answer
91 views

grep screen output differs from output to file

When using grep (and sed also) the results I get in the terminal are different than what I get if I direct the output to file. My starting file is the captured output from a darknet yolo mAP run and ...
user avatar
0 votes
1 answer
165 views

echo "Hello World" > /dev/stdout prints nothing

Scenario: $ echo "Hello World" > /dev/stderr Hello World $ echo "Hello World" > /dev/stdout $ uname -a CYGWIN_NT-10.0 xxx 3.3.4(0.341/5/3) 2022-01-31 19:35 x86_64 Cygwin ...
user avatar
  • 397
2 votes
1 answer
76 views

POSIX Shell: Output to log and prepend lines with timestamps using a function

I would like a shell script to have all it's output for stdout and stderr logged to an external file with timestamps prepended to each line (plus a few extra items), without changing how the script is ...
user avatar
  • 73
0 votes
1 answer
145 views

CRON 2>&1 >/dev/null vs >/dev/null 2>&1 [duplicate]

I just created a tiny shell script to be run every minute. What happened was that it started flooding my mail. $ crontab -l * * * * * /home/user/bin/test 2>&1 >/dev/null I changed the ...
user avatar
0 votes
1 answer
52 views

redirect TTY input to output without serial cable

I'm using simulation software for unix (ucsim) that runs two simulated programs that supposed to connect to each other via its own simulated serial ports. The software provides me with these methods ...
user avatar
  • 1
1 vote
0 answers
52 views

SQL output piped to `kubectl exec` crashes, if not replacing TABS with SPACES

The context: I'm generating SQL from a backup dump, inside a subshell, to then pipe it to mysql for execution: ( echo "SET FOREIGN_KEY_CHECKS=0;"; tar --to-stdout -xvjf archive.tar; echo &...
user avatar
0 votes
1 answer
34 views

How might I save the the output of "find .... -execdir ..." to a log?

I cannot get the following script: #!/usr/bin/bash /usr/bin/find ~/CS -iname "*.html.pmd" -type f -execdir /usr/bin/raco pollen render {} \; > ~/rendering to redirect the output to the ...
user avatar
0 votes
0 answers
42 views

Why doesn't creating file with echo work for remote directories in this script?

I aliased the following script: #!/usr/bin/bash if [[ -d "template.html" ]]; then : else cp -n /home/$(whoami)/CS/Software/Pollen/template.html . fi /usr/bin/echo "#lang pollen [...
user avatar
0 votes
0 answers
99 views

Listening to stdout of a remote machine

I'm looking for a way to get a local terminal tail log from an stdout execution on a remote machine without directly executing with ssh. The general procedure is: The local client machine runs a ...
user avatar
  • 101
2 votes
1 answer
94 views

Create parent directories when using shell redirect

In Zsh, the following fails: $ echo hi > /tmp/this/path/does/not/exist/out.txt zsh: no such file or directory: /tmp/this/path/does/not/exist/out.txt Obviously the problem is that > cannot ...
user avatar
  • 684
1 vote
1 answer
76 views

Redirect stdout from a runing script

In C, you can redirect stdout somewhere when a program is running, for example: int fd = open("some_file", O_RDWR); dup2(fd, STDOUT_FILENO); printf("write to some_file\n"); Can I ...
user avatar
  • 205
3 votes
2 answers
420 views

How can I pipe a command to grep and get the exit status while still sending command output to stdout and errors to stderr?

Concrete use case: I'm trying to curl a URL with -v, output the verbose debug information to stderr as normal, output the response body to stdout as normal, but also check for the presence of a regexp ...
user avatar
1 vote
1 answer
81 views

Piping output from GitHub clone using tee misses output

I thought I would log the output of cloning a GitHub repo to a file. If I run git clone git@github.com:my_repo/my_repo.git | tee -a log I get an empty log file, but the following output to the ...
user avatar
8 votes
4 answers
2k views

How does "cat << EOF | grep" work in bash?

In this answer to How does "cat << EOF" work in bash? on Stack Overflow, I get the first two points. But I don't get the third point Pass multi-line string to a pipe in Bash Pass ...
user avatar
  • 411
1 vote
2 answers
145 views

Avoid creating an empty file when using redirection [duplicate]

foo | bar > out.txt would normally save the output to out.txt. However, if foo fails, then clearly bar cannot have any output, so it makes no sense to create out.txt. However, usually the result is ...
user avatar
  • 684
0 votes
1 answer
61 views

What's the "printing difference" between `git tag` and `git describe --tags`?

git tag seems to print in a separate process (window? app?) whereas git describe --tags is printed "in place". I can achieve the same printing behavior with git tag as git describe --tags by ...
user avatar
0 votes
0 answers
178 views

How to pipe an input into the cat command

I was reading an answer regarding sending large files from one Linux system to another by quick methods, and I was trying this solution The answer is saying that if you don't want to use the tar ...
user avatar
  • 259
0 votes
3 answers
82 views

Using `1>&- 2>&- &' as an alias after a shell command

To run a program with bash in background, with no text output, it is possible to use: 1>&- 2>&- &. I would like to define an alias for this. However, if I write in .bashrc alias ...
user avatar
  • 4,503
1 vote
1 answer
49 views

Temporarily showing stdout while a command is running

I'm scp'ing several large files to a few servers in succession. I'd like to see the progress (so I don't want to use scp --quiet), but I don't need to see the stats when the copy is complete. Is ...
user avatar
  • 2,319
0 votes
1 answer
117 views

How might I direct the output of "wget -qO- whatever.tar.gz" to both tar and sha256sum?

The following creates csfile with a hash in it, unpacks go1.17.4.linux-amd64.tar.gz downloaded from Golang's official website, prints out the checksum to stdout and then removes the no longer needed ...
user avatar
2 votes
0 answers
105 views

Possible reasons for redirection to /dev/null suddenly breaking

I am using a Bash loop to monitor a process that I spawned, and after some time (hours/days) it sometimes produces an "operation not permitted" error trying to redirect output to /dev/null: /...
user avatar
  • 43
0 votes
1 answer
89 views

Pipe output of two commands into another script as arguments

ntopng can handle multiple network interfaces and I've a script that takes the name of the interface and outputs it in to stdout. I can pipe this output into ntopng my_packet_tracer.sh | ntopng -i - ...
user avatar
0 votes
2 answers
60 views

exec vscode not redirecting output to /dev/null

I have the following bash script: exec '/mnt/c/Users/WindowsUserName/AppData/Local/Programs/Microsoft VS Code/Code.exe' $@ &>/dev/null & When I run the script I get ➜ ~ ./code foo.js ➜ ~ ...
user avatar

1
2 3 4 5
28