process-substitution

Watch with Process Substitution

喜你入骨 提交于 2019-12-08 05:07:58
问题 I often run the command squeue -u $USER | tee >(wc -l) where squeue is a Slurm command to see how many jobs you are running. This gives me both the output from squeue and automatically tells how many lines are in it. How can I watch this command? watch -n.1 "squeue -u $USER | tee >(wc -l)" results in Every 0.1s: squeue -u randoms | tee >(wc -l) Wed May 9 14:46:36 2018 sh: -c: line 0: syntax error near unexpected token `(' sh: -c: line 0: `squeue -u randoms | tee >(wc -l)' 回答1: From the watch

POSIX shell equivalent to <()

十年热恋 提交于 2019-12-04 03:06:58
问题 <(commands ...) in bash/zsh makes the output behavior as a file. Does a POSIX equivalent exist? 回答1: mkfifo foo.fifo ## if your "commands" is multiple commands # { commands ...; } >foo.fifo & # otherwise, if it's just one commands ... >foo.fifo & something_else foo.fifo is the closest available equivalent to something_else <( commands ... ) 来源: https://stackoverflow.com/questions/38796224/posix-shell-equivalent-to

Bash setting a global variable inside a loop and retaining its value — Or process substituion for dummies

杀马特。学长 韩版系。学妹 提交于 2019-12-03 14:59:26
I'm a C/C++ programmer and quite stupid in general (or at least the way bash does things it makes me feel confused). I can't wrap my head around process substitution . I need to define a global boolean, set it somewhere in a loop, and make use of it in global scope. Could someone please explain in the simplest way possible how to adapt the code below to allow me to achieve my use case, simple enough so that I don't have to contort my brain again tomorrow to try and grasp process substitution . # DEFINE HERE for i in `seq 0 ${DAEMON_COUNT}`; do if [ ! -d "data$i" ]; then # SET HERE echo "data$i

How to find next available file descriptor in Bash?

寵の児 提交于 2019-12-03 12:55:44
问题 How can I figure out if a file descriptor is currently in use in Bash? For example, if I have a script that reads, writes, and closes fd 3, e.g. exec 3< <(some command here) ... cat <&3 exec 3>&- what's the best way to ensure I'm not interfering with some other purpose for the descriptor that may have been set before my script runs? Do I need to put my whole script in a subshell? 回答1: In pure bash , you can use the following method to see if a given file descriptor ( 3 in this case) is

Process substitution not allowed by Python's subprocess with shell=True?

吃可爱长大的小学妹 提交于 2019-12-02 23:13:26
问题 Here is a toy example of process substitution that works fine in Bash: $ wc -l <(pwd) 1 /proc/self/fd/11 So why does the same command give a syntax error when invoked from Python's subprocess with shell=True? >>> subprocess.check_call('wc -l <(pwd)', shell=True) /bin/sh: 1: Syntax error: "(" unexpected Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/path/to/my/python/lib/python3.5/subprocess.py", line 581, in check_call raise CalledProcessError(retcode, cmd)

Process substitution not allowed by Python's subprocess with shell=True?

和自甴很熟 提交于 2019-12-02 13:14:50
Here is a toy example of process substitution that works fine in Bash: $ wc -l <(pwd) 1 /proc/self/fd/11 So why does the same command give a syntax error when invoked from Python's subprocess with shell=True? >>> subprocess.check_call('wc -l <(pwd)', shell=True) /bin/sh: 1: Syntax error: "(" unexpected Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/path/to/my/python/lib/python3.5/subprocess.py", line 581, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command 'wc -l <(pwd)' returned non-zero exit status 2 /bin/sh: 1: Syntax

bash: How do I ensure termination of process substitution used with exec?

▼魔方 西西 提交于 2019-11-30 23:05:21
If I run $#/bin/bash for i in `seq 5`; do exec 3> >(sed -e "s/^/$i: /"; echo "$i-") echo foo >&3 echo bar >&3 exec 3>&- done then the result is not synchronous; it could be something like: 1: foo 1: bar 2: foo 2: bar 1- 3: foo 3: bar 2- 3- 4: foo 5: foo 4: bar 5: bar 4- 5- How do I ensure that the process substitution >(...) is completed before proceeding to the next iteration? Inserting sleep 0.1 after exec 3>&- helped, but it's inelegant, inefficient, and not guaranteed to always work. EDIT: The example may look silly, but it was for illustration only. What I'm doing is reading a stream of

Command substitution vs process substitution

心已入冬 提交于 2019-11-30 06:35:13
问题 I am trying to understand the differences between these two similar commands. aa=$(foo | bar | head -1) read aa < <(foo | bar | head -1) I know that <() requires #!/bin/bash , but does that make it slower? Do they create the same amount of subshells? Do they require the same amount of bash or sh processes? I am looking to use the command with the best performance. 回答1: No. Bash is faster in non-POSIX mode. It depends. This is both an implementation-specific and platform-specific detail. In

Get exit code of process substitution with pipe into while loop

流过昼夜 提交于 2019-11-29 06:34:03
The following script calls another program reading its output in a while loop (see Bash - How to pipe input to while loop and preserve variables after loop ends ): while read -r col0 col1; do # [...] done < <(other_program [args ...]) How can I check for the exit code of other_program to see if the loop was executed properly? mklement0 Note: ls -d / /nosuch is used as an example command below, because it fails (exit code 1 ) while still producing stdout output ( / ) (in addition to stderr output). Bash v4.2+ solution: ccarton's helpful answer works well in principle, but by default the while

Bash process substitution and syncing

半城伤御伤魂 提交于 2019-11-28 08:19:19
(Possibly related to Do some programs not accept process substitution for input files? ) In some Bash unit test scripts I'm using the following trick to log and display stdout and stderr of a command: command > >(tee "${stdoutF}") 2> >(tee "${stderrF}" >&2) This process produces some output to stdout, so the $stdoutF file gets some data. Then I run another command which does not output any data: diff -r "$source" "$target" > >(tee "${stdoutF}") 2> >(tee "${stderrF}" >&2) However, it doesn't look like this process always finishes successfully before the test for emptiness is run (using shunit