subshell

Jenkins pipeline sh does not seem to respect pipe in shell command

一曲冷凌霜 提交于 2019-11-27 23:43:47
问题 I am using a Jenkinsfile in a pipeline on version 2.32.2. For various reasons I want to extract the version string from the pom. I was hoping I wouldn't have to add the maven help plugin and use evaluate. I quickly came up with a little sed expression to get it out of the pom which uses pipes and works on the commandline in the jenkins workspace on the executor. $ sed -n '/<version>/,/<version/p' pom.xml | head -1 | sed 's/[[:blank:]]*<\/*version>//g' 1.0.0-SNAPSHOT It could probably be

Get exit code from subshell through the pipes

只愿长相守 提交于 2019-11-27 22:16:15
How can I get exit code of wget from the subshell process? So, main problem is that $? is equal 0. Where can $?=8 be founded? $> OUT=$( wget -q "http://budueba.com/net" | tee -a "file.txt" ); echo "$?" 0 It works without tee , actually. $> OUT=$( wget -q "http://budueba.com/net" ); echo "$?" 8 But ${PIPESTATUS} array (I'm not sure it's related to that case) also does not contain that value. $> OUT=$( wget -q "http://budueba.com/net" | tee -a "file.txt" ); echo "${PIPESTATUS[1]}" $> OUT=$( wget -q "http://budueba.com/net" | tee -a "file.txt" ); echo "${PIPESTATUS[0]}" 0 $> OUT=$( wget -q "http:

When does command substitution spawn more subshells than the same commands in isolation?

笑着哭i 提交于 2019-11-27 11:49:44
Yesterday it was suggested to me that using command substitution in bash causes an unnecessary subshell to be spawned. The advice was specific to this use case : # Extra subshell spawned foo=$(command; echo $?) # No extra subshell command foo=$? As best I can figure this appears to be correct for this use case. However, a quick search trying to verify this leads to reams of confusing and contradictory advice. It seems popular wisdom says ALL usage of command substitution will spawn a subshell. For example: The command substitution expands to the output of commands. These commands are executed

Pipe multiple commands into a single command

微笑、不失礼 提交于 2019-11-27 11:38:57
How can I pipe the stdout of multiple commands to a single command? Example 1: combine and sort the output of all three echo commands: echo zzz; echo aaa; echo kkk desired output: aaa kkk zzz Example 2: rewrite the following so that all the commands are in a single command-line using pipes, without redirects to a temp file: setopt > /tmp/foo; unsetopt >> /tmp/foo; set >> /tmp/foo; sort /tmp/foo Use parentheses ()'s to combine the commands into a single process, which will concatenate the stdout of each of them. Example 1 (note that $ is the shell prompt): $ (echo zzz; echo aaa; echo kkk) |

Left side of pipe is the subshell?

痞子三分冷 提交于 2019-11-27 08:43:33
Edit: My comment below regarding sed 's@^@ @' <(f1) is incorrect While $BASH_SUBSHELL indicates that we are in the same level as the launch, the variables are lost in the main script. based on Gordons answer I tested f1 > >(sed 's@^@ @') instead and that seems to work correctly. Still, shouldn't BASH_SUBSHELL should be 1 and not 0 for the first form? Consider this small test #!/bin/bash declare -i i=0 function f1() { let i++ echo "In f1, SUBSHELL: $BASH_SUBSHELL, i=$i" >&2 } f1 f1 | sed 's@^@ @' echo "at end, i=$i" with the following output: In f1, SUBSHELL: 0, i=1 In f1, SUBSHELL: 1, i=2 at

Is there a way to write a bash function which aborts the whole execution, no matter how it is called?

天涯浪子 提交于 2019-11-27 03:00:27
I was using "exit 1" statement in my bash functions to terminate the whole script and it worked fine: function func() { echo "Goodbye" exit 1 } echo "Function call will abort" func echo "This will never be printed" But then I realized that it doesn't do the work when called like: res=$(func) I understand that I created a subshell and "exit 1" aborts that subshell and not the primary one.... But is there a way to write a function which aborts the whole execution, no matter how it is called? I just need to get the real return value (echoed by the function). What you could do, is register the top

Get exit code from subshell through the pipes

两盒软妹~` 提交于 2019-11-26 20:56:37
问题 How can I get exit code of wget from the subshell process? So, main problem is that $? is equal 0. Where can $?=8 be founded? $> OUT=$( wget -q "http://budueba.com/net" | tee -a "file.txt" ); echo "$?" 0 It works without tee , actually. $> OUT=$( wget -q "http://budueba.com/net" ); echo "$?" 8 But ${PIPESTATUS} array (I'm not sure it's related to that case) also does not contain that value. $> OUT=$( wget -q "http://budueba.com/net" | tee -a "file.txt" ); echo "${PIPESTATUS[1]}" $> OUT=$(

When does command substitution spawn more subshells than the same commands in isolation?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 18:06:10
问题 Yesterday it was suggested to me that using command substitution in bash causes an unnecessary subshell to be spawned. The advice was specific to this use case: # Extra subshell spawned foo=$(command; echo $?) # No extra subshell command foo=$? As best I can figure this appears to be correct for this use case. However, a quick search trying to verify this leads to reams of confusing and contradictory advice. It seems popular wisdom says ALL usage of command substitution will spawn a subshell.

Pipe multiple commands into a single command

倖福魔咒の 提交于 2019-11-26 15:42:58
问题 How can I pipe the stdout of multiple commands to a single command? Example 1: combine and sort the output of all three echo commands: echo zzz; echo aaa; echo kkk desired output: aaa kkk zzz Example 2: rewrite the following so that all the commands are in a single command-line using pipes, without redirects to a temp file: setopt > /tmp/foo; unsetopt >> /tmp/foo; set >> /tmp/foo; sort /tmp/foo 回答1: Use parentheses ()'s to combine the commands into a single process, which will concatenate the

Is there a way to write a bash function which aborts the whole execution, no matter how it is called?

☆樱花仙子☆ 提交于 2019-11-26 12:36:59
问题 I was using \"exit 1\" statement in my bash functions to terminate the whole script and it worked fine: function func() { echo \"Goodbye\" exit 1 } echo \"Function call will abort\" func echo \"This will never be printed\" But then I realized that it doesn\'t do the work when called like: res=$(func) I understand that I created a subshell and \"exit 1\" aborts that subshell and not the primary one.... But is there a way to write a function which aborts the whole execution, no matter how it is