Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [io-redirection]

I/O redirection is used in shell scripting. This feature enables to take the output of a command and redirect it as an input for another command. Or command can take the input from a file instead of the keyboard. The whole process helps connect commands in various ways.

3
votes
1answer
156 views

How to use variables inside back-quotes

I have a bash script that accepts a parameter "$input" and (among other things) directs it into a program and collects the output. Currently my line is: OUTPUT=`./main.exe < $input` But I get ...
0
votes
1answer
24 views

Remote overwrite crontab file with sudo

I'm trying to build a script that automatically takes a file on the remote server and replace with crontab file, but I get permission denied. My idea is to create a shell function for it: ...
0
votes
0answers
53 views

Redirecting input bash

I have a program, that runs on only one Raspberry Pi, but could be run from multiple other computers (through SSH). When I run my program on the Pi, it runs as expected. To be able to run from crontab ...
5
votes
1answer
727 views

What's going on with this dash '-' thing?

I was trying to export the Python environment requirements and this is what I intended to do: conda list -e > requirements.txt However I mistakenly typed this instead: conda list -e -> ...
0
votes
2answers
55 views

How could I know inside a script if stdout has been redirected for it?

I have a script in ksh; depending whether there is a redirection from the command line, I redirect the output via a exec 1>file. How can I test from inside the script itself if the command calling ...
0
votes
2answers
30 views

Nohup: Redirect stderr to file and dismiss stdout

Per default, nohup writes stdout to nohup.out and dismisses stderr. I would like to have it the other way around: write stderr to a file and dismiss stdout.
0
votes
1answer
33 views

redirect created files in a directory from command line

I was wondering if there is any way to redirect created files from a .sh script to a specific directory without modifying the script itself but straight from command line. Let's say: I've a script ...
2
votes
1answer
71 views

Redirect script output both to console and to log

Take a look on the following # more /tmp/myscript #!/bin/bash echo I AM echo BIG GUY echo AND ALSO SMART # bash /tmp/myscript | tee -a >/tmp/log why we not get the output from echo in the ...
3
votes
4answers
78 views

Why does `… | sed 's/^/stdout: /'` print on empty stdin when `… > >(sed 's/^/stdout: /')` doesn't?

I'm trying to understand what causes the difference in these 2 constructs that I thought were functionally equivalent: $ ( echo foo >&2 ) 2> >(sed 's/^/stderr: /') | sed 's/^/stdout: /' ...
0
votes
1answer
43 views

Spaces after io redirection operator? [duplicate]

I've noticed that when writing a shell script, there's a lot of inconsistency in whether people put a space after the I/O redirection operators. For example: foo >bar.txt Will work, but so will ...
-1
votes
3answers
44 views

Oneliner for outputting to file only if string is matched (bash)?

I am making a script where I curl a URL and output to a file like this: curl http://example.com/$1 > $1 Is there any way to make it dismiss anything that doesn't include, say <head>? I ...
20
votes
3answers
2k views

How does this script ensure that only one instance of itself is running?

On 19 Aug 2013, Randal L. Schwartz posted this shell script, which was intended to ensure, on Linux, "that only one instance of [the] script is running, without race conditions or having to clean up ...
1
vote
2answers
60 views

Run parallel command and redirect the output to files with specific name

I can't undestand well how the parallel command works. I need to run this simple command: (100 times) curl https://jsonplaceholder.typicode.com/todos/1 curl https://jsonplaceholder.typicode.com/...
5
votes
3answers
217 views

Portable way to invoke tar with a list of files from stdin

I want to create a .tgz from the content of a directory. I also want to strip the leading "./" from the tar'ed content. I had done this as follows: cd /path/to/files/ && find . -type f | cut -...
2
votes
1answer
19 views

Ambigious redirect error on different volumes with mysql

i want to import a database with mysql (running with MAMP): #! /bin/bash # variables currdir=$PWD dbname=TEST dbuser=root dbpass=root dbpath="/applications/MAMP/Library/bin" exname=database if [ "$1"...
1
vote
3answers
34 views

How can I remove a newline from redirected output

Can I remove a newline if I redirect output like this? echo "a" >> file I have something similar in my script which contains a for loop that redirects every character (except numbers) to a ...
0
votes
3answers
66 views

How can you write to a file from the command line without using shell output redirection? [duplicate]

I'm trying to: sudo echo "$USER: my message" > /dev/kmsg to write a message to the dmesg log. This causes a 'permission denied' error, because it is redirecting the output of 'sudo' (while no ...
3
votes
1answer
53 views

What's the purpose of 1> in exec 1> >(logger -s -t tagname) 2>&1?

I just stumbled over exec 1> >(logger -s -t $(basename $0)) 2>&1 which is used to redirect the output of the current script to the system logger (in case you've never seen this, but are ...
0
votes
2answers
67 views

Redirect output to standard out instead of file

I am writing a java wrapper for convert (which is part of image magick). What I want to do is call a command like convert -thumbnail 1024 test.JPG >(cat) to make it print the thumbnails byte stream ...
4
votes
2answers
71 views

Strange behaviour redirecting man pages

I wanted to redirect mc's man page to a text file using man mc > mc, but I accidentally executed man mc > mc grep. What resulted was a text file named mc containing two concatenated mc man pages....
0
votes
1answer
72 views

Bash output redirection to new file each time command is run

I have a Bash one-liner that redirects the output of a command to a new, fresh file each time the command is run. Logging the output of each time the command is run is important (I'm using ls here as ...
0
votes
1answer
16 views

How do I avoid this unexpected behaviour when redirecting output from echo?

I wanted to add to my hosts file easily using a one line command, but I get an unexpected result. $ sudo sh -c 'echo -e "10.0.10.0\tserver.bananas.com\tserver" >> /etc/hosts' $ cat /etc/hosts -...
0
votes
1answer
74 views

-c and -b options not working in mailx in ubuntu

I was trying to send a mail from the shell script using the command mailx -s "TEST MAIL" -c "user_name ..." user<message.txt but it generates an error as unrecognized option -c ,the same happens ...
9
votes
1answer
590 views

Redirecting stdout from two programs

I'm running a memory check tool (prog1) on a C++ code (prog2), both of which give me a huge and detailed output. In order to chase down some bugs I need to save this output to a file. I tried ...
0
votes
0answers
110 views

Cannot pass argument through /proc/<pid>/fd/0

I have written a basic C++ program as such: #include <sstream> #include <unistd.h> #include <cstdio> #include <iostream> int main(void) { int foo; std::cin >> foo; ...
-2
votes
1answer
52 views

Piping output of command to sed

How would i pipe results of a command to sed without using '>>' so it appends that output to file? What i want is something like this command | sed -i "${command output}" file
1
vote
1answer
99 views

Is it possible to get the output of only sudo command?

I want to write the output of only sudo command to some file i.e. the line which I get on terminal after writing sudo i.e. [sudo] password for user: If I use sudo ls > file or sudo ls 2> file ...
2
votes
2answers
38 views

Does output redirection to a block device overwrite bytes or whole blocks?

A comment on another question made me wonder: Assuming I redirect the output from a command to a block device, and the length of that output is less than the block size of the block device: % ...
0
votes
2answers
38 views

How do I diff the output of these two commands? [duplicate]

I have an alias which I use to strip out comments that are piped to it: alias decomment='egrep -v "(^#.*|^$)"' I'm currently having some difficulty with a squid proxy setup, so I want to compare ...
1
vote
1answer
22 views

A command wants file paths. How can I give it stdin for the “infile” and stdout for the “outfile”?

Consider a command like foo -in /path/to/infile -out /path/to/outfile, that you'd like to pass strings to rather than using temp files. It may be called a lot causing much disk access, or is related ...
0
votes
2answers
58 views

What is the end of here string?

It seemed to me that the end of a here string is newline. I realize I am wrong: $ cat <<< hello world cat: world: No such file or directory What can signify the end of a here string?
1
vote
1answer
20 views

What is happening in these MULTIOS redirections?

When I run 1>&2 echo hi |& grep hi, I get a non-colored hi followed by a red hi on a new line. This is the same output as 1>&2 echo hi | grep hi. Why aren’t both of the “hi”s red in ...
1
vote
1answer
100 views

How does sponge (from moreutils) work?

sponge can “soak up” stdin and write it atomically to a file, enabling one to do cat f|sponge a. I want to know how exactly it accomplishes this. How does it know when the input is finished?
0
votes
1answer
23 views

Why are redirections not working following assignment?

./NulFile contains NUL. When command expansion removes NUL from it, it outputs warnings. Is there some way to suppress the warnings? Why are redirections not working? $ filecontent="$(cat ./NulFile)"...
2
votes
1answer
202 views

What are the differences between here document and here string in their purposes? [closed]

In bash, it seems to me both here document and here string can be used for providing strings as stdin inputs. here document may provide an extra features to specify delimiter, which I am not sure if ...
2
votes
2answers
67 views

Bash: turn stderr back on after suppressing

I've made this script which automates installing/uninstalling git. In my function for testing if git is installed, I use the git --version command and test the return code. I dislike the stderr ...
4
votes
4answers
658 views

Redirect to file, including current prompt line

How to include the redirected command itself to the output file redirection? For example echo "hello!">output.txt I want the content of output file like this: echo "hello!">output.txt hello!
3
votes
2answers
141 views

Why sed -i doesn't work with fd

I have this file and fd: exec 88<>abc Why $ sed -i "s/cd/II/g" /proc/$$/fd/88 sed: couldn't open temporary file /proc/26194/fd/sedS1D1FT: No such file or directory but this work: $ cat /...
0
votes
3answers
59 views

Script execution to continue even if redirection to a file fails [closed]

Its a typical scenario that, we redirect script outputs to a file. Either because of a permissions issue or whatsoever reason, output file cant be created. (Typically, these are log files ...
0
votes
0answers
10 views

How pipe/redirect influences the execution of the script which output is to be piped/redirected? [duplicate]

I stumbled on the example of something I wasn't aware to be possible, that the mere fact of putting | or > after the script, can influence of how it will run. Details of how I realized it can be ...
0
votes
1answer
110 views

How to redirect output to file to STDOUT?

Similar problem as here How to redirect output from file to stdout? However, the solution given there is not universal. My problem: I have a command that produces output to STDOUT. At the same time, ...
-1
votes
2answers
77 views

What does this command do (strange assignments and redirections) [closed]

I would like to get more details on what the below command is doing: PASSING=0 FAILED=0 > log_${ENV} print "Test Started at `date`\n"
-4
votes
3answers
60 views

What does this command mean? (IO redirection question) [closed]

I am trying to figure out what this command is doing: rm OUTPUT/* >> log_${ENV} 2>&1
3
votes
3answers
161 views

How to `paste` stdin next to stderr?

Usually paste prints two named (or equivalent) files in adjacent columns like this: paste <(printf '%s\n' a b) <(seq 2) Output: a 1 b 2 But when the two files are /dev/stdin and /dev/...
1
vote
2answers
202 views

sysctl command returning info not asked about

I want to grep a particular kernel setting as follows $ sudo sysctl -a --ignore | grep -i max_map_count 2>/dev/null sysctl: reading key "net.ipv6.conf.all.stable_secret" sysctl: reading key "net....
0
votes
0answers
36 views

Logging output of scripts called inside another script

Inside bashscript, I am using exec and tee to redirect logs to stdout and a log file in the same time - Unix & Linux Stack Exchange. exec &> >(tee -a "${LogFile}") I am sourcing ...
1
vote
1answer
53 views

Using exec 1>&2 in a POSIX shell script function. Will it interfere with other io-redirects?

I am in process of writing a POSIX shell script. I have one function, which redirects (almost) all output to standard error stream like that: # print something to stderr printf "..." $var 1>&...
2
votes
2answers
287 views

Read / write to the same file descriptor with shell redirection

I'm trying to understand file descriptors in the context of shell redirection. Why can't I have cat read from FD 3, which is being written to by ls's STDOUT? { err=$(exec 2>&1 >&3; ls -...
0
votes
1answer
41 views

How does redirection work between arguments

Given a command like this: command <inputfile> <outputfile> And then called like this: command FileA > FileC FileB It takes A and B as input and outputs to C, but how can it parse ...
-1
votes
1answer
51 views

Why output is on the prompt line?

$ echo 'hello world' > >(wc) $ 1 2 12 █ Why output is on the prompt line and not in the separate line below? Also why expects input at the end? Since, the command below works fine ...