subshell

Does trap work as expected while piping?

无人久伴 提交于 2019-12-22 04:42:23
问题 Here is minimal code for issue demonstration: http://pastebin.com/5TXDpSh5 #!/bin/bash set -e set -o pipefail function echoTraps() { echo "= on start:" trap -p trap -- 'echo func-EXIT' EXIT echo "= after set new:" trap -p # we can ensure after script done - file '/tmp/tmp.txt' was not created trap -- 'echo SIG 1>/tmp/tmp.txt' SIGPIPE SIGHUP SIGINT SIGQUIT SIGTERM } trap -- 'echo main-EXIT1' EXIT echo "===== subshell trap" ( echoTraps; ) echo "===== pipe trap" echoTraps | cat echo "===== done

Bash bad substitution with subshell and substring

蹲街弑〆低调 提交于 2019-12-21 03:19:18
问题 A contrived example... given FOO="/foo/bar/baz" this works (in bash) BAR=$(basename $FOO) # result is BAR="baz" BAZ=${BAR:0:1} # result is BAZ="b" this doesn't BAZ=${$(basename $FOO):0:1} # result is bad substitution My question is which rule causes this [subshell substitution] to evaluate incorrectly? And what is the correct way, if any, to do this in 1 hop? 回答1: First off, note that when you say this: BAR=$(basename $FOO) # result is BAR="baz" BAZ=${BAR:0:1} # result is BAZ="b" the first

Assign and use of a variable in the same subshell

倖福魔咒の 提交于 2019-12-17 20:45:31
问题 I was doing something very simple like: v=5 echo "$v" and expected it to print 5 . However, it does not. The value that was just set is not available for the next command. I recently learnt that " In most shells, each command of a pipeline is executed in a separate SubShell ". However, in this case both commands are being executed in the same subshell. Why does this happen? Is there any way to have this working? Full example: $ v=1 $ v=5 echo "$v" 1 # I expected 5! 回答1: Let's look to the

Having an issue passing variables to subshell

﹥>﹥吖頭↗ 提交于 2019-12-12 02:28:53
问题 So here is my problem, I have this script I wrote where I'm exporting two variables however they're not making it into the subshell. The point of this script is to change a users password and clear out their pam_tally for CentOS and Ubuntu hosts. A little background is that this environment's users are managed by puppet but the passwords are all local, ssh keys are not allowed either (this is set in stone and can't be changed so I have to work with what I got) and the reason is that every log

How to avoid subshell behaviour using while and find? [duplicate]

你离开我真会死。 提交于 2019-12-11 16:30:58
问题 This question already has answers here : While-loop subshell dilemma in Bash (4 answers) Closed 9 months ago . Because I trapped into this myself, I asked a question and did give also the answer here. If find iterates over what the command did find, this is executed from bash in a subshell. So you can not fill an array with results and use it after your loop. 回答1: You have to change the syntax from: i=0 find $cont_dirs_abs -type l -exec test -e {} \; -print0 | while IFS= read -r -d '' lxc

Why does command grouping with curly brackets cause two bash shells to be spawned?

夙愿已清 提交于 2019-12-11 05:34:54
问题 Consider the following command $ bash -c " sleep 10000 | sed 's/ Something//g '" The process tree due to that command looks like this; \-+= 69771 hbaba -bash \-+= 39225 hbaba bash -c sleep 10000 | sed 's/ Something//g ' |--- 39226 hbaba sleep 10000 \--- 39227 hbaba sed s/ Something//g Adding command grouping with curly brackets changes the process tree significantly. $ bash -c " { sleep 10000; } | sed 's/ Something//g '" The bash process is spawned twice \-+= 69771 hbaba -bash \-+= 39323

Why does tee wait for all subshells to finish?

流过昼夜 提交于 2019-12-10 21:20:23
问题 I have a server script that runs mysqld and forks to continue running. As an example: ./mysqld <parameters> & echo "Parent runs next line in script." <do more stuff> Why does tee wait for the child process to end before it ends itself? EDIT: For example, the following always hangs: ./myscript | tee -a logfile.log 回答1: Because it can't be sure it has tee'd all the output if the child process is still running (and still has its standard output open). Since the parent and child use the same

Difference between $() and () in Bash

落爺英雄遲暮 提交于 2019-12-10 12:36:51
问题 When I type ls -l $(echo file) output from bracket (which is just simple echo'ing) is taken and passed to external ls -l command. It equals to simple ls -l file . When I type ls -l (echo file) we have error because one cannot nest () inside external command. Can someone help me understand the difference between $() and () ? 回答1: $(cmd) substitutes the result of cmd as a string, whereas (cmd; cmd) run a list of commands in a subprocess. If you want to put the output of one or more commands

How can I use read timeouts with stat?

孤人 提交于 2019-12-10 11:56:33
问题 I have the following code: #!/bin/bash read -t1 < <(stat -t "/my/mountpoint") if [ $? -eq 1 ]; then echo NFS mount stale. Removing... umount -f -l /my/mountpoint fi How do I mute the output of stat while at the same time being still able to detect its error level in the subsequent test? Adding >/dev/null 2>&1 inside the subshell, or in the end of the read line does not work. But there must be a way... Thanks for any insights on this! 回答1: Use Command-Subsitution, Not Process Substitution

Bash subshell for setting SHELLOPTS in a script

雨燕双飞 提交于 2019-12-07 20:54:00
问题 This question is not cygwin specific. However, in the cygwin mail archive https://cygwin.com/ml/cygwin-announce/2010-08/msg00015.html are various instructions for setting the cygwin specific igncr shellopt variable and one of them is the instruction: 4a. For a single affected script, add this line just after the she-bang: ~ (set -o igncr) 2>/dev/null && set -o igncr; # comment is needed I understand that set -o igncr sets igncr in SHELLOPTS. However, I do not understand why the instruction