"Shell Programming Practice" Chapter 3 Shell Variable Fundamentals (Part 2)

1. Two ways to set login prompt
(1) /etc/motd
[root@thzzc1994 thzzc]# echo this is thzzc test linux >/etc/motd
[root@thzzc1994 thzzc]# cat /etc/motd
this is thzzc test linux
(2) /etc/profile.d
[root@thzzc1994 home]# echo echo this is thzzc test linux >/etc/profile.d/test.sh
[root@thzzc1994 home]# cat /etc/profile.d/ test.sh
echo this is thzzc test linux
2. Example of the difference between single quotes, double quotes, and no quotes
(1) [root@thzzc1994 ~]# cat test.sh
a=192.168.1.1
b='192.168.1.1'
c ="192.168.1.1"
echo a=$a
echo b=$b
echo c=$c
[root@thzzc1994 ~]# sh test.sh
a=192.168.1.1
b=192.168.1.1
c=192.168.1.1
(2) [root@thzzc1994 ~]# cat test.sh
a=192.168.1.1
a=192.168.1.1-$a
b='192.168.1.1-$a'
c="192.168.1.1-$a"
echo a=$a
echo b=$b
echo c=$c
[root @thzzc1994 ~]# sh test.sh
a=192.168.1.1-192.168.1.1
b=192.168.1.1-$a
c=192.168.1.1-192.168.1.1-192.168.1.1
3. Abnormal single and double quotes in awk
[root@thzzc1994 ~]# oldboy=123
[root@thzzc1994 ~]# awk 'BEGIN {print "$oldboy"}'
$oldboy
[root@thzzc1994 ~]# awk 'BEGIN {print $oldboy}'

[root@thzzc1994 ~]# awk 'BEGIN {print '$oldboy'}'
123
[root@thzzc1994 ~]# awk 'BEGIN {print "'$oldboy'"}'
123
When using awk, the situation is just the opposite, single quotes , double + single parsing variables, double quotes are output as they are. Nothing is added and the output is empty.
Summary: If you want to output as it is, use double quotes, and use double + single ('xxx'") for parsing variables.
For convenience, awk is generally not used directly. Instead, the pipeline method is used, echo the variable first, and then let the pipeline Pass parameters through $0.
[root@thzzc1994 ~]# oldboy=123
[root@thzzc1994 ~]# echo $oldboy |awk '{print $0}'
123
[root@thzzc1994 ~]# echo '$oldboy' |awk '{ print $0}'
$oldboy
[root@thzzc1994 ~]# echo "$oldboy" |awk '{print $0}'
123
Both sed and grep are consistent with the previous conclusions, but awk is weird. Awk is really a language!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325458697&siteId=291194637