-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.sh
53 lines (46 loc) · 1 KB
/
file.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
pipe_to_fd() {
# make sure fds are valid
for ((i=1; i<=$#; ++i)); do
{ >&"${!i}"; } 2> /dev/null ||
bail "tee_fd: Invalid fd (${!i}) given"
done
while true; do
if IFS= read -r line; then
for ((i=1; i<=$#; ++i)); do
echo "${line}" >&"${!i}";
done
[ "${tee:-0}" = 0 ] ||
echo "${line}"
else
break;
fi;
done
}
pipe_to_file() {
while true; do
if IFS= read -r line; then
for ((i=1; i<=$#; ++i)); do
echo "${line}" >>"${!i}";
done
[ "${tee:-0}" = 0 ] ||
echo "${line}"
else
break;
fi;
done
}
is_fd() {
[[ "$1" =~ ^[0-9]+$ ]] && {
{ : >&"$1"; } 2> /dev/null
}
}
unused_fd() {
: "${__max_fd:="$(ulimit -n)"}"
for ((i=0; i<"$__max_fd"; ++i)); do
is_fd "$i" || {
echo -n "$i"
break;
}
done
}