subshell

Limiting the number of subshells spawned

℡╲_俬逩灬. 提交于 2019-12-07 12:12:34
问题 I'm trying to limit the amount of subshells that are spawned in a script that I'm using to sniff our internal network to audit Linux servers in our network. The script works as intended, but due to the way I'm nesting the for loop, it spawns 255 Sub Shells for each network, so therefore it kills the CPU due to the fact that there are over 1000 processes spawned off. I need to be able to limit the amount of processes, and since variables lose their value when in a Sub Shell, I can't figure out

Bash subshell for setting SHELLOPTS in a script

…衆ロ難τιáo~ 提交于 2019-12-06 09:00:54
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 also includes invoking it in a subshell. From what I understand, the variables and environment of the

Limiting the number of subshells spawned

徘徊边缘 提交于 2019-12-05 21:54:15
I'm trying to limit the amount of subshells that are spawned in a script that I'm using to sniff our internal network to audit Linux servers in our network. The script works as intended, but due to the way I'm nesting the for loop, it spawns 255 Sub Shells for each network, so therefore it kills the CPU due to the fact that there are over 1000 processes spawned off. I need to be able to limit the amount of processes, and since variables lose their value when in a Sub Shell, I can't figure out a way to make this work. Again, the script works, it just spawns a ton a processes - I need to limit

Does trap work as expected while piping?

给你一囗甜甜゛ 提交于 2019-12-05 04:32:22
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 everything" output ===== subshell trap = on start: = after set new: trap -- 'echo func-EXIT' EXIT func

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

Why does ssh wait for my subshells without -t, and kill them with -t?

痞子三分冷 提交于 2019-12-03 12:29:58
问题 I have a bash script start.sh which looks like this: for thing in foo bar; do { background_processor $thing cleanup_on_exit $thing } & done This does what I want: I run start.sh, it exits with code 0, and the two subshells run in the background. Each subshell runs background_processor , and when that exits, it runs cleanup_on_exit . This works even if I exit the terminal from which I originally ran start.sh (even if that was an ssh connection). Then I tried this: ssh user@host "start.sh" This

Bash bad substitution with subshell and substring

爷,独闯天下 提交于 2019-12-03 09:59:07
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? 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 bit in the construct for BAZ is BAR and not the value that you want to take the first character of. So even if

Why does ssh wait for my subshells without -t, and kill them with -t?

我只是一个虾纸丫 提交于 2019-12-03 01:59:51
I have a bash script start.sh which looks like this: for thing in foo bar; do { background_processor $thing cleanup_on_exit $thing } & done This does what I want: I run start.sh, it exits with code 0, and the two subshells run in the background. Each subshell runs background_processor , and when that exits, it runs cleanup_on_exit . This works even if I exit the terminal from which I originally ran start.sh (even if that was an ssh connection). Then I tried this: ssh user@host "start.sh" This works, except that after start.sh has exited, ssh apparently also waits for the subshells to exit. I

When are bash variables exported to subshells and/or accessible by scripts?

时光怂恿深爱的人放手 提交于 2019-11-30 02:22:28
问题 I'm confused over whether bash variables are exported to subshells and when they are accessible by scripts. My experience so far led me to believe that bash variables are automatically available to subshells. E.g.: > FOO=bar > echo $FOO bar > (echo $FOO) bar The above appears to demonstrate that bash variables are accessible in subshells. Given this script: #! /usr/bin/bash # c.sh func() { echo before echo ${FOO} echo after } func I understand that calling the script in the current shell

git branch vs $(git branch)

眉间皱痕 提交于 2019-11-28 02:23:46
When I execute git branch on the command line I get a list of all the branches on a repo, however when I execute $(git branch) in a sub-shell, it first prints out a list of files in the top level folder in a repo before printing out the branch names. Why? I'm basically trying to iterate over the branches using a for loop, but the listing of files breaks my script. for i in $(git branch); do echo $i done $ git branch * master Try echo * master in your shell and see what you get? Hint: You'll get the list of files in the current directory from the shell glob expansion of * . See