I have the following code:
#!/usr/bin/env bash
set -o errexit
set -o nounset
declare LOOP_STEPS=2000
declare -r REQUIRED_STRING="required time"
declare day
declare -i end
declare i
declare minutes
declare seconds
declare -i start
declare -i total
function doTests {
start=$(date +%s)
for i in $(seq ${LOOP_STEPS}) ; do
read minutes seconds day < <(date '+%M %S %d')
done
end=$(date +%s)
total=${end}-${start}
echo "${REQUIRED_STRING} values with read: ${total}"
echo
}
start=$(date +%s)
for i in $(seq ${LOOP_STEPS}) ; do
read minutes seconds day < <(date '+%M %S %d')
done
end=$(date +%s)
total=${end}-${start}
echo "${REQUIRED_STRING} values with read: ${total}"
doTests
When doTests is called (which has the same code as executed before the
call) I get:
required time values with read: 4
/home/cecil/bin/test2.sh: redirection error: cannot duplicate fd: Too many open files
/home/cecil/bin/test2.sh: cannot make pipe for process substitution: Too many open files
/home/cecil/bin/test2.sh: cannot make pipe for process substitution: Too many open files
/home/cecil/bin/test2.sh: line 20: <(date '+%M %S %d'): ambiguous redirect
So when the code is put at the top level it works okay, but when put
into a function there is an error. Why is this?
By the way, when I remove the code before the function call, it still
goes wrong, So it is not because the code is executed a second time.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn:
http://www.linkedin.com/in/cecilwesterhof