Shell 变量及函数讲解 [2]
Shell 变量的输入
[root@web02 ~]# read -p "Pls input one num:" numPls input one num:11
[root@web02 ~]# read -t 3 -p "Pls input one num:" numPls input one num:[root@web02 ~]#
[root@web02 ~]# read -t 18 -p "Pls input one num:" numPls input one num:18[root@web02 ~]# echo $num18其中此步骤相当于赋值,num=18
[root@web02 scripts]# sh abc.sh Pls input one num:1818[root@web02 scripts]# cat abc.sh #!/bin/bashread -t 18 -p "Pls input one num:" numecho $nu
read在脚本中常用的例子:
[root@web02 scripts]# sh abc.sh Pls input one num:1 21-2 =-11+2 =31*2 =21/2 =01**2 =11%2 =1[root@web02 scripts]# cat abc.sh #!/bin/bashread -t 18 -p "Pls input one num:" a becho "$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@web02 scripts]# sh b.sh 请输入号码:2 42+4 =62*4 =82/4 =02**4 =162%4 =2[root@web02 scripts]# cat b.sh #!/bin/bashread -p "请输入号码": num1 num2a=$num1b=$num2echo "$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@web02 scripts]# sh b.sh 请输入两个数字:2 52+5 =72*5 =102/5 =02**5 =322%5 =2[root@web02 scripts]# cat b.sh #!/bin/bashecho -n "请输入两个数字:"read 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 ))"
read 脚本常见的错误
[root@web02 scripts]# cat bash.sh #!/bin/basha=$1b=$2read -p "pls input" echo "a-b =$(( $a - $b ))"echo "a+b =$(( $a + $b ))"echo "a*b =$(( $a * $b ))"错误:read 后面没有变量,会造成下面无法引用。造成错误在 read 添加 a b即可-------------------------------------------------------正确操作如下[root@web02 scripts]# cat bash.sh #!/bin/basha=$1b=$2read -p "pls input" a becho "a-b =$(( $a - $b ))"echo "a+b =$(( $a + $b ))"echo "a*b =$(( $a * $b ))"
[root@web02 scripts]# cat bash.sh #!/bin/basha=$1b=$2read -p "pls input" $1 $2echo "a-b =$(( $a - $b ))"echo "a+b =$(( $a + $b ))"echo "a*b =$(( $a * $b ))"错误:其中read 后面的$1 已经有了变量,不能重复使用
[root@web02 scripts]# cat bash.sh #!/bin/basha=$1b=$2read -p "pls input" $aread -p "pls input" $b echo "a-b =$(( $a - $b ))"echo "a+b =$(( $a + $b ))"echo "a*b =$(( $a * $b ))"错误:1. 首先read 这样写会比较麻烦,相当于输入一个2 还需要在输入一个2 才可以输出2.read 已经是一个变量了,不可以在定义a=$1
[root@web02 scripts]# sh abc.sh 请输入2个数字:6 06-0 =66+0 =66*0 =00=0 can not be 0,so /,% cat not yunsuan6**0 =1[root@web02 scripts]# sh abc.sh 请输入2个数字:6 96-9 =-36+9 =156*9 =546/9 =06%9 =66**9 =10077696[root@web02 scripts]# sh abc.sh 请输入2个数字:a b请输入整数[root@web02 scripts]# sh abc.sh 请输入2个数字:a 8请输入整数[root@web02 scripts]# sh abc.sh 请输入2个数字:8 8 8请输入整数======================================脚本内容:[root@web02 scripts]# cat abc.sh #!/bin/bashread -t 18 -p "请输入2个数字:" a b#no.1[ -z "$a" ]&&{ echo "必须为整数" exit 1}[ -z "$b" ]&&{ echo "输入两个整数" exit 2}#no.2expr $a + $b + 1 &>/dev/null[ $? -ne 0 ]&&{ echo "请输入整数"exit 3}#no.3echo "$a-$b =$(( $a - $b ))"echo "$a+$b =$(( $a + $b ))"echo "$a*$b =$(( $a * $b ))"if [ $b -eq 0 ];thenecho "$b=0 can not be 0,so /,% cat not yunsuan"elseecho "$a/$b =$(( $a / $b))"echo "$a%$b =$(( $a % $b))"fiecho "$a**$b =$(( $a ** $b ))"
条件测试与比较
test测试表达式
1.判断是不是文件[root@web02 scripts]# test -f /etc/hosts &&echo 1||echo 01[root@web02 scripts]# test -f /etc/hostsa &&echo 1||echo 002.判断文件是否可以执行[root@web02 scripts]# test -x /server/scripts/abc.sh &&echo 1||echo 00[root@web02 scripts]# chmod +x abc.sh [root@web02 scripts]# test -x /server/scripts/abc.sh &&echo 1||echo 013.判断是不是目录[root@web02 scripts]# test -d /etc/ &&echo 1||echo 01[root@web02 scripts]# test -d /etc/hosts &&echo 1||echo 004.是否为空,为空是真,不为空是假[root@web02 scripts]# test -z "oldboy"&& echo 1||echo 00[root@web02 scripts]# test -z ""&& echo 1||echo 01
[]中括号表达式
1、判断是不是普通文件[root@web02 scripts]# [ -f /etc/hosts ]&&echo 1||echo 01[root@web02 scripts]# [ -f /etc/host ]&&echo 1||echo 002、是否是目录[root@web02 scripts]# [ -d /etc/host ]&&echo 1||echo 00[root@web02 scripts]# [ -d /etc/ ]&&echo 1||echo 013、是否可执行[root@web02 scripts]# [ -x /etc/hosts ]&&echo 1||echo 00[root@web02 scripts]# [ -x /server/scripts/abc.sh ]&&echo 1||echo 01
[[]]双括号表达式
[root@web02 scripts]# [[ -x /server/scripts/abc.sh ]]&&echo 1||echo 01[root@web02 scripts]# [[ -x /etc/hosts ]]&&echo 1||echo 00
[root@web02 scripts]# [[ -d /etc/ && -f /server/scripts/abc.sh ]]&&echo 1||echo 01[root@web02 scripts]# [[ -d /etc/ && -f /server/scripts/aabc.sh ]]&&echo 1||echo 00
文件测试表达式
操作符 | 说明 | 举例 |
---|---|---|
-b file | 检测文件是否是块设备文件,如果是,则返回 true。 | [ -b $file ] 返回 false。 |
-c file | 检测文件是否是字符设备文件,如果是,则返回 true。 | [ -c $file ] 返回 false。 |
-d file | 检测文件是否是目录,如果是,则返回 true。 | [ -d $file ] 返回 false。 |
-f file | 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 | [ -f $file ] 返回 true。 |
-g file | 检测文件是否设置了 SGID 位,如果是,则返回 true。 | [ -g $file ] 返回 false。 |
-k file | 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 | [ -k $file ] 返回 false。 |
-p file | 检测文件是否是具名管道,如果是,则返回 true。 | [ -p $file ] 返回 false。 |
-u file | 检测文件是否设置了 SUID 位,如果是,则返回 true。 | [ -u $file ] 返回 false。 |
-r file | 检测文件是否可读,如果是,则返回 true。 | [ -r $file ] 返回 true。 |
-w file | 检测文件是否可写,如果是,则返回 true。 | [ -w $file ] 返回 true。 |
-x file | 检测文件是否可执行,如果是,则返回 true。 | [ -x $file ] 返回 true。 |
-s file | 检测文件是否为空(文件大小是否大于0),不为空返回 true。 | [ -s $file ] 返回 true。 |
-e file | 检测文件(包括目录)是否存在,如果是,则返回 true。 | [ -e $file ] 返回 true。 |
例子:
[root@web02 ~]# touch oldboy[root@web02 ~]# ls -l oldboy-rw-r--r-- 1 root root 0 Jul 8 07:28 oldboy[root@web02 ~]# [ -f oldboy ]&& echo 1 ||echo 01[root@web02 ~]# [ -f oldboy1 ]&& echo 1 ||echo 00
[root@web02 ~]# [ -d oldgirl ]&& echo 存在||echo 不存在存在[root@web02 ~]# [ -d oldgirl1 ]&& echo 存在||echo 不存在不存在测试是否存在[root@web02 ~]# [ -e oldboy ]&& echo 存在||echo 不存在存在[root@web02 ~]# [ -e oldgirl ]&& echo 存在||echo 不存在存在
[root@web02 ~]# ls -l oldboy -rw-r--r-- 1 root root 0 Jul 8 07:28 oldboy[root@web02 ~]# [ -r oldboy ]&&echo 1||echo 21[root@web02 ~]# [ -w oldboy ]&&echo 1||echo 21[root@web02 ~]# [ -x oldboy ]&&echo 1||echo 22[root@web02 ~]# chmod +x oldboy [root@web02 ~]# [ -x oldboy ]&&echo 1||echo 21
[root@web02 ~]# file1=/etc/services [root@web02 ~]# file2=/etc/rc.local
[root@web02 ~]# [ -f $file1 ]&&echo 1||echo 01[root@web02 ~]# [ -d $file2 ]&&echo 1||echo 00最谨慎的操作步骤应该是加上双引号[root@web02 ~]# [ -f "$file1" ]&&echo 1||echo 01[root@web02 ~]# [ -d "$file2" ]&&echo 1||echo 00提示:此处判断的是file2的变量的目录是否存在
[root@web02 ~]# [ -f "$file1" ]||echo 2[root@web02 ~]# [ -f "$fil11e1" ]||echo 22
[root@web02 ~]# [ -f /etc/hosts ]&&{ echo 1;echo 2;echo 3;echo 3; }123
[root@web02 ~]# sh 1.sh 3123[root@web02 ~]# cat 1.sh #!/bin/bash[ $1 -eq 3 ]&&{echo 1echo 2echo 3}
[ 3 -ne 3 ]||{ echo "I am ok" echo "I am no" exit 1}
字符串表达式
常用字符串测试操作符 | 说明 |
-z “字符串” | 若串长度为0则真,-z可以理解为zero |
-n ”字符串“ | 若昂度不为0则真,-n 可以理解为no zero |
”串1“ = ”串2“ | 若串1等于串2则真,可以使用”==“代替”=“ |
“串2” != “串2” | 若串1不等于串2则真,不能用“!==“ 代替”!=“ |
字符串测试操作符提示:
[root@web02 ~]# [ -n "abc" ]&&echo 1||echo 01[root@web02 ~]# [ -n "" ]&&echo 1||echo 00[root@web02 ~]# [ -z abc"" ]&&echo 1||echo 00[root@web02 ~]# [ ! -z abc"" ]&&echo 1||echo 01
[root@web02 ~]# test=oldboy[root@web02 ~]# [ -n "$test" ]&&echo 1||echo 01[root@web02 ~]# [ -z "$test" ]&&echo 1||echo 00
[root@web02 ~]# [ "$test" = "oldboy" ]&&echo 1||echo 21[root@web02 ~]# [ "$test" = "oldbo1y" ]&&echo 1||echo 22
在[]以及test 中会用的比较符号 | 在(())和[[]]中使用的比较符 | 说明 |
-eq | ==或== | equal的偶写,相等 |
-ne | != | not equal的缩写,不相等 |
-gt | > | 大于greater than |
-ge | >= | 大于等于greater equal |
-lt | < | 小于类似less than |
-le | <= | 小于等于less equal |
更多参数可以使用man test
[root@web02 ~]# [ 2 -eq 1 ]&&echo 1||echo 00[root@web02 ~]# [ 2 -ne 1 ]&&echo 1||echo 01[root@web02 ~]# [ 2 -gt 1 ]&&echo 1||echo 01[root@web02 ~]# [ 2 -ge 1 ]&&echo 1||echo 01[root@web02 ~]# [ 2 -lt 1 ]&&echo 1||echo 00[root@web02 ~]# [ 2 -le 1 ]&&echo 1||echo 00
[root@web02 ~]# grep -w "\-eq" /etc/init.d/nfs [ $RETVAL -eq 0 ] && RETVAL=$rval [ $RETVAL -eq 0 ] && RETVAL=$rval [ $RETVAL -eq 0 ] && RETVAL=$rval [ $RETVAL -eq 0 ] && RETVAL=$rval [ $RETVAL -eq 0 ] && RETVAL=$rval
逻辑操作符
在[]和test中使用 | 在[[]]中使用 | 说明 |
---|---|---|
-a | && | and与,两端都为真,则真 |
-o | || | or或,两端有一个为真则真 |
! | ! | not非,相反则为真 |
[root@web02 ~]# f1=/etc/rc.local;f2=/etc/services [root@web02 ~]# [ -f "$f1" -a -f "$f2" ]&&echo 1||echo 01
[root@web02 ~]# [ -f "$f1" && -f "$f2" ]&&echo 1||echo 0-bash: [: missing `]'0
[root@web02 ~]# [[ -f "$f1" && -f "$f2" ]]&&echo 1||echo 01==================================[root@web02 ~]# [ $a -eq 2 -a $b -eq 2 ]&&echo 1||echo 00[root@web02 ~]# [ $a -eq 1 -a $b -eq 2 ]&&echo 1||echo 01[root@web02 ~]# [ $a -eq 3 -a $b -eq 3 ]&&echo 1||echo 00[root@web02 ~]# [ $a -eq 3 -o $b -eq 3 ]&&echo 1||echo 00[root@web02 ~]# [ $a -eq 1 -o $b -eq 3 ]&&echo 1||echo 01[root@web02 ~]# [[ $a -eq 1 || $b -eq 3 ]]&&echo 1||echo 01
[root@web02 ~]# sed -n '87,90p' /etc/init.d/nfs [ "$NFSD_MODULE" != "noload" -a -x /sbin/modprobe ] && { /sbin/modprobe nfsd [ -n "$RDMA_PORT" ] && /sbin/modprobe svcrdma }
[root@web02 ~]# cat 1212.sh#!/bin/sh#no.1[ $# -ne 2 ]&&{ echo "USAGE:$0 num1 num2" exit 1}#no.2a=$1b=$2expr $a + 1 &>/dev/null[ $? -ne 0 ]&&{ echo "First arg must be int." exit 2}expr $b + 1 &>/dev/null[ $? -ne 0 ]&&{ echo "Second arg must be int." exit 3}#no.3[ $a -gt $b ]&&{ echo "$a > $b" exit 0}[ $a -eq $b ]&&{ echo "$a = $b" exit 0}[ $a -lt $b ]&&{ echo "$a < $b" exit 0}
[root@web02 ~]# cat www.sh#!/bin/sh#no.1read -p "Pls input two num:" a b[ -z "$a" -o -z "$b" ]&&{ echo "must be two num." exit 1}#no.2expr $a + 1 &>/dev/null[ $? -ne 0 ]&&{ echo "First arg must be int." exit 2}expr $b + 1 &>/dev/null[ $? -ne 0 ]&&{ echo "Second arg must be int." exit 3}#no.3[ $a -gt $b ]&&{ echo "$a > $b" exit 0}[ $a -eq $b ]&&{ echo "$a = $b" exit 0}[ $a -lt $b ]&&{ echo "$a < $b" exit 0}
综合案例:打印选择菜单
[root@web02 scripts]# cat install.sh #!/bin/bash. /etc/init.d/functions export PATHmenu(){cat <<END 1.[install lamp] 2.[install lnmp] 3.[exit]END}menuread -p "pls input the num you want" acase "$a" in 1) action "正在安装lnmp" /bin/true exit 0 ;; 2) action "正在安装lamp" /bin/true exit 0 ;; 3) exit 0 ;; [4-9]) echo "请重新输入" exit 1 ;;esac
分支与循环结构
if [ 条件 ] then 指令fi或if [ 条件 ];then 指令fi
if [ 条件 ]then 指令集1else 指令集2fi
if 条件1then 指令1elif 条件2then指令2else 指令3fi ----- if 条件then 指令 elif 条件then 指令 elif 条件then 指令... ...else 指令fi
[root@db01 scripts]# cat free.sh #!/bin/shmem=$(free -m|awk 'NR==3{print $NF}')if [ $mem -lt 1000 ]then echo "mem is not enough. $mem" echo "mem is not enough. $mem"|mail -s "`date` mem is not enough" 31333741-@qq.comelse echo okfi[root@db01 scripts]# crontab -l#time sync by oldboy at 2010-2-1*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1###########*/3 * * * * /bin/sh /server/scripts/free.sh >/dev/null 2>&1
[root@db01 scripts]# cat read.sh #!/bin/sh#no.1read -p "Pls input two num:" a b[ -z "$a" -o -z "$b" ]&&{ echo "must be two num." exit 1}#no.2expr $a + 1 &>/dev/null[ $? -ne 0 ]&&{ echo "First arg must be int." exit 2}expr $b + 1 &>/dev/null[ $? -ne 0 ]&&{ echo "Second arg must be int." exit 3}#no.3[ $a -gt $b ]&&{ echo "$a > $b" exit 0}[ $a -eq $b ]&&{ echo "$a = $b" exit 0}[ $a -lt $b ]&&{ echo "$a < $b" exit 0}
未经允许不得转载:Linux 自动化运维之路~ » Shell 变量
Shell 变量及函数讲解 [2]相关推荐
- Linux 命令之 unset -- 删除指定的shell变量或函数
文章目录 一.命令介绍 二.选项参数 三.命令示例 (一)删除指定的环境变量 一.命令介绍 unset 为 shell 内建指令,用于删除已定义的shell变量(包括环境变量)和shell函数.uns ...
- shell变量、函数和数组以及字符串的截取
一.变量 1.shell变量名 (1)可以由字母.数字.下划线等字符组成.但是第一个字符必须是字母或者下划线. (2)若果变量中包含下划线(_)则要特别注意,$project_svn_$date.ta ...
- Linux 命令之 typeset -- 声明 shell 变量,设置变量的属性
文章目录 一.命令介绍 二.常用选项 三.命令示例 (1)定义关联数组并访问 一.命令介绍 typeset 命令是 bash 的内建命令,是命令 declare 的别名,两者是完全一样的,用来声明 s ...
- [转]Shell中脚本变量和函数变量的作用域
转自: http://blog.csdn.net/ltx19860420/article/details/5570902 在shell中定义函数可以使代码模块化,便于复用代码.不过脚本本身的变 ...
- Shell 变量的作用域
Shell 变量的作用域(Scope),就是 Shell 变量的有效范围(可以使用的范围). 在不同的作用域中,同名的变量不会相互干涉,就好像 A 班有个叫小明的同学,B 班也有个叫小明的同学,虽然他 ...
- linux变量中代,linux中shell变量$#,$@,$0,$1,$2和变量${0%/*}
一直傻傻搞不清,还是梳理一下吧. linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Pr ...
- Shell 脚本知识回顾 (二) —— Shell变量
一.Shell变量:Shell变量的定义.删除变量.只读变量.变量类型 Shell支持自定义变量. 定义变量 定义变量时,变量名不加美元符号($),如: [cpp] view plaincopy va ...
- C语言的函数讲解(一)
文章目录 前言:什么是函数? 函数的定义 函数的组成 一.库函数是什么? 二.如何阅读库函数的文档 总结 前言:什么是函数? 函数的定义 在讲解库函数之前,首先明确一下函数的定义. 在计算机科学中,函 ...
- shell中的函数shell中的数组告警系统需求分析
2019独角兽企业重金招聘Python工程师标准>>> 20.16/20.17 shell中的函数 函数的概念 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用 ...
最新文章
- Asp.net中GridView使用详解(引)【转】
- 一键还原奥运版_福田奥铃CTS超越版和江淮全能卡车,哪款实力更强?
- 更改eclipse中jsp默认编码格式为UTF-8
- db2的bufferpool不足报错的快速解决
- 计算机考试题库电子版,全国计算机等级考试一级试题库大全完整版附参考答案...
- html5鼠标点击弹出层,jQuery实现单击弹出Div层窗口效果(可关闭可拖动)
- 团队作业4——第一次项目冲刺(Alpha版本)
- 帧率常量15可以随便改,问为什么是15,是想暴露自己不懂编程?
- c++win32项目 如何显示后再删除一个绘图_sai绘图软件中文版
- 什么是 BI?和报表有什么关系?有了 BI 还要做报表吗?
- 中专计算机应用专业简历模板,2016计算机应用专业个人简历模板
- 组装台式计算机的相关要求,台式电脑的组装配置清单
- python123作业怎么提交_使用python提交作业
- 忘了neo4j密码怎么办
- MySQL数据库乱码(中文变问号)问题解决
- keras神经风格迁移_知识分享 | 神经风格迁移-把每一张图片都变成自己喜欢的样子...
- 手机浏览器类型ua php,通过userAgent判断手机浏览器类型(示例代码)
- 社招和校招有什么不同?阿里美团等大厂JAVA社招面经分享!
- 计算机的ip地址和用户名和密码是什么原因,电脑的ip地址账户和密码忘记怎么办...
- Android开发在路上:少去踩坑,多走捷径(上)
热门文章
- r语言如何读取matlab数据类型,[转载]R语言数据类型解析[转]
- 线性回归 —— python
- Latex笔记(一)—— 复杂表格的制作
- 【youcans 的 OpenCV 例程200篇】114. 形态学操作之膨胀
- flume数据丢失与重复_Flume监听文件到kafka,文件通道,kafka消费重复问题
- git 提交文件_GIT不小心提交了大文件导致提交失败怎么办?
- 含枚举类型的函数声明_02Golang基础类型
- python调用shell脚本的参数_使用python执行shell脚本 并动态传参 及subprocess的使用详解
- oracle 查询cpu 100%,Oracle 11g中查询CPU占有率高的SQL
- 点工转自动化速成指南