Guest User

Untitled

a guest
Oct 19th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. exec 3<> /tmp/foo # open fd 3.
  2. echo a >&3 # write to it
  3. exec 3>&- # close fd 3.
  4.  
  5. FILE=/tmp/foo
  6. echo a > "$FILE"
  7.  
  8. while IFS= read -r line; do
  9. printf '%sn' "$line"
  10. if IFS= read -r line; then printf '%sn' "$line" >&3; fi
  11. done >odd.txt 3>even.txt
  12.  
  13. { while … done | odd-filter >filtered-odd.txt; } 3>&1 | even-filter >filtered-even.txt
  14.  
  15. exec 8<&0 9>&1
  16. exec >output12
  17. command1
  18. exec <input23
  19. command2
  20. exec >&9
  21. command3
  22. exec <&8
  23.  
  24. (
  25. rm -f fifo
  26. mkfifo fifo
  27. exec 3<fifo # open fifo for reading
  28. trap "exit" 1 2 3 15
  29. exec cat fifo | nl
  30. ) &
  31. bpid=$!
  32.  
  33. (
  34. exec 3>fifo # open fifo for writing
  35. trap "exit" 1 2 3 15
  36. while true;
  37. do
  38. echo "blah" > fifo
  39. done
  40. )
  41. #kill -TERM $bpid
  42.  
  43. #!/bin/bash
  44.  
  45. log() {
  46. echo $* >&3
  47. }
  48. info() {
  49. echo $* >&4
  50. }
  51. err() {
  52. echo $* >&2
  53. }
  54. debug() {
  55. echo $* >&5
  56. }
  57.  
  58. VERBOSE=1
  59.  
  60. while [[ $# -gt 0 ]]; do
  61. ARG=$1
  62. shift
  63. case $ARG in
  64. "-vv")
  65. VERBOSE=3
  66. ;;
  67. "-v")
  68. VERBOSE=2
  69. ;;
  70. "-q")
  71. VERBOSE=0
  72. ;;
  73. # More flags
  74. *)
  75. echo -n
  76. # Linear args
  77. ;;
  78. esac
  79. done
  80.  
  81. for i in 1 2 3; do
  82. fd=$(expr 2 + $i)
  83. if [[ $VERBOSE -ge $i ]]; then
  84. eval "exec $fd>&1"
  85. else
  86. eval "exec $fd> /dev/null"
  87. fi
  88. done
  89.  
  90. err "This will _always_ show up."
  91. log "This is normally displayed, but can be prevented with -q"
  92. info "This will only show up if -v is passed"
  93. debug "This will show up for -vv"
  94.  
  95. env -i bash --norc # clean up environment
  96. set +o history
  97. read -s -p "Enter your password: " passwd
  98. exec 3<<<"$passwd"
  99. mycommand <&3 # cat /dev/stdin in mycommand
  100.  
  101. arg1 string to echo
  102. arg2 flag 0,1 print or not print to 3rd fd stdout descriptor
  103. function ecko3 {
  104. if [ "$2" -eq 1 ]; then
  105. exec 3>$(tty)
  106. echo -en "$1" | tee >(cat - >&3)
  107. exec 3>&-
  108. else
  109. echo -en "$1"
  110. fi
  111. }
  112.  
  113. #exit if we can't obtain the lock
  114. set -e
  115.  
  116. #open file descriptor 3 for writing
  117. exec 3> /tmp/file.lock
  118.  
  119. #create an exclusive lock on the file using file descriptor 3
  120. #exit if lock could not be obtained
  121. flock -n 3
  122.  
  123. #execute serial code
  124.  
  125. #close the open file handle which releases the file lock
  126. exec 3>&-
Add Comment
Please, Sign In to add comment