typecho做网站百度如何建网站
typecho做网站,百度如何建网站,怀远做网站电话,网店代运营协议Shell 变量的输入 Shell变量除了可以直接赋值或脚本传参外#xff0c;还可以使用read命令从标准输入获得#xff0c;read为bash内置命令#xff0c;可以通过help read查看帮助【语法格式】read [参数] [变量名]【常用参数】-p prompt#xff1a;设置提示信息-t timeout还可以使用read命令从标准输入获得read为bash内置命令可以通过help read查看帮助【语法格式】 read [参数] [变量名]【常用参数】 -p prompt设置提示信息-t timeout设置输入等待的事件单位默认为秒 read的基本读入如果我们不加-t read就会一直等待。[rootweb02 ~]# read -p Pls input one num: numPls input one num:11 设置超时时间3秒[rootweb02 ~]# read -t 3 -p Pls input one num: numPls input one num:[rootweb02 ~]# read 后面的参数就是一个变量。[rootweb02 ~]# read -t 18 -p Pls input one num: numPls input one num:18[rootweb02 ~]# echo $num18其中此步骤相当于赋值num18 使用脚本操作步骤 [rootweb02 scripts]# sh abc.sh Pls input one num:1818[rootweb02 scripts]# cat abc.sh #!/bin/bashread -t 18 -p Pls input one num: numecho $nu read在脚本中常用的例子 第一种方法 [rootweb02 scripts]# sh abc.sh Pls input one num:1 21-2 -112 31*2 21/2 01**2 11%2 1[rootweb02 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 )) 第二种方法 [rootweb02 scripts]# sh b.sh 请输入号码:2 424 62*4 82/4 02**4 162%4 2[rootweb02 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 )) 第三种方法利用echo命令替代和read -p的功能 [rootweb02 scripts]# sh b.sh 请输入两个数字:2 525 72*5 102/5 02**5 322%5 2[rootweb02 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 脚本常见的错误 [1]错误 [rootweb02 scripts]# cat bash.sh #!/bin/basha$1b$2read -p pls input echo a-b $(( $a - $b ))echo ab $(( $a $b ))echo a*b $(( $a * $b ))错误read 后面没有变量会造成下面无法引用。造成错误在 read 添加 a b即可-------------------------------------------------------正确操作如下[rootweb02 scripts]# cat bash.sh #!/bin/basha$1b$2read -p pls input a becho a-b $(( $a - $b ))echo ab $(( $a $b ))echo a*b $(( $a * $b )) [2] 错误 [rootweb02 scripts]# cat bash.sh #!/bin/basha$1b$2read -p pls input $1 $2echo a-b $(( $a - $b ))echo ab $(( $a $b ))echo a*b $(( $a * $b ))错误其中read 后面的$1 已经有了变量不能重复使用 [3] 错误 [rootweb02 scripts]# cat bash.sh #!/bin/basha$1b$2read -p pls input $aread -p pls input $b echo a-b $(( $a - $b ))echo ab $(( $a $b ))echo a*b $(( $a * $b ))错误1. 首先read 这样写会比较麻烦相当于输入一个2 还需要在输入一个2 才可以输出2.read 已经是一个变量了不可以在定义a$1 实战判断输入2个数是否是整数 [rootweb02 scripts]# sh abc.sh 请输入2个数字:6 06-0 660 66*0 000 can not be 0,so /,% cat not yunsuan6**0 1[rootweb02 scripts]# sh abc.sh 请输入2个数字:6 96-9 -369 156*9 546/9 06%9 66**9 10077696[rootweb02 scripts]# sh abc.sh 请输入2个数字:a b请输入整数[rootweb02 scripts]# sh abc.sh 请输入2个数字:a 8请输入整数[rootweb02 scripts]# sh abc.sh 请输入2个数字:8 8 8请输入整数脚本内容[rootweb02 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 $b0 can not be 0,so /,% cat not yunsuanelseecho $a/$b $(( $a / $b))echo $a%$b $(( $a % $b))fiecho $a**$b $(( $a ** $b )) 条件测试与比较 在bash的各种流程控制结构中通常要进行各种测试然后根据测试结果执行不同的操作有时也会通过与if等条件语句相结合更方便的完成判断 条件测试通常由如下3种语法形式 语法1test测试表达式 语法2[测试表达式]语法3[[测试表达式]]说明1.上述语法格式1和语法格式2的写法是相等的。语法格式3为扩展的test命令。推荐使用语法格式22.在[[]]中可以使用通配符进行模式匹配。、||、、等操作可以应用于[[]]中但不能应用于[]中3.对于整数的关系运算也可以使用Shell的算术运算符(()) test测试表达式 1.判断是不是文件[rootweb02 scripts]# test -f /etc/hosts echo 1||echo 01[rootweb02 scripts]# test -f /etc/hostsa echo 1||echo 002.判断文件是否可以执行[rootweb02 scripts]# test -x /server/scripts/abc.sh echo 1||echo 00[rootweb02 scripts]# chmod x abc.sh [rootweb02 scripts]# test -x /server/scripts/abc.sh echo 1||echo 013.判断是不是目录[rootweb02 scripts]# test -d /etc/ echo 1||echo 01[rootweb02 scripts]# test -d /etc/hosts echo 1||echo 004.是否为空为空是真不为空是假[rootweb02 scripts]# test -z oldboy echo 1||echo 00[rootweb02 scripts]# test -z echo 1||echo 01 []中括号表达式 1、判断是不是普通文件[rootweb02 scripts]# [ -f /etc/hosts ]echo 1||echo 01[rootweb02 scripts]# [ -f /etc/host ]echo 1||echo 002、是否是目录[rootweb02 scripts]# [ -d /etc/host ]echo 1||echo 00[rootweb02 scripts]# [ -d /etc/ ]echo 1||echo 013、是否可执行[rootweb02 scripts]# [ -x /etc/hosts ]echo 1||echo 00[rootweb02 scripts]# [ -x /server/scripts/abc.sh ]echo 1||echo 01 [[]]双括号表达式 [rootweb02 scripts]# [[ -x /server/scripts/abc.sh ]]echo 1||echo 01[rootweb02 scripts]# [[ -x /etc/hosts ]]echo 1||echo 00 此括号和[[]]几乎和[]一样 区别是可以在多括号里面添加多个判断 例如判断是不是目录 并判断下一个文件是不是可执行 [rootweb02 scripts]# [[ -d /etc/ -f /server/scripts/abc.sh ]]echo 1||echo 01[rootweb02 scripts]# [[ -d /etc/ -f /server/scripts/aabc.sh ]]echo 1||echo 00 当2个参数都成立的时候就输入echo 1 当不成立的时候echo 2 提示只在双括号里生效如果在单括号需要使用-a双中括号或用|| 单括号使用-o 其中-a and -o or 文件测试表达式
常用的文件测试操作符操作符 说明 举例 -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。 特别说明这些操作符号对于[[]]、[]、test几乎都是通用的更多操作符请man test查询例子
测试文件是否存在[rootweb02 ~]# touch oldboy[rootweb02 ~]# ls -l oldboy-rw-r--r-- 1 root root 0 Jul 8 07:28 oldboy[rootweb02 ~]# [ -f oldboy ] echo 1 ||echo 01[rootweb02 ~]# [ -f oldboy1 ] echo 1 ||echo 00 测试目录 [rootweb02 ~]# [ -d oldgirl ] echo 存在||echo 不存在存在[rootweb02 ~]# [ -d oldgirl1 ] echo 存在||echo 不存在不存在测试是否存在[rootweb02 ~]# [ -e oldboy ] echo 存在||echo 不存在存在[rootweb02 ~]# [ -e oldgirl ] echo 存在||echo 不存在存在 测试文件属性 [rootweb02 ~]# ls -l oldboy -rw-r--r-- 1 root root 0 Jul 8 07:28 oldboy[rootweb02 ~]# [ -r oldboy ]echo 1||echo 21[rootweb02 ~]# [ -w oldboy ]echo 1||echo 21[rootweb02 ~]# [ -x oldboy ]echo 1||echo 22[rootweb02 ~]# chmod x oldboy [rootweb02 ~]# [ -x oldboy ]echo 1||echo 21 测试shell变量定义 [rootweb02 ~]# file1/etc/services [rootweb02 ~]# file2/etc/rc.local 对单个文件变量测试 [rootweb02 ~]# [ -f $file1 ]echo 1||echo 01[rootweb02 ~]# [ -d $file2 ]echo 1||echo 00最谨慎的操作步骤应该是加上双引号[rootweb02 ~]# [ -f $file1 ]echo 1||echo 01[rootweb02 ~]# [ -d $file2 ]echo 1||echo 00提示此处判断的是file2的变量的目录是否存在 生产场景案例例如rpcbind 另一种写法 如果文件不存在我就进行操作 [rootweb02 ~]# [ -f $file1 ]||echo 2[rootweb02 ~]# [ -f $fil11e1 ]||echo 22 特殊文件测试表达式写法案例 条件表达式判断条件后面执行多条命令语句写法。 范例1当条件1成立时同时执行命令1、命令2、命令3. 用法 [ 条件1 ]{ 命令1 命令2 命令3 } 示例 [rootweb02 ~]# [ -f /etc/hosts ]{ echo 1;echo 2;echo 3;echo 3; }123 等于3 执行什么操作 [rootweb02 ~]# sh 1.sh 3123[rootweb02 ~]# 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} 字符串表达式 字符串测试操作符的作用有比较两个字符串是否相同、字符串的长度是否为零字符串是否为NULL注bash区分零长度字符串和空字符串等 常用字符串测试操作符 说明 -z “字符串” 若串长度为0则真-z可以理解为zero -n ”字符串“ 若昂度不为0则真-n 可以理解为no zero ”串1“ ”串2“ 若串1等于串2则真可以使用”“代替”“ “串2” “串2” 若串1不等于串2则真不能用““ 代替”“ 特别注意 1. 以上表格中的字符串测试操作符号务必要用”“引起来。 2.比较符号两端有空格 字符串测试操作符提示 1-n 比较字符串长度是否不为零如果不为零则为真如[ -n “$myvar” ] 2-z 比较字符串长度是否等于零如果等于零则为真如[ -z “$myvar” ] 特别注意对于以上表格中的字符串测试操作符号如[ -n “$myvar” ]要把字符串用“”引起来。 注意事项 1、字符串或字符串变量比较都要加双引号之后再比较。 2、字符串或字符串变量比较比较符号两端最好都有空格可以参考系统脚本 “”比较两个字符串是否相同与“”等价如[ “$a” “$b” ]其中$a这样的变量最好用“”括起来因为如果中间由空格*等符号就可能出错更好的办法就是[ “${a}” “${b}” ] “” 比较两个字符串是否相同不同则为“是” [rootweb02 ~]# [ -n abc ]echo 1||echo 01[rootweb02 ~]# [ -n ]echo 1||echo 00[rootweb02 ~]# [ -z abc ]echo 1||echo 00[rootweb02 ~]# [ ! -z abc ]echo 1||echo 01 其中-z 相当于-n 例子演示 [rootweb02 ~]# testoldboy[rootweb02 ~]# [ -n $test ]echo 1||echo 01[rootweb02 ~]# [ -z $test ]echo 1||echo 00 相等测试 [rootweb02 ~]# [ $test oldboy ]echo 1||echo 21[rootweb02 ~]# [ $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 例子演示 [rootweb02 ~]# [ 2 -eq 1 ]echo 1||echo 00[rootweb02 ~]# [ 2 -ne 1 ]echo 1||echo 01[rootweb02 ~]# [ 2 -gt 1 ]echo 1||echo 01[rootweb02 ~]# [ 2 -ge 1 ]echo 1||echo 01[rootweb02 ~]# [ 2 -lt 1 ]echo 1||echo 00[rootweb02 ~]# [ 2 -le 1 ]echo 1||echo 00 特别提示 经过实践“”和“”在[]中使用不需要转移包含“”和“”的符号在[]中使用需要转移对于数字不转义的结果未必会报错但是结果可能不对 实际测试结果结论 1、整数加双引号也是对的。 2、[[]]用-eq等的写法也是对的[[]]用写法也可能不对只比较第一位逻辑结果不对。 3、[]用号的写法语法没错逻辑结果不对。 工作场景推荐[]的-eq的用法。 [rootweb02 ~]# 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非相反则为真 提示 中文意思是反与一个逻辑值相关的逻辑值 -a 中文意思是and|两个逻辑值都为“真”返回值才为“真”反之为“假” -o 中文意思是或or| ||两个逻辑值只要有一个为“真”返回值就为“真” 逻辑操作运算规则 结论-a和 的运算规则只有两端都是1才为真真true 假false举个例子 [rootweb02 ~]# f1/etc/rc.local;f2/etc/services [rootweb02 ~]# [ -f $f1 -a -f $f2 ]echo 1||echo 01 如果使用就会报错 [rootweb02 ~]# [ -f $f1 -f $f2 ]echo 1||echo 0-bash: [: missing ]0 要想使用就加双括号 [rootweb02 ~]# [[ -f $f1 -f $f2 ]]echo 1||echo 01[rootweb02 ~]# [ $a -eq 2 -a $b -eq 2 ]echo 1||echo 00[rootweb02 ~]# [ $a -eq 1 -a $b -eq 2 ]echo 1||echo 01[rootweb02 ~]# [ $a -eq 3 -a $b -eq 3 ]echo 1||echo 00[rootweb02 ~]# [ $a -eq 3 -o $b -eq 3 ]echo 1||echo 00[rootweb02 ~]# [ $a -eq 1 -o $b -eq 3 ]echo 1||echo 01[rootweb02 ~]# [[ $a -eq 1 || $b -eq 3 ]]echo 1||echo 01 系统案例 [rootweb02 ~]# sed -n 87,90p /etc/init.d/nfs [ $NFSD_MODULE ! noload -a -x /sbin/modprobe ] { /sbin/modprobe nfsd [ -n $RDMA_PORT ] /sbin/modprobe svcrdma } 小结逻辑操作符使用总结 []中用-a-o [[]]中用|| test用法和[]相同 多个[]之间以及多个[[]]之间或者任意混合中间逻辑操作符都是或|| 特别声明有关test[][[]]的操作符的用法help test或man test查询第一手帮助。 综合实战案例开发Shell脚本分别实现以定义变量脚本传参以及read读入的方式比较2个整数大小。用条件表达式禁止if进行判断并以屏幕输出的方式提醒用户比较结果。注意。一共是开发3个脚本。当用脚本传参以及read读入的方式需要对变量是否为数字、并且传参个数不对给予提示。 第一种方法 [rootweb02 ~]# 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} 第二种方式 [rootweb02 ~]# 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} 综合案例打印选择菜单 [rootweb02 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 因为此脚本比较简单我不详细介绍。下面可以看一下操作因为很多地方没有定义直接设置为空还需完善 分支与循环结构 [1] 单分支结构语法if [ 条件 ] then 指令fi或if [ 条件 ]then 指令fi [2] 双分支结构 语法if [ 条件 ]then 指令集1else 指令集2fi [3] 多分支结构 语法 if 条件1then 指令1elif 条件2then指令2else 指令3fi ----- if 条件then 指令 elif 条件then 指令 elif 条件then 指令... ...else 指令fi 范例2开发shell脚本判断系统剩余内存的大小如果低于100M就邮件报警给管理员并且系统定时任务每3分支执行一次检查。 思路 1.分析需求 2.简单设计 a.取当前内存大小 b.判断是否大于10m如果小于100M就报警 c.加入定时任务每三分钟检查一次 3.编码实现 [rootdb01 scripts]# cat free.sh #!/bin/shmem$(free -m|awk NR3{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[rootdb01 scripts]# crontab -l#time sync by oldboy at 2010-2-1*/5 * * * * /usr/sbin/ntpdate time.nist.gov /dev/null 21###########*/3 * * * * /bin/sh /server/scripts/free.sh /dev/null 21 范例使用read及脚本传参方式如何实现2个整数比较大小 解答特别强调read读入和命令行传参是两种输入内容的方法两个脚本[rootdb01 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 变量
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/88442.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!