Getting an “ambiguous redirect” error

Bash can be pretty obtuse sometimes. The following commands all return different error messages for basically the same error: $ echo hello > bash: syntax error near unexpected token `newline` $ echo hello > ${NONEXISTENT} bash: ${NONEXISTENT}: ambiguous redirect $ echo hello > “${NONEXISTENT}” bash: : No such file or directory Adding quotes around the … Read more

Parallelize Bash script with maximum number of processes

Depending on what you want to do xargs also can help (here: converting documents with pdf2ps): cpus=$( ls -d /sys/devices/system/cpu/cpu[[:digit:]]* | wc -w ) find . -name \*.pdf | xargs –max-args=1 –max-procs=$cpus pdf2ps From the docs: –max-procs=max-procs -P max-procs Run up to max-procs processes at a time; the default is 1. If max-procs is 0, … Read more

How to calculate time elapsed in bash script?

Bash has a handy SECONDS builtin variable that tracks the number of seconds that have passed since the shell was started. This variable retains its properties when assigned to, and the value returned after the assignment is the number of seconds since the assignment plus the assigned value. Thus, you can just set SECONDS to … Read more