output redirection inside bsub command

旧时模样 提交于 2020-01-16 16:30:16

问题


Is it possible to use output redirection inside bsub command such as:

bsub -q short "cat <(head -2 myfile.txt) > outputfile.txt"

Currently this bsub execution fails. Also my attempts to escape the redirection sign and the parenthesis were all failed, such as:

bsub -q short "cat \<\(head -2 myfile.txt\) > outputfile.txt"
bsub -q short "cat <\(head -2 myfile.txt\) > outputfile.txt"

*Note, I'm well aware that the redirection in this simple command is not necessary as the command could easily be written as:

bsub -q short "head -2 myfile.txt > outputfile.txt" 

and then it would indeed be executed properly (without errors). I am however interested in implementing the redirection of output '<' within the context of a more composed command, and am bringing this simple command here as an example only.


回答1:


<(...) is process substitution -- a bash extension not available on baseline POSIX shells. system(), subprocess.Popen(..., shell=True) and similar calls use /bin/sh, which is not guaranteed to have such extensions.


As a mechanism that works with any possible command without needing to worry about how to correctly escape it into a string, you can export that function and any variables it uses through the environment:

# for the sake of example, moving filenames out-of-band
in_file=myfile.txt
out_file=outputfile.txt

mycmd() { cat <(head -2 <"$in_file") >"$out_file"; }

export -f mycmd                # export the function into the environment
export in_file out_file        # and also any variables it uses

bsub -q short 'bash -c mycmd'  # ...before telling bsub to invoke bash to run the function



回答2:


<(...) is a bash feature while your command runs with sh.

Invoke bash explicitly to handle your bash-only features:

bsub -q short "bash -c 'cat <(head -2 myfile.txt) > outputfile.txt'"


来源:https://stackoverflow.com/questions/50609898/output-redirection-inside-bsub-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!