shell基础(变量和变量的数值计算)

一.定义变量

[[email protected] ~]# a=hello
[[email protected] ~]# echo $a
hello
[[email protected] ~]# b='hello'
[[email protected] ~]# echo $b
hello
[[email protected] ~]# c="hello"
[[email protected] ~]# echo $c
hello
[[email protected] ~]# a=westos-$a
[[email protected] ~]# echo $a
westos-hello
[[email protected] ~]# b='westos-$a'
[[email protected] ~]# echo $b
westos-$a
[[email protected] ~]# c="westos-$a"
[[email protected] ~]# echo $c
westos-westos-hello
[[email protected] ~]# a=westos hello
bash: hello: command not found...
[[email protected] ~]# a="westos hello"
[[email protected] ~]# echo $a
westos hello

注意:建议没有特别要求时,字符串都加双引号,需要原样输出就加单引号,双引号还可以定义包含的空格字符
二.特殊变量

$0 获取脚本文件名,如果执行时包含路径,则输出脚本路径
$n(>0) 获取脚本的第n个参数
$# 获取脚本后参数的总数
$* 获取所有参数
[email protected] 获取所有参数
$? 获取上一条命令结果的返回值,执行成功输出为0,执行失败输出非0
$$ 获取shell的进程值
$0:
[[email protected] mnt]# cat westos.sh 
#!/bin/bash
echo $0
[[email protected] mnt]# sh westos.sh 
westos.sh
[[email protected] mnt]# /mnt/westos.sh 
/mnt/westos.sh


$n:
[[email protected] mnt]# cat westos.sh 
#!/bin/bash
echo $1 $2

[[email protected] mnt]# sh westos.sh hello westos
hello westos
[[email protected] mnt]# sh westos.sh hello redhat
hello redhat

[[email protected] mnt]# echo \${1..10} > westos.sh 
[[email protected] mnt]# cat westos.sh 
$1 $2 $3 $4 $5 $6 $7 $8 $9 $10
[[email protected] mnt]# sh westos.sh  {1..10}
1 2 3 4 5 6 7 8 9 10
[[email protected] mnt]# sh westos.sh  {a..z}
a b c d e f g h i a0
[[email protected] mnt]# sh westos.sh  {a..z}
a b c d e f g h i j


$#:
[[email protected] mnt]# cat westos.sh ]
echo $1 $2 $3 $4 $5 $6 $7 $8 $9
echo $#
[[email protected] mnt]# sh westos.sh {1..100}
1 2 3 4 5 6 7 8 9
100

$?:
表示上条命令执行结果的返回值
0表示执行成功
非0表示执行失败

三.
read用法:
[[email protected] mnt]# read str westos hello
[[email protected] mnt]# echo $str
westos hello
[[email protected] mnt]# read -p “请输入一个整数:” i
请输入一个整数:10
shell基础(变量和变量的数值计算)
shell基础(变量和变量的数值计算)

将命令的结果赋值给变量:
[[email protected] mnt]# CMD=ls -l
[[email protected] mnt]# echo $CMD
total 8 -rwxr-xr-x. 1 root root 492 Dec 22 10:25 test.sh -rwxr-xr-x. 1 root root 40 Dec 22 10:40 westos.sh

[[email protected] mnt]# CMD=$(ls -l)
[[email protected] mnt]# echo $CMD
total 8 -rwxr-xr-x. 1 root root 492 Dec 22 10:25 test.sh -rwxr-xr-x. 1 root root 40 Dec 22 10:40 westos.sh
shell基础(变量和变量的数值计算)
五.练习:打包日至格式为log_日期.tar.gz

打包日志:
[[email protected] mnt]# tar zcf log_$(date +%F).tar.gz /var/log/
tar: Removing leading `/’ from member names
[[email protected] mnt]# ls
log_2018-12-22.tar.gz test.sh westos.sh
shell基础(变量和变量的数值计算)
六.变量数值的计算
1.expr命令:此命令在进行乘法计算时需要进行转义,执行后不保留新的值
shell基础(变量和变量的数值计算)

2)[][]和(())表达式:正常运算,不需要转义,执行后不保留新的数值
shell基础(变量和变量的数值计算)

3)let命令(let命令在执行后会保存新的值)
shell基础(变量和变量的数值计算)
4)小数运算工具bc
scale=数字,保留多少位小数3)let命令(let命令在执行后会保存新的值)
shell基础(变量和变量的数值计算)
七.练习
计算两个数值的加减乘除
shell基础(变量和变量的数值计算)
或者,改进版

#!/bin/bash

read -t 5 -p "请输入两个整数:" a b

echo "a+b=$[a+b]"
echo "a-b=$[a-b]"
echo "a*b=$[a*b]"
echo "a/b=$[a/b]"
echo "a**b=$[a**b]"
echo "a%b=$[a%b]"

-t 5 若没有输入值,默认等待五秒后退出