...
 
Commits (2)
#!/bin/sh
# Changing a variable in a subshell, then accessing it from the main program.
# WARNING: This will not work as expected.
# Try to count files in a subshell, which will fail, because the counter is not modified in the main program.
# shellcheck disable=SC2034,SC2030,SC2031
path="${1:-/tmp}"
match=${2:-test}
counter=0
# Pipeline 'find | while' spawns subshell, changes to $counter are lost.
find "$path" -type f |
grep -Fv systemd |
while read -r void ; do
counter=$((counter+1))
done
find "$path" -type f 2> /dev/null |
while read -r file ; do
grep -q "$match" "$file" && counter=$((counter+1))
done
echo "counter=$counter"
......
#!/bin/sh
# Using a FIFO for passing a value from a subshell to the main program.
# Count files using a FIFO for reporting from a subshell.
path="${1:-/tmp}"
match="${2:-test}"
fifodir=$(mktemp -d)
fifo="$fifodir/fifo"
......@@ -11,10 +12,8 @@ exec 3<>"$fifo"
counter=0
find "$path" -type f | while read -r f ; do
if echo "$f" | grep -v -q 'systemd' ; then
echo "$f" >&3
fi
find "$path" -type f 2> /dev/null | while read -r f ; do
grep -q "$match" "$f" && echo "$f" >&3
done
exec 3<"$fifo"
......
......@@ -2,16 +2,15 @@
# Bash only: Using command-redirection to pipe output into the main program's stdandard input.
path="${1:-/tmp}"
match="${2:-test}"
counter=0
while read -r void ; do
echo "$void" > /dev/null
while read -r ; do
counter=$((counter+1))
done < <(
find "$path" -type f |
grep -F -v systemd
find "$path" -type f -print0 2>/dev/null |
xargs -0 grep "$match"
)
echo "counter=$counter"
......
......@@ -2,13 +2,14 @@
# Using a temporary file to pass values from a subshell to the main program.
path="${1:-/tmp}"
match="${2:-test}"
counter=0
listfile="$(mktemp)"
exec 3> "$listfile"
( find "$path" -type f | grep -F -v systemd ) >&3
( find "$path" -type f -print0 2> /dev/null | xargs -0 grep "$match" ) >&3
exec 3< "$listfile"
......
......@@ -70,13 +70,15 @@ Table of Contents
Using Bash'S PIPESTATUS array.
### 6 [Subshells](./06-subshells/)
* 6.1 [Shell-Variable Version](./06-subshells/1-shell--variable-version.sh)
Changing a variable in a subshell, then accessing it from the main program.
* 6.2 [Fifo](./06-subshells/2-fifo.sh)
Using a FIFO for passing a value from a subshell to the main program.
* 6.3 [Command Redirection](./06-subshells/3-command-redirection.bash)
* 6.1 [Xargs](./06-subshells/1-xargs.sh)
Count files in "$1" that match "$2" using xargs for matching.
* 6.2 [Shell-Variable Version](./06-subshells/2-shell--variable-version.sh)
Try to count files in a subshell, which will fail, because the counter is not modified in the main program.
* 6.3 [Fifo](./06-subshells/3-fifo.sh)
Count files using a FIFO for reporting from a subshell.
* 6.4 [Command Redirection](./06-subshells/4-command-redirection.bash)
Bash only: Using command-redirection to pipe output into the main program's stdandard input.
* 6.4 [Tempfile](./06-subshells/4-tempfile.sh)
* 6.5 [Tempfile](./06-subshells/5-tempfile.sh)
Using a temporary file to pass values from a subshell to the main program.
### 7 [Terminal](./07-terminal/)
......