Shell中的变量、变量的数值计算

变量的定义:

变量即在程序运行过程中它的值是允许改变的量,变量是用一串固定的字符来标示不固定的值的一种方法,
变量是一种使用方便的占位符,用于引用计算机内存地址,该地址可以存储 Script 运行时可更改的程序信息,
在 shell 中变量是不能永久保存在系统中的,必须在文件中声明

1.shell变量

在shell中变量分为环境级变量,用户级变量,系统级变量。

环境级变量:只在当前 shell 中生效, 当shell关闭时变量便会丢失;
用户级变量:写在用户的骨文件中,只针对当前用户生效;
系统级变量:写在系统的配置文件 /etc/profile 或者 /etc/profile.d/ 中,对于所有用户都生效

(1).设定环境级变量

环境级变量:只在当前 shell 中生效, 当shell关闭时变量便会丢失;
[root@localhost ~]# export a=1
[root@localhost ~]# echo $a
1
[root@localhost ~]# logout
Connection to 172.25.254.166 closed.
[root@foundation66 ~]# ssh root@172.25.254.166
root@172.25.254.166's password: 
Last login: Mon Dec 24 21:25:00 2018 from 172.25.254.66
[root@localhost ~]# echo $a

在这里插入图片描述
(2)设定用户级变量

用户级变量:写在用户的骨文件中,只针对当前用户生效;
[root@localhost ~]# vim .bash_profile 
###################
export a=1

在这里插入图片描述

[root@localhost ~]# source .bash_profile 
[root@localhost ~]# echo $a
1
[root@localhost ~]# exit
logout
Connection to 172.25.254.166 closed.
[root@foundation66 ~]# ssh root@172.25.254.166
root@172.25.254.166's password: 
Last login: Mon Dec 24 21:33:23 2018 from 172.25.254.66
[root@localhost ~]# echo $a
1

在这里插入图片描述

[root@localhost ~]# echo $a
1
[root@localhost ~]# su - student
Last login: Mon Dec 24 22:00:41 CST 2018 on pts/0
[student@localhost ~]$ echo $a

在这里插入图片描述

(3)设定系统级变量

系统级变量:写在系统的配置文件 /etc/profile 或者 /etc/profile.d/ 中,对于所有用户都生效
[root@localhost ~]# vim /etc/profile
###################
export a=1

在这里插入图片描述

[root@localhost ~]# source /etc/profile
[root@localhost ~]# echo $a
1
[root@localhost ~]# su - student
Last login: Tue Dec 25 22:40:36 CST 2018 on pts/0
[student@localhost ~]$ echo $a
1

在这里插入图片描述
2.环境变量

环境变量也可叫全局变量,可以在创建他们的shell及派生出的子shell中使用(无需定义,直接可以使用,如:$UID)
相关命令:
   set :输出所有变量
   env:只显示全局变量
   declare:输出所有变量,函数,整数等
##系统中无变量a,即输出空行
[root@localhost mnt]# echo $a

##查看当前系统所使用的shell
[root@localhost mnt]# echo $SHELL
/bin/bash
##查看当前系统用户的uid
[root@localhost mnt]# echo $UID
0
##查看当前系统用户家目录
[root@localhost mnt]# echo $HOME
/root
[root@localhost mnt]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
##查看系统中所有的shell
[root@localhost mnt]# cat /etc/shells 
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
## /bin/sh 实质上就是 bash(软链接)
[root@localhost mnt]# ll /bin/sh
lrwxrwxrwx. 1 root root 4 Nov 17 22:30 /bin/sh -> bash

在这里插入图片描述

3.普通变量

1)普通变量赋值
变量名=value
变量名='value'
变量名="value"

例如: a=westos

2)命令结果作为内容赋值
变量名=`命令`
变量名=$(命令)

例如: b=`ls`或 b=$(ls)

(1)普通变量赋值

[root@localhost mnt]# a=hello
[root@localhost mnt]# echo $a
hello
[root@localhost mnt]# a='hello'
[root@localhost mnt]# echo $a
hello
[root@localhost mnt]# a="hello"
[root@localhost mnt]# echo $a
hello

在这里插入图片描述

##单引号对变量不做转换(默认不解析内容)
[root@localhost mnt]# b=westos-$a
[root@localhost mnt]# echo $b
westos-hello
[root@localhost mnt]# b='westos-$a'
[root@localhost mnt]# echo $b
westos-$a
[root@localhost mnt]# b="westos-$a"
[root@localhost mnt]# echo $b
westos-hello

在这里插入图片描述

[root@localhost mnt]# a= westos hello
bash: westos: command not found...
[root@localhost mnt]# a='westos hello'
[root@localhost mnt]# echo $a
westos hello
[root@localhost mnt]# a="westos hello"
[root@localhost mnt]# echo $a
westos hello

在这里插入图片描述

注意:建议没有特别要求时,字符串都加双引号,需要原样输出就加单引号

(2)命令结果作为内容赋值

## -p表示同时建立多个目录(/westos目录及其子目录file)
[root@localhost mnt]# mkdir -p /westos/file
[root@localhost mnt]# ll -d /westos/
drwxr-xr-x. 3 root root 17 Dec 25 23:59 /westos/
## 1. ` ` 表示执行命令的结果
[root@localhost mnt]# a=`ll -d /westos/`
[root@localhost mnt]# echo $a
drwxr-xr-x. 3 root root 17 Dec 25 23:59 /westos/
##2. $() 也表示执行命令的结果
[root@localhost mnt]# b=$(ll -d /westos/)
[root@localhost mnt]# echo $b
drwxr-xr-x. 3 root root 17 Dec 25 23:59 /westos/

4.特殊变量

$0          ##获取脚本文件名,如果执行时包含路径,则输出脚本路径
$n(>0)      ##以空格符为分隔符,脚本后的第n串字符 (n>0)
$#          ##脚本后边跟的参数个数
$*          ##脚本后所跟的所有参数
$@          ##脚本后所跟的所有参数(区别在于:$*表示脚本后边跟的是一串字符,而$@表示脚本后边跟的是多串字符)
$?          ##获取上一条命令执行状态的返回值,非0为失败
$$          ##获取当前shell进程号

(1)$0

[root@localhost ~]# vim test.sh
###################
#!/bin/bash

echo $0    
##1.sh方调用脚本时,输出脚本名称
[root@localhost ~]# sh test.sh 
test.sh

在这里插入图片描述

##2.绝对路径方式调用脚本,此时输出脚本路径
[root@localhost mnt]# chmod +x test.sh 
[root@localhost mnt]# /mnt/test.sh 
/mnt/test.sh

(2)$n (n>0)

[root@localhost mnt]# vim test.sh 
####################
#!/bin/bash

echo $1 $2
##$1表示脚本后的第一串字符
[root@localhost mnt]# sh test.sh hello word
hello word
[root@localhost mnt]# vim test.sh 
[root@localhost mnt]# cat test.sh 
#!/bin/bash

echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10
[root@localhost mnt]# sh test.sh {1..10}
1 2 3 4 5 6 7 8 9 10
##不识别$10
[root@localhost mnt]# sh test.sh {a..z}
a b c d e f g h i a0
[root@localhost mnt]# vim test.sh 
##更改为${10}  系统才会识别
[root@localhost mnt]# cat test.sh 
#!/bin/bash

echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
[root@localhost mnt]# sh test.sh {a..z}
a b c d e f g h i j

(3)$#

[root@localhost mnt]# vim test.sh 
[root@localhost mnt]# cat test.sh 
#!/bin/bash

echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
echo $#
[root@localhost mnt]# sh test.sh {1..10}
1 2 3 4 5 6 7 8 9 10
10
##  $#表示脚本后跟的参数个数
[root@localhost mnt]# sh test.sh {1..100}
1 2 3 4 5 6 7 8 9 10
100

(4)$?

##  $?表示获取上一条命令执行状态的返回值,0为执行成功,非0表示执行失败
[root@localhost mnt]# ls /etc/passwd
/etc/passwd
[root@localhost mnt]# echo $?
0
[root@localhost mnt]# ls /etc/password
ls: cannot access /etc/password: No such file or directory
[root@localhost mnt]# echo $?
2

(5)$$

[root@localhost mnt]# echo $$
2915
[root@localhost mnt]# ps
  PID TTY          TIME CMD
 2915 pts/0    00:00:00 bash
 4368 pts/0    00:00:00 ps

在这里插入图片描述
(6) $*

##  $* 和 $@ 都表示脚本后跟的所有参数
[root@localhost mnt]# vim test.sh 
root@localhost mnt]# cat test.sh 
#!/bin/bash

echo $*
[root@localhost mnt]# sh test.sh I like you
I like you
[root@localhost mnt]# vim test.sh 
[root@localhost mnt]# cat test.sh 
#!/bin/bash

echo $@
[root@localhost mnt]# sh test.sh I want you
I want you

练习1:如何将日志打包成log_2018-12-26.tar.gz(2018-12-26.为当前日期)?

[root@localhost mnt]# date +%Y-%m-%d
2018-12-26
[root@localhost mnt]# date +%F
2018-12-26
[root@localhost mnt]# tar zcf log_`date +%Y-%m-%d`.tar.gz /var/log/messages
tar: Removing leading `/' from member names
[root@localhost mnt]# ls
log_2018-12-26.tar.gz  messages.sh

在这里插入图片描述

[root@localhost mnt]# rm -rf log_2018-12-26.tar.gz
[root@localhost mnt]# vim messages.sh 
#####################
#!/bin/bash

tar zcf log_`date +%Y-%m-%d`.tar.gz /var/log/messages
[root@localhost mnt]# sh messages.sh 
tar: Removing leading `/' from member names
[root@localhost mnt]# ls
log_2018-12-26.tar.gz  messages.sh

注意:一般编写shell脚本,需要先在shell中执行成功后,再写入shell脚本文件中

5.变量数值计算

(1)expr命令

[root@localhost mnt]# a=123
##运算符与数值之间必须要有空格,否则不识别
[root@localhost mnt]# expr $a+10
123+10
[root@localhost mnt]# expr $a + 10
133
[root@localhost mnt]# expr $a - 10
113
## 乘运算 必须要加 \ 转译
[root@localhost mnt]# expr $a * 10
expr: syntax error
[root@localhost mnt]# expr $a \* 10
1230
[root@localhost mnt]# expr $a / 10
12
[root@localhost mnt]# expr $a % 10
3

(2)$[ ] 表达式

$[ ] 表达式 和 $(( ))表达式 实质上是相同的
[root@localhost mnt]# a=10
[root@localhost mnt]# echo $[a+10]
20
[root@localhost mnt]# echo $[a-10]
0
[root@localhost mnt]# echo $[a*10]
100
[root@localhost mnt]# echo $[a/10]
1
[root@localhost mnt]# echo $[a%10]
0
[root@localhost mnt]# echo $((a+10))
20
[root@localhost mnt]# echo $((a-10))
0
[root@localhost mnt]# echo $((a*10))
100
[root@localhost mnt]# echo $((a/10))
1
[root@localhost mnt]# echo $((a%10))
0

( 3)let命令

let命令与前两种数值运算方法最大的不同在于:此命令在执行后会保存新的值
[root@localhost mnt]# a=10
[root@localhost mnt]# let a+=10
[root@localhost mnt]# echo $a
20
[root@localhost mnt]# let a-=10
[root@localhost mnt]# echo $a
10
[root@localhost mnt]# let a*=10
[root@localhost mnt]# echo $a
100
[root@localhost mnt]# let a/=10
[root@localhost mnt]# echo $a
10
[root@localhost mnt]# let a%=10
[root@localhost mnt]# echo $a
0

(4)bc 小数运算工具

[root@localhost mnt]# 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'. 
1+1
2
quit   ##退出
[root@localhost mnt]# bc <<EOF
> 1+1
> EOF
2
[root@localhost mnt]# echo 1.2+3.4 | bc
4.6
[root@localhost mnt]# echo 1.23+4.56 | bc
5.79
## scale表示保留的几位小数
[root@localhost mnt]# echo "scale=4;1.23*4.56" | bc
5.6088
[root@localhost mnt]# echo "scale=2;1.23*4.56" | bc
5.60

练习2:计算任意两个整数的加减乘除、求余、次方等运算

##read用于接收用户输入的信息
[root@localhost mnt]# read -p "请输入两个整数:" i j 
请输入两个整数:2 3
[root@localhost mnt]# echo $i
2
[root@localhost mnt]# echo $j
3

方法1:

[root@localhost mnt]# vim caculate.sh 
#####################
#!/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]

在这里插入图片描述

[root@localhost mnt]# sh caculate.sh 
请输入两个整数:2 3
2+3=5
2-3=-1
2*3=6
2/3=0
2%3=2
2**3=8

在这里插入图片描述
方法2:

[root@localhost mnt]# vim caculate.sh 
##################
#!/bin/bash

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

echo $a+$b=`expr $a + $b`
echo $a-$b=`expr $a - $b`
echo $a*$b=`expr $a \* $b`
echo $a/$b=`expr $a / $b`
echo $a%$b=`expr $a % $b`

在这里插入图片描述

[root@localhost mnt]# sh caculate.sh 
请输入两个整数:2 3
2+3=5
2-3=-1
2*3=6
2/3=0
2%3=2

在这里插入图片描述
注意: expr 命令无法计算次方运算

[root@localhost mnt]# expr $a ** $b
expr: syntax error
[root@localhost mnt]# expr $a \** $b
expr: syntax error
  • 12
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值