Bash Redirect

Write and read from a pipe using fd=3:

exec 3< <(echo hi)
cat <&3
exec 3>&-

Read and write from a pipe using fd=4:

exec 4> >(cat)
echo hi >&4
exec 4>&-

Check the current file descriptors for current shell:

lsof -p $$

Keep the mysql connection open using fd=3 for writing:

exec 3> >(mysql)
echo "SELECT 1;" >&3
echo "SELECT 2;" >&3
exec 3>&-

Keep the mysql connection open using fd=3 for reading:

exec 3< <(echo "SELECT 1;"|mysql|sed '1d')
while read <&3
do
  echo $REPLY
done

Note: sed '1d' removes the header.

Split input:

exec 3> >(awk '{print 3,$1}')
exec 4> >(awk '{print 4,$1}')
echo hi|tee >(cat >&3) >(cat >&4)|cat >/dev/null

produces:

3 hi
4 hi


bash

117 Words

2010-08-13 11:02 +0000