{{ cat }}'s docs

13.3.0: 脚本基础-变量及计算


1. shell脚本中的变量

适用情况

  • 某个字符串会被频繁使用或字符串长度很长时
  • 使用条件语句时
  • 引用某个命令的结果时
  • 写和用户交互的脚本时

1) 内置变量:

查看内置变量$@、$#、$0、$1、$2...

#!/bin/bash
##This is a demo shell scripts for testing of $0 $1 $2...
## Created by Zhao Peiwu 16/12/2014


echo "below is the preset var info"
echo "The scripts name is :$0"
echo "Total parameter number is $#"

[ "$#" -lt 2 ] && echo "The number you input is less than 2." && exit 0
echo "Your parameter is $@"
echo "The first parameter is $1"
echo "The second parameter is $2"

执行结果

sh para.sh var1 var2
below is the preset var info
The scripts name is :para.sh
Total parameter number is 2
Your parameter is var1 var2
The first parameter is var1
The second parameter is var2

变量说明:

  • $@ - 所有输入参数
  • $# - 输入参数个数
  • $0 - 脚本名称
  • $1 - 第一个输入参数
  • $2 - 第二个输入参数

2) 扩展其他变量

  • $$ - 此脚本的pid
  • $PPID - 此脚本父进程的pid
  • $UID - 执行此脚本的uid

2. read命令

1) "-p"参数

#-p参数后面接一个字符串,并可以接n个变量,用这些变量来接收用户输入的字符串
read -p "put your name here please:" name sex
put your name here please:someone female
echo $name
someone
echo $sex
female

2) 尽量避免接受多个变量

## [cibr.sh]脚本内容
=======================================================
#!/bin/bash
# Programe
# cibr--collect information by read command
# Created by Zhao Peiwu 16/12/2014

Tips="Please put your information here :   "
#这里放多个变量个人认为并不是个好事情,下面你会看到
read -p "$Tips" name sex age
echo $name
echo $sex
echo $age
=======================================================

bash cibr.sh
Please put your information here :   some one male 26
some
one
male 26  
# some one 本来应该是同一个变量,这里却分成了两个
# read是通过空格来识别每个变量之间的分隔的

## 若我们这样修改脚本
===========================================================
#!/bin/bash
# Programe
# cibr--collect information by read command
# Created by Zhao Peiwu 16/12/2014

Tips="Please put here your"
read -p "$Tips name: " name
read -p "$Tips sex: " sex
read -p "$Tips age: " age
echo "Name: $name"
echo "Sex: $sex"
echo "Age: $age"
===========================================================
#执行过程为
bash cibr.sh
Please put here your name: some one
Please put here your sex: male
Please put here your age: 26
Name: some one
Sex: male
Age: 26

3) -t参数,timeout

# read -t 3  -p "Please quickly,I just can wait you for 3 seconds." a
Please quickly,I just can wait you for 3 seconds.[root@web01 sbin]#
##系统等待了3秒钟,由于我没有进行输入动作,程序自动终止

3. 脚本中的数学运算

1) 计算器工具bc

# 安装计算器工具bc
yum install bc -y


## 创建cacu.sh并输入以下内容
==========================================================
#!/bin/bash
# Program
# This program is a demo scripts for testing caculator in shell
# Created by somebody 16/12/2014

read -p "please input the first number:" a
read -p "please input the second number:" b
echo $(($a+$b))
echo $[$a-$b]
c=`echo "$a*$b" | bc`
echo $c
==========================================================

# 脚本执行结果

bash cacu.sh
please input the first number:12
please input the second number:23
35
-11
276

2) 双引、单引、双括号、方括号在变量相加中的区别演示

# 双引号可识别变量,但计算时是当作字符串来拼接
a=1;b=2;c="$a+$b";echo $c
1+2

# 单引号无法识别变量
a=1;b=2;c='$a+$b';echo $c
$a+$b

# 以下两种方式均能正常获取值并计算成功
a=1;b=2;c=$(($a+$b));echo $c
3
a=1;b=2;c=$[$a+$b];echo $c
3

3) 浮点运算

# 以上计算方式全部不支持浮点运算,可用bc的scale参数
bc
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'.

# 直接进入bc工具设定scale为2
scale=2    
5/2
2.50

# 用管道传递执行命令,和上面进入bc工具异曲同工
echo "scale=2;5/2"|bc          
2.50

Contents