mirror of https://github.com/tLDP/LDP
41 lines
1.2 KiB
Bash
41 lines
1.2 KiB
Bash
#!/bin/bash
|
|
# Exercising "eval" ...
|
|
|
|
y=`eval ls -l` # Similar to y=`ls -l`
|
|
echo $y #+ but linefeeds removed because "echoed" variable is unquoted.
|
|
echo
|
|
echo "$y" # Linefeeds preserved when variable is quoted.
|
|
|
|
echo; echo
|
|
|
|
y=`eval df` # Similar to y=`df`
|
|
echo $y #+ but linefeeds removed.
|
|
|
|
# When LF's not preserved, it may make it easier to parse output,
|
|
#+ using utilities such as "awk".
|
|
|
|
echo
|
|
echo "==========================================================="
|
|
echo
|
|
|
|
eval "`seq 3 | sed -e 's/.*/echo var&=ABCDEFGHIJ/'`"
|
|
# var1=ABCDEFGHIJ
|
|
# var2=ABCDEFGHIJ
|
|
# var3=ABCDEFGHIJ
|
|
|
|
echo
|
|
echo "==========================================================="
|
|
echo
|
|
|
|
|
|
# Now, showing how to do something useful with "eval" . . .
|
|
# (Thank you, E. Choroba!)
|
|
|
|
version=3.4 # Can we split the version into major and minor
|
|
#+ part in one command?
|
|
echo "version = $version"
|
|
eval major=${version/./;minor=} # Replaces '.' in version by ';minor='
|
|
# The substitution yields '3; minor=4'
|
|
#+ so eval does minor=4, major=3
|
|
echo Major: $major, minor: $minor # Major: 3, minor: 4
|