Linked Questions
32 questions linked to/from When would you use an additional file descriptor?
346
votes
8
answers
355k
views
How create a temporary file in shell script?
While running a script, I want to create a temporary file in /tmp directory.
After execution of that script, that will be cleaned by that script.
How to do that in shell script?
290
votes
10
answers
220k
views
How can I send stdout to multiple commands?
There are some commands which filter or act on input, and then pass it along as output, I think usually to stdout - but some commands will just take the stdin and do whatever they do with it, and ...
170
votes
12
answers
166k
views
How to grep standard error stream (stderr)?
I am using ffmpeg to get the meta info of an audio clip. But I am unable to grep it.
$ ffmpeg -i 01-Daemon.mp3 |grep -i Duration
FFmpeg version SVN-r15261, Copyright (c) 2000-2008 Fabrice ...
190
votes
8
answers
222k
views
How big is the pipe buffer?
As a comment in I'm confused as to why "| true" in a makefile has the same effect as "|| true" user cjm wrote:
Another reason to avoid | true is that if the command produced ...
116
votes
5
answers
148k
views
What's the difference between eval and exec?
eval and exec are both built in commands of bash(1) that execute commands.
I also see exec has a few options but is that the only difference? What happens to their context?
111
votes
4
answers
136k
views
What does "3>&1 1>&2 2>&3" do in a script?
I saw this line in a script:
DEVICE=`dialog --inputbox "Festplatten-Laufzeit auslesen. Gebe Sie das
gewünschte Device an: " 0 70 "" 3>&1 1>&2 2>&3`
What is
3>&1 1>&...
107
votes
4
answers
151k
views
Why is `while IFS= read` used so often, instead of `IFS=; while read..`?
It seems that normal practice would put the setting of IFS outside the while loop in order to not repeat setting it for each iteration... Is this just a habitual "monkey see, monkey do" style, as it ...
95
votes
2
answers
190k
views
suppress stderr messages in a bash script
Consider the following (slightly silly) script name 'test1.sh':
#/bin/bash
#
sleep 10 &
echo sleep pid = $!
pkill sleep
When I run it, I get not only the output of the echo, but bash's reporting ...
38
votes
5
answers
123k
views
How to read from two input files using while loop
I wanted to know if there is any way of reading from two input files in a nested while loop one line at a time. For example, lets say I have two files FileA and FileB.
FileA:
[jaypal:~/Temp] cat ...
58
votes
3
answers
32k
views
How to redirect stderr and stdout to different files and also display in terminal?
I want to see the output of a command in the terminal as if there was no redirection.
Also, stderr needs to be redirected to err.log and stdout needs to be redirected to stdout.log.
It would be nice ...
38
votes
4
answers
52k
views
Reading lines from a file with bash: for vs. while
I'm trying to read a text file and do something with each line, using a bash script.
So, I have a list that looks like this:
server1
server2
server3
server4
I thought I could loop over this using a ...
43
votes
2
answers
62k
views
File descriptors & shell scripting
I am having a very hard time understanding how does one use file descriptors in shell scripts.
I know the basics such as
exec 5 > /tmp/foo
So fd 5 is attached to foo for writing.
exec 6 < /...
11
votes
4
answers
15k
views
Bash script to detect the version control system by testing command return status
I am working on a bash script that I would like to work for several types of VCS.
I am thinking of testing if a directory is a repo for a system by running a typical info command and checking the ...
11
votes
4
answers
1k
views
Program output redirection
When trying to redirect program output with the "some number greater than" syntax (eg foo 2> myfile), what are the possible numbers here and what do they represent?
I believe 1 is /dev/stdout, 2 ...
12
votes
3
answers
18k
views
Using time on bash functions (not commands)
How can one measure individual calls to bash functions from inside the bash file.
I have a program that I call using the command
eclipse -b col_solve.pl -e "myPred"
This call outputs some ...
11
votes
2
answers
10k
views
A while loop and an here-document - what happens when?
I have this while loop and here-document combo which I run in Bash 4.3.48(1) and I don't understand its logic at all.
while read file; do source ~/unwe/"$file"
done <<-EOF
x.sh
y.sh
EOF
...
5
votes
2
answers
26k
views
Read multiple lines from text files in bash
When I am shell scripting, the majority of what I am doing is wrapping the I/O of other modules in python, matlab, etc. To do so, I usually use text files or something of that nature with the input/...
7
votes
3
answers
6k
views
Bash: Scope of variables in a for loop using tee
Consider:
numbers="1 111 5 23 56 211 63"
max=0
for num in ${numbers[@]}; do
[ $num -gt $max ]\
&& echo "old -> new max = $max -> $num"\
&& max=$num
...
3
votes
3
answers
12k
views
Piping in awk scripts
I'm trying to write a ls wrapper that uses awk to parse the output of ls -lhF. Right now I've split the program into two files - my_ls.sh and my_ls.awk. my_ls.sh's only purpose is to pipe the output ...
5
votes
2
answers
3k
views
Which programs use a file descriptor higher than 2?
Standard file descriptors <= 2 are opened by default. A program can write to or read from, a file descriptor after 2, without using the open system call to obtain such a descriptor. The program ...
1
vote
4
answers
5k
views
How to pipe fd 3 to consumer instead of fd1 / fd2
I was hoping to do something like this:
echo 'foo' >&3 3| cat
Basically, I want to write 'foo' to 3, and then only pipe the data in 3 to cat. But the above doesn't work, I get:
bash: 3: Bad ...
4
votes
3
answers
4k
views
Howto run interactive commands, as another user
I'm trying to write a bash script that
context switches to another user (root, in this case)
executes a set of commands to i. create a user (interactively prompting invoker for username) then ii. ...
4
votes
2
answers
5k
views
Pass a command to ROOT from a shell script and having it stay open
I am writing a bash script to perform some analysis using the program ROOT. I want to run some initial command to load the result of the analysis, then continue using ROOT interactively.
The analysis ...
3
votes
3
answers
2k
views
Please help explain this bash output redirection
This script was posted as answer to a Question.
And I'm trying to work out what's going on.
result=$(
{
{
ssh host app-status >&3 3>&-; echo "$?"
} | {
until read -...
4
votes
1
answer
3k
views
Is it safe to use fd 3?
There are several questions that use it to do things like multiple output pipes without using process substitution:
Is there a way to pipe the output of one program into two other programs?
Swapping ...
3
votes
2
answers
2k
views
bash: loop through two string variables to create symbolic links
I have managed to create two bash variables, both of which are lines of strings.
$oldfilenames which looks like
path/old-filename-1.txt
path/old-filename-2.txt
path/old-filename-3.txt
path/old-...
3
votes
3
answers
919
views
Print a message from the left-hand side of a pipe
I want to use a variable from the main shell in a sub shell. I did:
export mysql_root_password="test"
(
echo $mysql_root_password
) | dialog --gauge "Working hard..." 6 50
There is no output. Of ...
4
votes
1
answer
1k
views
Is there a way to bypass the pipe in a shell script or function?
I would like to write a small wrapper around ps so that no matter what the options are or if I grep the result, I could have the first line of the output of ps which tells me what the columns are.
...
3
votes
1
answer
4k
views
Write data to both a file and a serial port — can I use tee?
I am trying to take the input from a serial port and write it to a file and also then read the file and send it back out the serial port to the host computer. A coworker suggested using the "tee" ...
1
vote
3
answers
3k
views
How to read an open file descriptor from outside the writing process
How do you open a file descriptor and echo it to a terminal while it's being written to from a process?
I have a backup program, Duplicity, that writes its logs to a file descriptor specified by the -...