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
2answers
44 views

What’s the difference between providing input to a command via an argument to that command and providing input through a shell redirection operator?

When I use my shell’s redirection operator to provide a (non-existent) filename to the grep command, the error message about that file not existing is displayed by my shell. But when I provide that ...
0
votes
0answers
18 views

modify binary output before returning to the prompt mode

I have a binary which prints the question and then waits for a user answer. I have no access to the source code of this binary, but I want to modify the screen output. Example: > ./commandx Day ...
0
votes
0answers
24 views

prompt for password in command's heredoc then pipe result to function causes script to hang

I'm writing a bash script that will run a series of SQL files and output to either LESS or a file. I figured it would be safer to not hard code the database password into my script, and I didn't feel ...
0
votes
2answers
51 views

Is it possible not to override a file in case of error when redirecting STDOUT?

I have a simple syntactically wrong command like this that is meant to be run on Unix/Linux hosts $ echo "first string $PATH" > out.lst $ ECHO second string %PATH% > out.lst The first line ...
0
votes
1answer
54 views

ambiguous redirect using curl [closed]

I have this: /dev/null <& curl --silent "$url" \ -H "Content-Type: application/json" \ -X POST \ -d "$(cat <<EOF {"state":"failure","context":"continuous-integration/jenkins","...
0
votes
0answers
14 views

Print clang include path and store in file

I'm writing a program that needs to pull in the clang include paths. Thanks to Dump include paths from g++ I'm half-way there. The command I'm using is: clang++ -E -x c++ - -v < /dev/null The ...
0
votes
3answers
37 views

echoing Command to cron.d

I am trying to append/echo a command to cron.d. However, when running the script that echoes the command: echo */2 * * * * /usr/bin/aws ssm-send-command --document-name "AWS-RunShellScript" --...
0
votes
2answers
53 views

Little question about redirection

I'm trying to understand bash redirection, but I stumbled upon a strange example. Consider the following interaction. 1: $ echo "Hello" 11>txt 11<&1 2: Hello 3: $ cat txt 4: $ echo "Hello" ...
0
votes
1answer
41 views

Alternative to “openvt”, but for pseudo terminal (pts). Exists?

Alternative to "openvt"...but for pseudo terminal(pts)... Exists? Openvrt operate only tty...but not pts. (Example: lauch comand in specific pts and with user of shell pts destination) Command <...
0
votes
1answer
37 views

why executing ls > empty.txt shows me empty.txt inside empty.txt in an empty folder?

why in an empty folder executing ls > empty.txt shows me empty.txt inside empty.txt ? it is not poetry, it is true and I do not know why. For me, they way to think ls > empty.txt is the ...
0
votes
0answers
28 views

How to redirect a sorted list of files returned by GNU find to a GUI file manager such as Nautilus or Dolphin

Question I often fall in this situation where I have meticulously forged the perfect find command that returns exactly the ordered list of files I need to use. However, I cannot simply click on files ...
0
votes
3answers
66 views

Writing information in another file using awk script

I have an awk script that looks like this: #!/bin/awk -f BEGIN{} { for(i=1;i<=NF;i++) sum+=$i; #sum of 3 numbers in a row printf "Hello %d%s, %s %s, %d\n",NR,OFS, $1,$2,j;j=0 ...
0
votes
1answer
23 views

How to save the contents of “Konsole” terminal in KDE

I want to know is there any way to save the contents (i.e, commands and stdout together) of Konsole terminal in KDE desktop as in Xfce4-terminal?
24
votes
5answers
1k views

Redirecting a pipeline into the file from which it originated

One might think that echo foo >a cat a | rev >a would leave a containing oof; but instead it is left empty. Why? How would one otherwise apply rev to a?
2
votes
1answer
41 views

How to redirect stderr in busybox?

I have BusyBox v1.27.1 (2017-08-03 03:14:35 CEST) built-in shell (ash) on router. I found a weird issue: root@DD-WRT:/# echo -n "Hi" 2>/dev/null -sh: 2: Invalid argument Seems I cannot redirect ...
0
votes
0answers
18 views

Why the following redirection doesn’t work? [duplicate]

i typed the following command which cp | ls -l It didn’t work as i expected. I wanted that the output of the which command will be a path name. So the ls command will show the information about the ...
4
votes
1answer
31 views

bash: use different input streams for loop condition and body

i have a script with a while loop like this: while IFS= read -r name; do {body} done < names.txt the loop condition needs to read names from a file, but {body} needs to read from stdin; the ...
0
votes
0answers
43 views

Prevent truncation of file with output redirection via /dev/stdout

I have a program that I want to execute and gather its output in a file. However, while running the program opens /dev/stdout (i.e., /proc/self/fd/0) and truncates it, which removes all content from ...
1
vote
1answer
26 views

Long running programs redirected file output

To help in my mathematics research I wrote a Python2 program and in bash invoked it as python myprog.py > myoutput.txt First I ran it for a small set of input values and got the output in ...
0
votes
1answer
45 views

Interactive bash script: Prefix all lines with a string

I want to create an interactive CLI in bash. Similar to the way bash prefixes every line with user@host:~$, I would like to prefix every line with foobar>. This functionality can be seen in CLI ...
0
votes
2answers
46 views

Get output redirection part of a running process

Sometimes I run jobs, I lose track of which output files I have redirected the output to. How can I get the full command I ran a job with, including the output redirection part? For example, when I ...
2
votes
2answers
59 views

Redirect stdout to command-A if success else stderr to command-b. While avoiding using temporary files

I have a command that writes to stdout when it is happy and stderr when it's not. I want to check the exit status of the command and do something with either stdout or stderr. if ! command >/tmp/...
0
votes
0answers
23 views

Removing file with output being redirected to

Say I have this Python program example.py (I am not familiar with bash enough to translate this to bash) from time import sleep for i in range(20): sleep(1) print(i) and I run python3 ...
0
votes
1answer
39 views

How do I really hide all console output from the process I start? [duplicate]

I was getting all those messages from firefox that I didn't care about: Gtk-Message: Failed to load module "pk-gtk-module" Gtk-Message: Failed to load module "canberra-gtk-module" So I killed ...
0
votes
1answer
69 views

pass data to separate input file descriptor of child process with posix shell

In a posix shell I would like to pass data to a background child process file descriptor and process output from the child in the parent. No mkfifo please, just standard posix shell file descriptor ...
1
vote
2answers
75 views

What does “<>” mean?

I am having difficulty understanding redirects how can we use exec <> as a Linux command what does it mean. Can you give an example and explain?
11
votes
4answers
660 views

Grep over multiple files redirecting to a different filename each time

I have a directory full of .tsv files and I want to run a grep command on each of them to pull out a certain group of text lines and then save it to an associated text file with a similar file name. ...
3
votes
1answer
254 views

Multiple processes redirecting to the same file with >

This is not a "how to append and not overwrite" question. I'm not looking for a file that combines the output of two commands. It's just a mistake I made and I would like to understand why the system ...
0
votes
1answer
83 views

Unable to redirect output of crontab -l to file

I tried backing up my Crontab to file using following command: crontab -l > backup/crontab. But this creates only empty file into directory backup and no output is printed to terminal. Exit code of ...
0
votes
2answers
26 views

Redirect content to file and print content at same time?

I'm wondering if there is a way to redirect lines to a file and print that redirected lines at the same time? Currently I do something like this: echo "123:456" > output.txt ; awk 'BEGIN{...} {...
0
votes
1answer
34 views

Bash here document: redirecting the input vs typing it directly

$ cat<<EOF > a > EOF a Can I automatically redirect the stream vs typing into stdin? I have tried but this is what I get. [debian:~]$ cat <<(printf "EOF\na\nEOF\n") -bash: syntax ...
0
votes
1answer
54 views

Disable buffering for file descriptors in bash

I may be misunderstanding what's going on here, but I feel it's got to do with pipe buffering. I have a script which uses several file descriptors (# 3 and above) for various logging levels. Depending ...
5
votes
1answer
198 views

What is really happening in this command?

When I use the command cat <<< Hey > text.txt text.txt I expect it to write "Hey" in the file text.txt and then display the file. But there is no output. How is bash interpreting it ...
0
votes
1answer
25 views

What does “numeric” redirect mean in Linux?

I found command like this (MYVAR=0 ./mybinary --my-arg myval) < ../input 5> ../output/5 1> ../output/1 What does 5> and 1> mean?
1
vote
2answers
31 views

Symlinked directories and output redirect

When I run the following command, where symlinked-dir is just that - a symlink to another directory, cat complains that there is no such file. There is, indeed, no such file - it's created in the ...
8
votes
1answer
376 views

Why bash redirection to a file happens in append mode

given this snippet: exec 3>f echo foo >&3 echo bar >&3 cat f # returns foo bar Why is bash appending to f? Am I missing something?
6
votes
5answers
242 views

A command to output each line forward then backwards

I'm looking for a "meta-command" xyz such that: (echo "foo"; echo "bar") | xyz rev will return: foo oof bar rab I'd like to avoid temporary files, i.e. I'm looking for a solution neater than: ...
1
vote
1answer
44 views

avoid bash buffering problem in log processing

I have bash buffering a problem similar to what can be found here: Turn off buffering in pipe The socat solution in the above question is quite interesting as I have access to this command in my ...
0
votes
0answers
23 views

Why I get the output of stderr printed in terminal instead of a file when I redirect the stdout into stderr? [duplicate]

When I run a command with output redirection into stdin like that: sudo -u postgres psql mydb -e -f ./my_psql.sql 2>&1 > output_log.log The error messages are being displayed into the ...
0
votes
2answers
66 views

Proper way to code dry run option without having to repeat myself?

I'm coding a script that goes and searches for files on a remote server and transfers them back to my local computer. I want to be able to do a dry run first, so I know which files I'm bringing back. ...
11
votes
1answer
656 views

Why can't you reverse the order of the input redirection operator for while loops?

In Bash you can move input redirection operators to the front of a command: cat <<< "hello" # equivalent to <<< "hello" cat Why are you unable to do the same for while loops? ...
0
votes
0answers
39 views

How can I redirect a long key press to a device file in bash?

I need a long key press to be redirected to a device file. If I send the same key multiple times, it is considered as separate key inputs (with carrige return). I just need one long key press. How can ...
1
vote
2answers
95 views

Shell command output to file with command being executed [duplicate]

How can I output the command itself in addition to its output to a file? I know that I can do how to output text to both screen and file inside a shell script? to capture the output. My use case is ...
0
votes
0answers
25 views

How can I redirect output from the end of a pipe to the input of the beginning of the pipe [duplicate]

I'm trying to learn the intricacies of piping and redirection, and there's one thing I can't seem to manage to do. I'd like to try to get a loop going such that AA stdout -> BB stdin BB stdout -> ...
1
vote
1answer
79 views

Shell redirection: <<<'foo' vs <(echo 'foo') [duplicate]

I've seen both <<<'foo' and <(echo 'foo') suggested as alternatives to echoing to pipes (echo foo | cat). When I use both of these constructs with cat I get: $ cat <<<'foo' foo $...
-3
votes
1answer
88 views

Please explain this shell script snippet with an rm command [duplicate]

I am a beginner to the unix scripts, can anyone please explain what does the below lines mean: BTEQTEMPDELLOGS=$LOGS/${tablename}.DELlog rm $BTEQTEMPDELLOGS 2>/dev/null 1>/dev/null
1
vote
2answers
73 views

Appending both stdout and stderr to file

I have this: nohup_ntrs(){ nohup_file="$HOME/teros/nohup/stdio.log" mkdir -p "$(dirname "$nohup_file")"; echo " ------------- BEGIN $(date) -------------- " >> "$nohup_file" nohup "$@" &...
1
vote
0answers
34 views

How to write `time` it takes for script to run to file? [duplicate]

I'm trying to measure how long it takes my script to run with time on command line. I would like to have the time commands out put written to my output file. This is the command I have so far: time ./...
2
votes
1answer
882 views

How to redirect CURL output to file and as bash function parameter

How can I make curl output to $LOG and as a second parameter for the json_extract function. json_extract is a function that takes two args. The 'key' and a json string (the output of CURL) param=$(...
0
votes
0answers
26 views

Redirect ldapsearch ldiff from Docker container to host while displaying password promt

Having an ApacheDS LDAP DC in a Docker container, I want to export it as ldiff to the host (not inside the container): docker exec -it apache-ds sh -c 'ldapsearch -D "uid=admin,ou=system" -W -p 10389 ...