Shell变量

Shell变量定义
变量的概述
什么是变量? 数据的一种传递方式。用一个固定的字符串去表示一个不固定的字符串,数值,内容,数据等,方便后续调用 变量名的规范 1. 建议采用驼峰式方式,如:Hostname_Ip 2. 变量名由字母,数字,下划线组成,不能使用空格,短横岗-,特殊符号尽量不用 3. 变量名要便于理解,赋值时等号两边不能有空格 Shell变量定义的方式 1. 用户自定义变量 2. 系统环境变量:系统定义好的,系统操作环境相关信息 3. 位置参数变量:变量名是固定的,意思也是固定的,向脚本中进行参数传递 4. 预定义的变量:脚本中已经定义好的,变量名是固定的,作用也是固定的:$- $? $$ $@ $*
变量的实践
1、用户自定义变量
[root@shell01 /scripts]# Name=qiudao #定义变量 [root@shell01 /scripts]# echo $Name #引用变量 qiudao [root@shell01 /scripts]# Name=qiudao shell #变量值中不能出现空格 -bash: shell: command not found [root@shell01 /scripts]# Name='qiudao shell' #需要引号引起来 [root@shell01 /scripts]# echo $Name qiudao shell [root@shell01 /scripts]# Name==qiudao [root@shell01 /scripts]# echo $Name qiudao [root@shell01 /scripts]# echo $Nameshell #调用变量时,变量名不能跟其他字符串连在一起 [root@shell01 /scripts]# echo ${Name}shell #将变量名使用大括号进行括起来 qiudaoshell [root@shell01 /scripts]# echo $Name_shell [root@shell01 /scripts]# echo $Name-shell qiudao-shell [root@shell01 /scripts]# echo $Name/shell qiudao/shell [root@shell01 /scripts]# echo $Name qiudao [root@shell01 /scripts]# echo '$Name' $Name [root@shell01 /scripts]# echo "$Name" qiudao 单引号:强引用,里面是什么,就输出什么,吃什么吐什么 双引号:弱引用,吃什么吐什么,会解析变量 $ $() 不加引号:在变量定义值时,出现空格,就不会看做是一个整体的值。吃什么吐什么,解析变量 反引号 `` 优先执行反引号里面的命令,相当于$() [root@shell01 /scripts]# echo $Name money is $10000000000000 qiudao money is 0000000000000 [root@shell01 /scripts]# echo '$Name money is $10000000000000' $Name money is $10000000000000 [root@shell01 /scripts]# echo "$Name money is $10000000000000" qiudao money is 0000000000000 [root@shell01 /scripts]# echo "$Name money is '$10000000000000'" qiudao money is '0000000000000' [root@shell01 /scripts]# echo "$Name money is \$10000000000000" qiudao money is $10000000000000 [root@shell01 /scripts]# echo '"$Name" money is $10000000000000' "$Name" money is $10000000000000 注意:遇到特殊符号时,使用撬棍进行转义,取消其特殊含义。 2、系统环境变量 [root@shell01 /scripts]# echo $Name qiudao [root@shell01 /scripts]# vim test.sh [root@shell01 /scripts]# cat test.sh echo $Name [root@shell01 /scripts]# sh test.sh #用户自定义变量,只在当前环境下生效 [root@shell01 /scripts]# export Name=qiudao #自定义系统环境变量,只是临时生效,当前环境及其子Shell环境下生效,永久生效:将定义的变量写入到环境变量配置文件中, /etc/profile /etc/profile.d/*.sh /etc/bashrc ~/.bashrc ~/.bash_profile #注:export:当前用户的所有环境生效 export写入/etc/bashrc对所有用户永久生效 export写入./bashrc仅对该用户生效 [root@shell01 /scripts]# echo $Name qiudao [root@shell01 /scripts]# sh test.sh qiudao [root@shell01 /scripts]# bash [root@shell01 scripts]# echo $Name qiudao [root@shell01 scripts]# exit [root@shell01 /scripts]# set |grep Name #显示系统中所有定义的变量 Name=qiudao gitcvs.dbTableNamePrefix [root@shell01 /scripts]# env #只显示系统环境变量,系统环境变量全部是大写的字母 XDG_SESSION_ID=47 HOSTNAME=shell01 TERM=xterm [root@shell01 /scripts]# echo $Name qiudao [root@shell01 /scripts]# unset Name #取消变量 [root@shell01 /scripts]# echo $Name #系统环境变量练习 [root@shell01 /scripts]# cat var.sh #!/bin/bash echo "当前系统登录的用户为:$USER" echo "当前所在的目录位置为:$PWD" echo "当前系统的主机名为:$HOSTNAME" echo "当前系统用户的家目录为:$HOME" echo "当前系统登录的用户的UID为:$UID" echo "当前系统登录的终端IP为:$SSH_CONNECTION" [root@shell01 /scripts]# sh var.sh 当前系统登录的用户为:root 当前所在的目录位置为:/scripts 当前系统的主机名为:shell01 当前系统用户的家目录为:/root 当前系统登录的用户的UID为:0 当前系统登录的终端IP为:10.0.0.1 63017 10.0.0.7 22 3、预定义变量及位置变量 [root@shell01 /scripts]# cat path.sh #!/bin/bash echo "当前shell的脚本名为:$0" echo "当前shell的第一个位置变量为:$1" echo "当前shell的第二个位置变量为:$2" echo "当前shell的第三个位置变量为:$3" echo "当前shell的所有位置变量为:$*" echo "当前shell的所有位置变量为:$@" echo "当前shell的位置变量的个数为:$#" echo "当前shell脚本的PID号为:$$" echo "上一条命令的执行结果为:$?" [root@shell01 /scripts]# sh /scripts/path.sh nginx php mysql 当前shell的脚本名为:/scripts/path.sh 当前shell的第一个位置变量为:nginx 当前shell的第二个位置变量为:php 当前shell的第三个位置变量为:mysql 当前shell的所有位置变量为:nginx php mysql 当前shell的所有位置变量为:nginx php mysql 当前shell的位置变量的个数为:3 当前shell脚本的PID号为:8994 上一条命令的执行结果为:0 $*与$@的区别(了解即可) $* 把参数作为一个整体字符串返回 $@ 把参数一个一个进行返回 [root@shell01 /scripts]# cat test.sh #!/bin/bash test() { echo "未加引号时对比" echo $* echo $@ echo "加入引号之后对比" for i in "$*" do echo $i done echo "###################" for j in "$@" do echo $j done } test nginx php mysql ansible [root@shell01 /scripts]# sh test.sh 未加引号时对比 nginx php mysql ansible nginx php mysql ansible 加入引号之后对比 nginx php mysql ansible ################### nginx php mysql ansible 位置变量: $1 $2 .... $9 ${10} 将命令的执行结果赋值给变量 [root@shell01 /scripts]# Hostname=$(hostname) [root@shell01 /scripts]# echo $Hostname shell01 [root@shell01 /scripts]# age=18 [root@shell01 /scripts]# echo $age 18 [root@shell01 /scripts]# echo $(($age+1)) 19 [root@shell01 /scripts]# # $(( )) 命令行上面的计算 [root@shell01 /scripts]# [root@shell01 /scripts]# [root@shell01 /scripts]# Date=$(date +%F) [root@shell01 /scripts]# echo $Date 2020-02-19 [root@shell01 /scripts]# mkdir oldboy [root@shell01 /scripts]# touch oldboy/test{01..10}.txt [root@shell01 /scripts]# ls oldboy/ test01.txt test02.txt test03.txt test04.txt test05.txt test06.txt test07.txt test08.txt test09.txt test10.txt [root@shell01 /scripts]# tar czf test.tar.gz $(find oldboy/ -type f -name "*.txt") [root@shell01 /scripts]# ll total 16 drwxr-xr-x 2 root root 186 2020-02-19 15:56 oldboy -rw-r--r-- 1 root root 440 2020-02-19 15:12 path.sh -rw-r--r-- 1 root root 285 2020-02-19 15:25 test.sh -rw-r--r-- 1 root root 196 2020-02-19 15:57 test.tar.gz -rw-r--r-- 1 root root 308 2020-02-19 14:52 var.sh [root@shell01 /scripts]# Data=$(tar czf test.tar.gz $(find oldboy/ -type f -name "*.txt")) #没有输出结果 [root@shell01 /scripts]# echo $Data [root@shell01 /scripts]# tar cvzf test.tar.gz $(find oldboy/ -type f -name "*.txt") oldboy/test01.txt oldboy/test02.txt oldboy/test03.txt oldboy/test04.txt oldboy/test05.txt oldboy/test06.txt oldboy/test07.txt oldboy/test08.txt oldboy/test09.txt oldboy/test10.txt [root@shell01 /scripts]# Data=$(tar cvzf test.tar.gz $(find oldboy/ -type f -name "*.txt")) #加“v”显示输出结果 [root@shell01 /scripts]# echo $Data oldboy/test01.txt oldboy/test02.txt oldboy/test03.txt oldboy/test04.txt oldboy/test05.txt oldboy/test06.txt oldboy/test07.txt oldboy/test08.txt oldboy/test09.txt oldboy/test10.txt Shell变量赋值 1、read进行交互赋值 1. read的脚本中示例语法 [root@shell scripts]# cat read01.sh #!/bin/bash read -p "please enter a number:" Var echo "the number you entered is $Var" 2. read简单备份场景 #提示用户输入要备份的目录 #告知用户要备份的目录是什么 #备份目录 #告知用户目录备份完成 [root@shell scripts]# cat read02.sh #!/bin/bash read -p "Please enter the directory you want to backup:" Dir echo "The directory you want to backup is: $Dir" echo "Backing up ${Dir}......" echo "$Dir backup completed" 3. 测试用户输入的IP地址是否能通 #提示用户输入IP地址 #根据用户输入的IP地址进行测试 #根据测试的结果,返回给用户,通的就返回通,不通就返回不通 ------第一种方法------ [root@shell scripts]# cat read03.sh #!/bin/bash #提示用户输入IP地址 read -p "Please input IP address: " Ip #测试IP地址是否能通 ping -c1 -W1 $Ip &>/dev/null ## -c:代表ping指定次数后停止ping ## -W:以毫秒为单位设置ping的超时时间 #将结果返回给用户 if [ $? -eq 0 ];then echo "$Ip can connent to the Internet!" else echo "NO!!!FAILURE!" fi ------第二种方法------ [root@shell scripts]# cat read03.sh #!/bin/bash #提示用户输入IP地址 read -p "Please input IP address: " Ip #测试IP地址是否能通,将结果返回给用户 ping -c1 -W1 $Ip &>/dev/null && echo "$Ip can connent to the Internet!" || echo "NO!!!FAILURE!" 4. 添加输出颜色 [root@shell scripts]# cat read04.sh #!/bin/bash #提示用户输入IP地址 read -p "Please input IP address: " Ip #测试IP地址是否能通 ping -c1 -W1 $Ip &>/dev/null #将结果返回给用户 if [ $? -eq 0 ];then echo -e "\033[32m$Ip can connent to the Internet!\033[0m" else echo -e "\033[31mNO!!!FAILURE!\033[0m" fi echo命令的特性:输出颜色 echo -e "\033[30m 黑色字 \033[0m" echo -e "\033[31m 红色字 \033[0m" echo -e "\033[32m 绿色字 \033[0m" echo -e "\033[33m 黄色字 \033[0m" echo -e "\033[34m 蓝色字 \033[0m" echo -e "\033[35m 紫色字 \033[0m" echo -e "\033[36m 天蓝字 \033[0m" echo -e "\033[37m 白色字 \033[0m" echo -e "\033[40;37m 黑底白字 \033[0m" echo -e "\033[41;37m 红底白字 \033[0m" echo -e "\033[42;37m 绿底白字 \033[0m" echo -e "\033[43;37m 黄底白字 \033[0m" echo -e "\033[44;37m 蓝底白字 \033[0m" echo -e "\033[45;37m 紫底白字 \033[0m" echo -e "\033[46;37m 天蓝底白字 \033[0m" echo -e "\033[47;30m 白底黑字 \033[0m" 5. 写一个脚本,修改主机名 #提示用户输入新的主机名 #提示用户是否确认修改主机名 #修改主机名 #根据修改的结果进行判断 #修改成功之后,让其立即生效 [root@shell /scripts]# cat read05.sh #!/bin/bash #提示用户输入新的主机名 read -p "Please enter new hostname:" Host #提示用户是否确认修改主机名 read -p "Confirm whether to change the host name: [y/n]" Judge #修改主机名 if [ "$Judge" == "y" ];then hostnamectl set-hostname $Host &>/dev/null if [ $? -eq 0 ];then echo "$Host is changed" bash else echo "Please check your script" fi else echo "See you next time!" fi 6. 写个脚本,修改主机IP地址 #提示用户输入新的IP地址 #提示用户是否真的修改IP地址 #修改eth0和eth1网卡 #修改IP地址,提示用户IP地址修改成功 #提示用户是否重启网络服务,重启后远程连接会断开 [root@shell /scripts]# cat read06.sh #!/bin/bash #提示用户输入新的IP地址 read -p "Please enter IP host: " Host #提示用户是否真的修改IP地址 read -p "Whether to change IP? [y/n]:" Judge if [ "$Judge" == "y" ];then sed -ri "/^IPAD/s#(.*\.)(.*)#\1${Host}#g" /etc/sysconfig/network-scripts/ifcfg-eth[01] &>/dev/null if [ $? -eq 0 ];then echo "IP has changed." read -p "Whether to restart network right now?[y/n]:" Res_Net if [ "$Res_Net" == "y" ];then systemctl restart network if [ $? -ne 0 ];then echo "Fail to restart" fi else echo "You can restart manually later" fi else echo "Please check your config" fi else echo "Bye!" fi Shell变量替换 变量 说明 ${#变量} 获取变量的长度 ${变量#匹配规则} 从头开始匹配,最短删除 ${变量##匹配规则} 从头开始匹配,最长删除 ${变量%匹配规则} 从尾开始匹配,最短删除 ${变量%%匹配规则} 从尾开始匹配,最长删除 ${变量/旧的字符串/新的字符串} 替换变量中的旧的字符串为新的字符串,只替换第一个 ${变量//旧的字符串/新的字符串} 替换变量中的旧的字符串为新的字符串,替换所有 ${变量:匹配规则:匹配规则} 索引及切片 [root@shell /scripts]# url=www.sina.com.cn #定义变量 [root@shell /scripts]# echo $url #打印变量 www.sina.com.cn [root@shell /scripts]# echo ${#url} #获取变量值的长度 15 [root@shell /scripts]# echo ${url#*.} #从头开始匹配,最短删除,*表示所有,点没有任何含义 sina.com.cn [root@shell /scripts]# echo ${url##*.} #从头开始匹配,最长删除 cn [root@shell /scripts]# echo ${url%.*} #从尾开始匹配,最短删除 www.sina.com [root@shell /scripts]# echo ${url%%.*} #从尾开始匹配,最长删除 www [root@shell /scripts]# echo ${url/w/W} #将旧的字符串替换为新的字符串,只替换第一个字符串 Www.sina.com.cn [root@shell /scripts]# echo ${url//w/W} #将旧的字符串替换为新的字符串,替换所有 WWW.sina.com.cn [root@shell /scripts]# echo ${url/w/} #将第一个匹配到w删除 ww.sina.com.cn [root@shell /scripts]# echo ${url//w/} #将所有匹配到w删除 .sina.com.cn [root@shell /scripts]# echo $url #打印变量 w w w . s i n a . com.cn 0 1 2 3 4 5 6 7 8 9 [root@shell /scripts]# echo ${url:0:3} #从0列开始匹配,切出3列 0-2列 www [root@shell /scripts]# echo ${url:5:4} #从5列开始匹配,切出4列, 5-8列 ina. [root@shell /scripts]# echo ${url:5} #切除前5列 0-4列删除 ina.com.cn 变量替换案例 1、案例一:查看当前内存的使用率,如果使用率大于80%,则进行报警 #获取内存使用率 #使用率用整数比较 #判断返回结果,大于80则报警,正常就正常提示 [root@shell /scripts]# cat free_use.sh #!/bin/bash #当前内存使用率 Mem_Use=$(free -m|awk '/^Mem/{print $3/$2*100}') #使用率是否超过80% if [ ${Mem_Use%.*} -gt 80 ];then echo -e "\033[31mExcessive memory usage! Current:${Mem_Use}%\033[0m" else echo -e "\033[32mNormal memory usage...Current:${Mem_Use}%\033[0m" fi #测试方法 [root@shell ~]# dd if=/dev/zero of=./1.txt bs=1000M count=1000 2、案例二:每天进行备份/etc/目录,将其备份到/backup目录,需要打包备份,压缩包名为:2020-02-20_Ip_etc.tar.gz #备份内容:/etc/目录 #备份存储位置:/backup #备份方式:打包压缩备份,格式2020-02-20_10.0.0.100_etc.tar.gz #备份的周期:每天 [root@shell /scripts]# cat backup.sh #!/bin/bash #定义变量 Dir=/backup Date=$(date +%F) Ip=$(ifconfig eth0|awk 'NR==2{print $2}') #判断目录是否存在,没有就创建 [ -d $Dir ] || mkdir -p $Dir #开始备份目录 cd / && tar czf ${Dir}/${Date}_${Ip}_etc.tar.gz etc &>/dev/null #判断备份是否成功 if [ $? -eq 0 ];then echo "Success!" else echo "Failure..." fi 3、案例三:在/oldboy目录下,存在很多文件,找出所有以.txt为结尾的文件 #批量重命名,将所有以.txt为结尾的文件重命名为.txt.bak #把所有的.bak的文件打包压缩 #批量还原.bak的文件重命名为.txt [root@shell /scripts]# touch /goudan/test{01..10}.txt [root@shell /scripts]# touch /goudan/test{01..10}.log #第一种方法 [root@shell /scripts]# cat mv.sh #!/bin/bash #把所有以.txt为结尾的文件重命名为.txt.bak ###方法一:sed### find /goudan/ -type f -name "*.txt" | sed -r 's#(.*)#mv \1 \1.bak#g' | bash &>/dev/null ###方法二:awk## find /goudan -type f -name "*.txt"|awk '{print"mv "$0" "$0".bak"}' #判断重命名是否成功 if [ $? -eq 0 ];then echo "所有.txt结尾文件已更改为.txt.bak" else echo "重命名失败!" exit fi #将所有.bak的文件进行打包压缩 cd /goudan && tar czf bak.tar.gz $(find /goudan -type f -name "*.bak") &>/dev/null #判断是否打包压缩成功 if [ $? -eq 0 ];then echo "所有.bak文件已打包........" else echo "打包压缩失败!" exit fi #还原名称 ###方法一:sed### find /goudan -type f -name "*.bak" | sed -r 's#(.*)(\.bak)#mv \1\2 \1#g' | bash &>/dev/null ###方法二:awk### find /goudan -type f -name "*.bak"|awk -F '.' '{print "mv "$0" "$1"."$2}'|bash &>/dev/null #判断还原名称是否成功 if [ $? -eq 0 ];then echo "已将.bak文件还原为.txt!" else echo "还原名称失败!" exit fi #方法三:for循环 [root@shell /scripts]# cat mv.sh #!/bin/bash # 批量修改.txt为.txt.bak for i in $(find /backup -name "*.txt") do mv $i ${i}.bak done # 判断是否执行成功 if [ $? -eq 0 ];then echo "将文件修改为.txt.bak成功" else echo "将文件修改为.txt.bak失败" exit fi # 打包以.bak结尾的文件 cd /backup && tar czf bak.tar.gz $(find /backup -name "*.bak") &>/dev/null # 判断打包是否成功 if [ $? -eq 0 ];then echo "打包成功" else echo "打包失败" exit fi # 将.bak结尾的文件还原为.txt for j in $(find /backup -name "*.bak") do mv $j ${j%.*} done # 判断还原是否成功 if [ $? -eq 0 ];then echo "成功将文件还原为.txt" else echo "还原失败" fi 4. 案例四:把以下的字符串中长度小于5的打印出来 When find examines or prints information a file # 方法一: [root@shell /backup]# echo "When find examines or prints information a file"|xargs -n1|awk "{if(length<5)print}" # 方法二: [root@shell /scripts]# cat length.sh #!/bin/bash string='When find examines or prints information a file' # 显示要打印的字符串 echo "$string" # 打印长度小于5的字符串 for i in ${string} do [ ${#i} -lt 5 ] && echo $i done Shell变量运算 加:num1 + num2 减:num1 - num2 乘:num1 * num2 除:num1 / num2 求余:num1 % num2 整数运算 只支持整数运算,不支持小数运算 expr:数值之间必须要有空格进行分开,当使用*乘的时候,需要对其进行转义使用,不能进行求方运算 $(()):没有严格格式要求,不能进行求方运算 $[]:相当于$(()),没有严格格式要求,不能进行求方运算 let:计数 1. expr [root@shell /scripts]# expr 1 + 1 2 [root@shell /scripts]# num1=10 [root@shell /scripts]# num2=5 [root@shell /scripts]# expr $num1 + $num2 15 [root@shell /scripts]# expr $num1 - $num2 5 [root@shell /scripts]# expr $num1 * $num2 expr: syntax error [root@shell /scripts]# expr $num1 \* $num2 50 [root@shell /scripts]# expr $num1 / $num2 2 [root@shell /scripts]# expr $num1 % $num2 0 2. $(( )) [root@shell /scripts]# echo $(($num1+$num2)) 16 [root@shell /scripts]# echo $(( $num1 + $num2 )) 16 [root@shell /scripts]# echo $(( $num1 - $num2 )) 6 [root@shell /scripts]# echo $(( $num1 * $num2 )) 55 [root@shell /scripts]# echo $(( $num1 / $num2 )) 2 [root@shell /scripts]# echo $(( $num1 % $num2 )) 1 3. $[ ] [root@shell /scripts]# echo $[$num1+$num2] 16 [root@shell /scripts]# echo $[1+1] 2 [root@shell /scripts]# echo $[ $num1 - $num2 ] 6 [root@shell /scripts]# echo $[ $num1 * $num2 ] 55 [root@shell /scripts]# echo $[ $num1 / $num2 ] 2 [root@shell /scripts]# echo $[ $num1 % $num2 ] 1 4. let [root@shell /scripts]# a=10 [root@shell /scripts]# let a++ [root@shell /scripts]# let a++ [root@shell /scripts]# let a++ [root@shell /scripts]# echo $a 13 [root@shell /scripts]# let a-- [root@shell /scripts]# echo $a 12 [root@shell /scripts]# echo $a 12 [root@shell /scripts]# let a-- [root@shell /scripts]# echo $a 11 小数运算 小数计算命令: bc、awk、python 1. bc [root@shell /scripts]# yum install -y bc [root@shell /scripts]# 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 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 2. 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 #BEGIN 行处理前 [root@shell /scripts]# awk "BEGIN{print $num1/$num2}" 2.2 [root@shell /scripts]# awk -vnum1=10 -vnum2=5 'BEGIN{print num1/num2}' 2 #-v #自定义内部变量 3. 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 变量运算案例 案例一:ps aux命令下,求VSZ列的和 #方法一:bc [root@shell /scripts]# ps aux | awk 'NR>1{print $5}' | xargs | tr ' ' '+' | bc #方法二:awk [root@shell /scripts]# ps aux | awk 'NR>1{i+=$5}END{print i}' #方法三:for循环 #!/bin/bash Vsz_Sum=0 #计算VSZ列的值 for i in $(ps -aux|awk 'NR>1{print $5}') do Vsz_Sum=$(($Vsz_Sum + $i)) done #判断执行是否成功 if [ $? -eq 0 ];then echo "$Vsz_Sum" else echo "请检查语法" fi 案例二:写个脚本,实现一个简单的计算器,实现加减乘除(执行脚本时,提示输入第一个数字:10,提示输入第二个数字:20) 10 + 20 = 30 10 - 20 = -10 [root@shell ~]# cat cal.sh #!/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变量案例 案例一:使用Shell脚本打印,系统版本、内核版本平台、虚拟平台、静态主机名、eth0网卡IP地址、lo网卡IP地址、当前主机的外网IP地址curl icanhazip.com [root@shell /scripts]# cat var.sh #!/bin/bash Version=$(awk '{print $1,$4}' /etc/redhat-release) Kernel=$(hostnamectl | awk '/Kernel/{print $3}') Vm=$(hostnamectl | awk '/Virtua/{print $2}') Static_Hostname=$(hostnamectl | awk '/hostname/{print $3}') Eth0=$(ifconfig eth0 | awk 'NR==2{print $2}') Lo=$(ifconfig lo | awk 'NR==2{print $2}') Wan=$(curl -s ifconfig.me) echo "当前系统版本号为:$Version" echo "当前系统内核版本号为:${Kernel%.e*}" echo "当前系统虚拟化平台为:$Vm" echo "当前系统静态主机名为:$Static_Hostname" echo "当前系统的Eth0网卡IP地址为:$Eth0" echo "当前系统的Lo网卡IP地址为:$Lo" echo "当前系统的外网IP地址为:$Wan" 案例二:需求描述:变量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,可以执行对应项的功能。 [root@shell /scripts]# cat var-1.sh #!/bin/bash #1.定义变量 String='Bigdata process is Hadoop, Hadoop is open source project' #2.打印变量 echo $String #3.输出菜单 cat<<EOF 1)打印string长度 2)删除字符串中所有的Hadoop 3)替换第一个Hadoop为Linux 4)替换全部Hadoop为Linux EOF #4.提示用户输入对应的数字,执行对应的功能 read -p "请输入上方菜单对应的数字,执行其对应的功能[1|2|3|4]:" Num #5.根据用户输入的数字进行执行对应的功能 if [ $Num -eq 1 ];then echo "正在打印变量String的长度....." echo ${#String} elif [ $Num -eq 2 ];then echo "正在删除字符串中的所有Hadoop" echo ${String//Hadoop/} elif [ $Num -eq 3 ];then echo "正在替换变量中第一个Hadoop为Linux" echo ${String/Hadoop/Linux} elif [ $Num -eq 4 ];then echo "正在替换变量中的所有Hadoop为Linux" echo ${String//Hadoop/Linux} else echo "你输入的数字不正确!请按照要求输入对应的数字!" fi


还没有评论,来说两句吧...