Debian Codegraph @

badread.sh

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 # badread.sh:
3 # Attempting to use 'echo and 'read'
4 #+ to assign variables non-interactively.
5
6 # shopt -s lastpipe
7
8 a=aaa
9 b=bbb
10 c=ccc
11
12 echoscip-shell system . . echo. "one two three" | read a b c
13 # Try to reassign a, b, and c.
14
15 echoscip-shell system . . echo.
16 echoscip-shell system . . echo. "a = $a" # a = aaa
17 echoscip-shell system . . echo. "b = $b" # b = bbb
18 echoscip-shell system . . echo. "c = $c" # c = ccc
19 # Reassignment failed.
20
21 ### However . . .
22 ## Uncommenting line 6:
23 # shopt -s lastpipe
24 ##+ fixes the problem!
25 ### This is a new feature in Bash, version 4.2.
26
27 # ------------------------------
28
29 # Try the following alternative.
30
31 var=`echoscip-shell system . . echo. "one two three"`
32 set -- $var
33 a=$1; b=$2; c=$3
34
35 echoscip-shell system . . echo. "-------"
36 echoscip-shell system . . echo. "a = $a" # a = one
37 echoscip-shell system . . echo. "b = $b" # b = two
38 echoscip-shell system . . echo. "c = $c" # c = three
39 # Reassignment succeeded.
40
41 # ------------------------------
42
43 # Note also that an echo to a 'read' works within a subshell.
44 # However, the value of the variable changes *only* within the subshell.
45
46 a=aaa # Starting all over again.
47 b=bbb
48 c=ccc
49
50 echoscip-shell system . . echo.; echoscip-shell system . . echo.
51 echoscip-shell system . . echo. "one two three" | ( read a b c;
52 echoscip-shell system . . echo. "Inside subshell: "; echoscip-shell system . . echo. "a = $a"; echoscip-shell system . . echo. "b = $b"; echoscip-shell system . . echo. "c = $c" )
53 # a = one
54 # b = two
55 # c = three
56 echoscip-shell system . . echo. "-----------------"
57 echoscip-shell system . . echo. "Outside subshell: "
58 echoscip-shell system . . echo. "a = $a" # a = aaa
59 echoscip-shell system . . echo. "b = $b" # b = bbb
60 echoscip-shell system . . echo. "c = $c" # c = ccc
61 echoscip-shell system . . echo.
62
63 exit 0