linux shell exec file descriptor associated

When writing shell scripts, if the input or output of more commands are the same file, and the path and name of this file is very long, you need to write a lot of times the same path would be a waste of time, we can use the exec command a custom file descriptor associated to a specific file.

 

execl open file descriptor syntax is

# Outputfile open file and associate it to a file descriptor FD 

# manner as to cover the open 
Exec. 3 > outputfile 

# open for appending 
Exec. 4 >> outputfile1 

# copy a file descriptor already exists 
Exec. 5 > & . 4 

after finished using # Close file descriptor 
Exec. 4 > & -

Special attention fd>, fd >>, and> & middle fd are no spaces.

#!/bin/bash

#exec.sh

echo "Open file descriptor 3(overwrite mode), which is associated with file log"
exec 3> log

echo "Open file descriptor 4(append  mode), which is associated with file log_1"
exec 4>> log_1

echo "Open file descriptor 5, which is associated with file descriptor 5"
exec 5>& 3


echo "sending some data..."
echo "exec test log" 1 >& 3

echo "exec test  log_1" 1 >& 4

echo "exec test  log_2" 1 >& 5

echo "Closing fd 3..."
exec 3>&-

echo "Closing fd 4..."
exec 4>&-

echo "Closing fd 5..."
exec 5>&-
~                 

 

Guess you like

Origin www.cnblogs.com/tid-think/p/10961973.html