小编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 $Nameqiudao shell[root@shell01 /scripts]# Name==qiudao[root@shell01 /scripts]# echo $Nameqiudao[root@shell01 /scripts]# echo $Nameshell #调用变量时,变量名不能跟其他字符串连在一起[root@shell01 /scripts]# echo ${Name}shell #将变量名利用大括号进行括起来qiudaoshell[root@shell01 /scripts]# echo $Name_shell[root@shell01 /scripts]# echo $Name-shellqiudao-shell[root@shell01 /scripts]# echo $Name/shellqiudao/shell[root@shell01 /scripts]# echo $Nameqiudao[root@shell01 /scripts]# echo '$Name'$Name[root@shell01 /scripts]# echo "$Name"qiudao单引号:强引用,里面是什么,就输出什么,吃什么吐什么双引号:弱引用,吃什么吐什么,会解析变量 $ $()不加引号:在变量定义值时,涌现空格,就不会看做是一个整体的值。
吃什么吐什么,解析变量反引号 `` 优先实行反引号里面的命令,相称于$() [root@shell01 /scripts]# echo $Name money is $10000000000000qiudao 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 $Nameqiudao[root@shell01 /scripts]# vim test.sh [root@shell01 /scripts]# cat test.shecho $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 $Nameqiudao[root@shell01 /scripts]# sh test.sh qiudao[root@shell01 /scripts]# bash[root@shell01 scripts]# echo $Nameqiudao[root@shell01 scripts]# exit[root@shell01 /scripts]# set |grep Name #显示系统中所有定义的变量Name=qiudao gitcvs.dbTableNamePrefix[root@shell01 /scripts]# env #只显示系统环境变量,系统环境变量全部是大写的字母XDG_SESSION_ID=47HOSTNAME=shell01TERM=xterm[root@shell01 /scripts]# echo $Nameqiudao[root@shell01 /scripts]# unset Name #取消变量[root@shell01 /scripts]# echo $Name#系统环境变量练习[root@shell01 /scripts]# cat var.sh#!/bin/bashecho "当前系统登录的用户为:$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 223、预定义变量及位置变量[root@shell01 /scripts]# cat path.sh #!/bin/bashecho "当前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/bashtest() { 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 ansiblenginx php mysql ansible加入引号之后比拟nginx php mysql ansible###################nginxphpmysqlansible位置变量: $1 $2 .... $9 ${10}将命令的实行结果赋值给变量[root@shell01 /scripts]# Hostname=$(hostname)[root@shell01 /scripts]# echo $Hostnameshell01[root@shell01 /scripts]# age=18[root@shell01 /scripts]# echo $age18[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 $Date2020-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]# lltotal 16drwxr-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.txtoldboy/test02.txtoldboy/test03.txtoldboy/test04.txtoldboy/test05.txtoldboy/test06.txtoldboy/test07.txtoldboy/test08.txtoldboy/test09.txtoldboy/test10.txt[root@shell01 /scripts]# Data=$(tar cvzf test.tar.gz $(find oldboy/ -type f -name ".txt")) #加“v”显示输出结果[root@shell01 /scripts]# echo $Dataoldboy/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.txtShell变量赋值1、read进行交互赋值1. read的脚本中示例语法[root@shell scripts]# cat read01.sh #!/bin/bashread -p "please enter a number:" Varecho "the number you entered is $Var"2. read大略备份场景#提示用户输入要备份的目录#奉告用户要备份的目录是什么#备份目录 #奉告用户目录备份完成[root@shell scripts]# cat read02.sh #!/bin/bashread -p "Please enter the directory you want to backup:" Direcho "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"fiecho命令的特性:输精彩彩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" fielse echo "See you next time!"fi6. 写个脚本,修正主机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]:" Judgeif [ "$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" fielse echo "Bye!"fiShell变量更换变量解释${#变量}获取变量的长度${变量#匹配规则}从头开始匹配,最短删除${变量##匹配规则}从头开始匹配,最长删除${变量%匹配规则}从尾开始匹配,最短删除${变量%%匹配规则}从尾开始匹配,最长删除${变量/旧的字符串/新的字符串}更换变量中的旧的字符串为新的字符串,只更换第一个${变量//旧的字符串/新的字符串}更换变量中的旧的字符串为新的字符串,更换所有${变量:匹配规则:匹配规则}索引及切片[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.cn0 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/$2100}')#利用率是否超过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=10002、案例二:每天进行备份/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=/backupDate=$(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..."fi3、案例三:在/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 "重命名失落败!
" exitfi#将所有.bak的文件进行打包压缩cd /goudan && tar czf bak.tar.gz $(find /goudan -type f -name ".bak") &>/dev/null#判断是否打包压缩成功if [ $? -eq 0 ];then echo "所有.bak文件已打包........"else echo "打包压缩失落败!" exitfi#还原名称###方法一: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 "还原名称失落败!
" exitfi#方法三:for循环[root@shell /scripts]# cat mv.sh #!/bin/bash# 批量修正.txt为.txt.bakfor i in $(find /backup -name ".txt")do mv $i ${i}.bakdone# 判断是否实行成功if [ $? -eq 0 ];then echo "将文件修正为.txt.bak成功"else echo "将文件修正为.txt.bak失落败" exitfi# 打包以.bak结尾的文件cd /backup && tar czf bak.tar.gz $(find /backup -name ".bak") &>/dev/null# 判断打包是否成功if [ $? -eq 0 ];then echo "打包成功"else echo "打包失落败" exitfi# 将.bak结尾的文件还原为.txtfor j in $(find /backup -name ".bak")do mv $j ${j%.}done# 判断还原是否成功if [ $? -eq 0 ];then echo "成功将文件还原为.txt"else echo "还原失落败"fi4. 案例四:把以下的字符串中长度小于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/bashstring='When find examines or prints information a file'# 显示要打印的字符串echo "$string"# 打印长度小于5的字符串for i in ${string}do [ ${#i} -lt 5 ] && echo $idoneShell变量运算加: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 + $num215[root@shell /scripts]# expr $num1 - $num25[root@shell /scripts]# expr $num1 $num2expr: syntax error[root@shell /scripts]# expr $num1 \ $num250[root@shell /scripts]# expr $num1 / $num22[root@shell /scripts]# expr $num1 % $num202. $(( ))[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 ))13. $[ ][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 ]14. 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 $a13[root@shell /scripts]# let a--[root@shell /scripts]# echo $a12[root@shell /scripts]# echo $a12[root@shell /scripts]# let a--[root@shell /scripts]# echo $a11小数运算小数打算命令: bc、awk、python 1. bc[root@shell /scripts]# yum install -y bc[root@shell /scripts]# bcbc 1.06.95Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.This is free software with ABSOLUTELY NO WARRANTY.For details type `warranty'. 1+123/21^C(interrupt) Exiting bc.[root@shell /scripts]# echo 10 + 20 | bc30[root@shell /scripts]# echo $num1 + $num2 | bc16[root@shell /scripts]# echo $num1 / $num2 | bc2[root@shell /scripts]# echo "scale=2;$num1 / $num2" | bc2.20[root@shell /scripts]# echo "scale=1;$num1 / $num2" | bc2.2[root@shell /scripts]# echo "scale=5;$num1 / $num2" | bc2.20000[root@shell /scripts]# echo "$num1 ^ $num2" | bc #求方运算1610512. 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]# pythonPython 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> 1+12>>> 10^100>>> 10/33>>> 10.0/33.3333333333333335>>> KeyboardInterrupt>>> quitUse quit() or Ctrl-D (i.e. EOF) to exit>>> quit() [root@shell /scripts]# echo "print $num1 + $num2" | python16[root@shell /scripts]# echo "print $num1 - $num2" | python6[root@shell /scripts]# echo "print $num1 ^ $num2" | python14[root@shell /scripts]# echo "print $num1 / $num2" | python2[root@shell /scripts]# echo "print ${num1}.0 / $num2" | python2.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/bashVsz_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 = 3010 - 20 = -10 [root@shell ~]# cat cal.sh #!/bin/bashread -p "请输入第一个数字:" Num1read -p "请输入第二个数字:" Num2echo "$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/bashVersion=$(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)删除字符串中所有的Hadoop3)更换第一个Hadoop为Linux4)更换全部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<<EOF1)打印string长度2)删除字符串中所有的Hadoop3)更换第一个Hadoop为Linux4)更换全部Hadoop为LinuxEOF#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
