Shell变量案例
1. 把以下的字符串中长度小于5的打印出来
When find examines or prints information a file
方法一: 了解
[root@shell /scripts]
When
find
or
a
file
方法二:
[root@shell /scripts]
#!/bin/bash
for i in When find examines or prints information a file
do
[ ${#i} -lt 5 ] && echo $i
done
[root@shell /scripts]
When
find
or
a
file
Shell变量运算
加减乘除 求余
num1 + num2
num1 - num2
num1 * num2
num1 / num2
num1 % num2
整数运算
expr
$(())
$[]
let
只支持整数运算,不支持小数运算
expr 数值之间必须要有空格进行分开,当使用*乘的时候,需要对其进行转义使用,不能进行求方运算
[root@shell /scripts]
2
[root@shell /scripts]
[root@shell /scripts]
[root@shell /scripts]
15
[root@shell /scripts]
5
[root@shell /scripts]
expr: syntax error
[root@shell /scripts]
50
[root@shell /scripts]
2
[root@shell /scripts]
0
[root@shell /scripts]
[root@shell /scripts]
1
$(()) 没有严格格式要求,不能进行求方运算
[root@shell /scripts]
16
[root@shell /scripts]
16
[root@shell /scripts]
6
[root@shell /scripts]
55
[root@shell /scripts]
2
[root@shell /scripts]
1
$[] 没有严格格式要求,不能进行求方运算
[root@shell /scripts]
16
[root@shell /scripts]
2
[root@shell /scripts]
6
[root@shell /scripts]
55
[root@shell /scripts]
2
[root@shell /scripts]
1
let 计数
[root@shell /scripts]
[root@shell /scripts]
[root@shell /scripts]
[root@shell /scripts]
[root@shell /scripts]
13
[root@shell /scripts]
[root@shell /scripts]
12
[root@shell /scripts]
12
[root@shell /scripts]
[root@shell /scripts]
11
小数运算
bc awk python
bc
[root@shell /scripts]
[root@shell /scripts]
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1+1
2
3/2
1
^C
(interrupt) Exiting bc.
[root@shell /scripts]# echo 10 + 20 | bc
30
[root@shell /scripts]# echo $num1 + $num2 | bc
16
[root@shell /scripts]# echo $num1 / $num2 | bc
2
[root@shell /scripts]# echo "scale=2;$num1 / $num2" | bc
2.20
[root@shell /scripts]# echo "scale=1;$num1 / $num2" | bc
2.2
[root@shell /scripts]# echo "scale=5;$num1 / $num2" | bc
2.20000
[root@shell /scripts]# echo "$num1 ^ $num2" | bc #求方运算
161051
awk
[root@shell /scripts]# awk 'BEGIN{print 10 + 5}'
15
[root@shell /scripts]# awk 'BEGIN{print 10 - 5}'
5
[root@shell /scripts]# awk 'BEGIN{print 10 * 5}'
50
[root@shell /scripts]# awk 'BEGIN{print 10 / 5}'
2
[root@shell /scripts]# awk 'BEGIN{print 10 % 5}'
0
[root@shell /scripts]# awk 'BEGIN{print 10 ^ 5}'
100000
[root@shell /scripts]# awk 'BEGIN{print 11 ^ 5}'
161051
[root@shell /scripts]# awk 'BEGIN{print 11 / 5}'
2.2
[root@shell /scripts]# awk 'BEGIN{print 10 / 3}'
3.33333
[root@shell /scripts]# awk 'BEGIN{printf "%.3f\n",10/3}'
3.333
[root@shell /scripts]# awk 'BEGIN{printf "%.3f",10/3}'
3.333[root@shell /scripts]#
[root@shell /scripts]# awk "BEGIN{print $num1/$num2}"
2.2
[root@shell /scripts]# awk -vnum1=10 -vnum2=5 'BEGIN{print num1/num2}'
2
-v #自定义内部变量
BEGIN 行处理前
python
[root@shell /scripts]# python
Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1+1
2
>>> 10^10
0
>>> 10/3
3
>>> 10.0/3
3.3333333333333335
>>>
KeyboardInterrupt
>>> quit
Use quit() or Ctrl-D (i.e. EOF) to exit
>>> quit()
[root@shell /scripts]# echo "print $num1 + $num2" | python
16
[root@shell /scripts]# echo "print $num1 - $num2" | python
6
[root@shell /scripts]# echo "print $num1 ^ $num2" | python
14
[root@shell /scripts]# echo "print $num1 / $num2" | python
2
[root@shell /scripts]# echo "print ${num1}.0 / $num2" | python
2.2
变量运算案例
1. ps aux命令下,求VSZ列的和
[root@shell /scripts]
4672184
[root@shell /scripts]
4442724
[root@shell /scripts]
#!/bin/bash
he=0
for i in $(ps aux| awk 'NR>1{print $5}')
do
he=$(expr $he + $i)
done
echo $he
2. 写个脚本,实现一个简单的计算器,实现加减乘除
[root@shell /scripts]
请输入你要计算的第一个数字:10
请输入你要计算的第二个数字:10
10 + 10 = 20
10 - 10 = 0
10 * 10 = 100
10 / 10 = 1
[root@shell /scripts]
#!/bin/bash
read -p "请输入你要计算的第一个数字:" num1
read -p "请输入你要计算的第二个数字:" num2
echo "$num1 + $num2 = $[ $num1 + $num2 ]"
echo "$num1 - $num2 = $[ $num1 - $num2 ]"
echo "$num1 * $num2 = $[ $num1 * $num2 ]"
echo "$num1 / $num2 = $(awk "BEGIN{print $num1 / $num2 }")"
Shell变量案例
需求描述:变量string="Bigdata process is Hadoop, Hadoop is open source project",执行脚本后,打印输出string变量,并给出用户以下选项:
1)打印string长度
2)删除字符串中所有的Hadoop
3)替换第一个Hadoop为Linux
4)替换全部Hadoop为Linux
用户请输入数字1|2|3|4,可以执行对应项的功能。
#!/bin/bash
string="Bigdata process is Hadoop, Hadoop is open source project"
echo "语句为:$string"
echo -e "\033[44;37m 1)打印string长度\n2)删除字符串中所有的Hadoop\n3)替换第一个Hadoop为Linux\n4)替换全部Hadoop为Linux\n6)退出 \033[0m"
while :
do
read -p "请输入:[1|2|3|4|6]::" qq
if [ $qq -eq 1 ];then
echo "${#string}"
elif [ $qq -eq 2 ];then
echo "${string//Hadoop/}"
elif [ $qq -eq 3 ];then
echo "${string/Hadoop/Linux}"
elif [ $qq -eq 4 ];then
echo "${string//Hadoop/Linux}"
elif [ $qq -eq 6 ];then
exit
else
echo "输入错误"
fi
done