Debian Codegraph @

ex43.sh

raw symbols SCIP
1 #!/bin/bash
2 # Exercising "eval" ...
3
4 y=`eval ls -l` # Similar to y=`ls -l`
5 echo $y #+ but linefeeds removed because "echoed" variable is unquoted.
6 echo
7 echo "$y" # Linefeeds preserved when variable is quoted.
8
9 echo; echo
10
11 y=`eval df` # Similar to y=`df`
12 echo $y #+ but linefeeds removed.
13
14 # When LF's not preserved, it may make it easier to parse output,
15 #+ using utilities such as "awk".
16
17 echo
18 echo "==========================================================="
19 echo
20
21 eval "`seq 3 | sed -e 's/.*/echo var&=ABCDEFGHIJ/'`"
22 # var1=ABCDEFGHIJ
23 # var2=ABCDEFGHIJ
24 # var3=ABCDEFGHIJ
25
26 echo
27 echo "==========================================================="
28 echo
29
30
31 # Now, showing how to do something useful with "eval" . . .
32 # (Thank you, E. Choroba!)
33
34 version=3.4 # Can we split the version into major and minor
35 #+ part in one command?
36 echo "version = $version"
37 eval major=${version/./;minor=} # Replaces '.' in version by ';minor='
38 # The substitution yields '3; minor=4'
39 #+ so eval does minor=4, major=3
40 echo Major: $major, minor: $minor # Major: 3, minor: 4