哈哈哈哈哈哈
这篇文章不错!
这篇文章不错!
作者对主题的挖掘深入骨髓,展现了非凡的洞察力和理解力。
情感真挚自然,字里行间传递出强烈的感染力。
作者的情感表达细腻入微,让人在阅读中找到了心灵的慰藉。
内容的丰富性和深度让人仿佛置身于知识的海洋,受益匪浅。
# 图片回复
你的文章让我感受到了正能量,非常棒! https://www.4006400989.com/qyvideo/93624.html
你的文章充满了智慧,让人敬佩。 https://www.yonboz.com/video/12554.html
你的文章让我感受到了正能量,非常棒! https://www.4006400989.com/qyvideo/82001.html
你的文章充满了智慧,让人敬佩。 https://www.yonboz.com/video/83913.html
你的文章让我感受到了正能量,非常棒! https://www.4006400989.com/qyvideo/82001.html
你的文章充满了智慧,让人敬佩。 https://www.yonboz.com/video/83913.html
首页
网站统计
关于本站
在线留言
友链申请
高清壁纸
论坛
开往
虫洞
推荐
Linux命令
资源网
Search
1
Typecho Cuteen主题美化
5,129 阅读
2
京东呆瓜搭建青龙面板+xdd-plus机器人+nvjdc配置
4,597 阅读
3
好久不见之网站底部样式
4,237 阅读
4
傻妞机器人最新版安装教程
4,108 阅读
5
Joe 主题 6.xx 底部增强,显示标签及二维码分享
3,114 阅读
Linux
Shell
Mysql
Typecho
网络
其他
Redis
登录
Search
标签搜索
Linux
Typecho
美化
Nginx
Shell
综合架构
Mysql
Joe
源码
Web
数据备份
命令
Ansible
k8s
定时任务
视频
网易云
白嫖
网络
Rsync
小黑
累计撰写
155
篇文章
累计收到
921
条评论
博主
4月14日
在线
首页
栏目
Linux
Shell
Mysql
Typecho
网络
其他
Redis
页面
网站统计
关于本站
在线留言
友链申请
高清壁纸
推荐
Linux命令
资源网
开往
搜索到
9
篇与
Shell
的结果
2021-08-16
NGINX日志切割
前言Nginx日志对于统计、系统服务排错很有用。Nginx日志主要分为两种:access_log(访问日志)和error_log(错误日志)。access.log 记录哪些用户,哪些页面以及用户浏览器,IP等访问信息;error.log 记录服务器错误的日志;设置access.log在nginx.conf文件里面配置access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; #设置访问日志 access_log off; #关闭访问日志path 指定日志的存放位置。format 指定日志的格式。默认使用预定义的combined。buffer 用来指定日志写入时的缓存大小。默认是64k。gzip 日志写入前先进行压缩。压缩率可以指定,从1到9数值越大压缩比越高,同时压缩的速度也越慢。默认是1。flush 设置缓存的有效时间。如果超过flush指定的时间,缓存中的内容将被清空。if 条件判断。如果指定的条件计算为0或空字符串,那么该请求不会写入日志。另外,还有一个特殊的值off。如果指定了该值,当前作用域下的所有的请求日志都被关闭。示例http { ... ##日志格式使用默认的combined;指定日志的缓存大小为32k;日志写入前启用gzip进行压缩,压缩比使用默认值1;缓存数据有效时间为1分钟。 access_log /var/logs/nginx-access.log buffer=32k gzip flush=1m; ... }access_log指令的作用域分别有http,server,location。log_format自定义格式默认的日志格式log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';tail -f /var/log/nginx/access.log设置error_log错误日志在Nginx中是通过error_log指令实现的。该指令记录服务器和请求处理过程中的错误信息。错误日志不支持自定义。error_log path [level];path参数指定日志的写入位置。level参数指定日志的级别。level可以是debug, info, notice, warn, error, crit, alert,emerg中的任意值(等级从低到高排列)。只有日志的错误级别等于或高于level指定的值才会写入错误日志中。默认值是error。error_log logs/error.log; error_log logs/error_notice.log notice; error_log logs/error_info.log info; ##可以将不同的错误类型分开存储日志切割测试按分钟分割编写脚本vim logaccess.sh #!/bin/bash #设置日志文件保存目录 LOG_PATH=/var/log/nginx/ #备份文件名称 LOG_BAK="$(date -d "last_minute" + %Y%m%d%H%M)" #重命名日志文件 mv ${LOG_PATH}/access.log ${LOG_PATH}/access.${LOG_BAK}.log # mv ${LOG_PATH}/error.log ${LOG_PATH}/error.${LOG_BAK}.log #向nginx主进程发信号重新打开日志 kill -USR1 `cat /run/nginx.pid`chmod +x logaccess.sh设置定时任务crontab -e #每分钟进行日志切割 */1 * * * * sh /root/logaccess.sh > /dev/null 2>&1按日切割:vim log2.sh #!/bin/bash #设置日志文件保存目录 LOG_PATH=/var/log/nginx/ #备份文件名称 LOG_PATH_BAK="$(date -d "yesterday" +%Y%m%d)" #重命名日志文件 mv ${LOG_PATH}/access.log ${LOG_PATH}/access.${LOG_BAK}.log # mv ${LOG_PATH}/error.log ${LOG_PATH}/error.${LOG_BAK}.log #向nginx主进程发信号重新打开日志 kill -USR1 `cat /run/nginx.pid`设置定时任务crontab -e #每天23:59分开始执行 59 23 * * * sh /root/logaccess.sh > /dev/null 2>&1
2021年08月16日
507 阅读
0 评论
1 点赞
2021-07-03
break、continue、exit、return的区别和对比
break、continue在条件语句及循环语句(for、while、if等)中用于控制程序的走向;而exit则表示终止所有语句并退出当前脚本,exit除此之外还可以返回上一次程序或命令的执行状态值返回给当前shell;return和exit类似,只不过return仅用于函数内部返回函数执行的状态值。{mtitle title="break执行流程逻"/} {mtitle title="continue执行流程逻"/} {mtitle title="exit执行流程逻"/} 示例#!/bin/bash if [ $# -ne 1 ]; then echo "Usage:$0 {break|continue|exit|return}" exit 1 fi test() { for ((i=0; i<=5; i++)) do if [ $i -eq 3 ];then $*; fi echo $i done echo "6666" } test $* func_ret=$? if [ `echo $*|grep return|wc -l` -eq 1 ];then echo "return's exit status:$func_ret" fi echo "ok" }
2021年07月03日
598 阅读
0 评论
0 点赞
2021-07-01
Shell for循环和select循环语句
for循环格式for 变量名 in 变量取值列表 do 指令 done示例打印5 4 3 2 1 5个数字#!/bin/bash for i in 5 4 3 2 1 do echo $i done 方法二 #!/bin/bash for (( i=5;i>0;i--)) do echo $i done PS:改名命令rename命令jpg格式的图片改成pngrename ".jpg" ".png" "*.jpg"打印九九乘法表#!/bin/bash for num1 in `seq 9` do for num2 in `seq 9` do if [ $num1 -ge $num2 ];then if ((($num1*$num2)>9)) ;then echo -en "${num1}x${num2}=$((num1*num2)) " else echo -en "${num1}x${num2}=$((num1*num2)) " fi fi done echo " " done计算1-100的和#!/bin/bash for ((i=1;i<=100;i++)) do ((sum=sum+$i)) done echo "$sum"select循环语句select 变量名 [ in 菜单取值列表] do 指令 done示例:select 循环打印简单菜单项 1、直接使用列表字符串#!/bin/bash select name in zhansan lisi wanger do echo $name done2、采用数组作为变量打印#!/bin/bash array=(zhansan lisi wanger) select $name in "${array[@]}" do echo "$name" done#!/bin/bash PS3="please select a num from menu:" #PS3是select循环的提示符 select name in zhansan lisi wanger do echo -e "I guess you selected the menu is:\n $REPLY) $name" #REPLY变量是菜单项对应的数字 done示例打印选择菜单 ,选择一键安装不同Web服务1.[install lamp] 2.[install lnmp] 3.[exit]#!/bin/bash RETVAR=0 path=/server/scripts #定义脚本路径 [ ! -d "$path" ] && mkdir $path -p #如果不存在就创建 function Usage() { echo "Usage :$0 argv" return 1 } function InstallService() { if [ $# -ne 1 ]; then Usage fi local RETVAR=0 echo "start installing ${1}." sleep 2 if [ ! -x "$path/${1}.sh" ]; then echo "$path/${1}.sh 没有执行权限" return 1 else $path/${1}.sh return $RETVAR fi } function main() { PS3="please input the num you want:" select num in "Install lamp" "Install lnmp" "exit" do case "$num" in "Install lamp") InstallService lamp RETVAR=$? ;; "Install lnmp") InstallService lnmp RETVAR=$? ;; exit) echo bye. return 3 ;; *) echo "the num you input must be {1|2|3}" echo "Input error" esac done echo $ RETVAR } main
2021年07月01日
294 阅读
0 评论
0 点赞
2021-06-26
Shell while循环和until循环语句
while 循环语句的基本语法while <条件表达式> do 指令... done示例:显示每分钟负载情况#!/bin/bash while true do uptime >> uptime.txt sleep 5 done系统显示时间 22:29:31up 6 days 距上次启动开始系统运行时间1 user 一个连接数load average 分别表示1分钟 5分钟 15 分钟系统的平均负载示例:打印54321#!/bin/bash i=5 while (( i > 0 )) do echo "$i" (( i-- )) done #或者用以下 #while [ $i -gt 0 ] #do # echo "$i" # (( i-- )) #doneuntil 循环是不成立进行循环#!/bin/bash i=5 until (( i < 1 )) do echo "$i" (( i-- )) done #until [ $i -lt 1 ] #do # echo "$i" # (( i-- )) #done示例:计算100以内的和#/bin/bash i=1 sum=0 while (( i <=100 )) do (( sum+=i )) (( i++ )) done [ "$sum" -ne 0 ] && printf "$sum\n" #while [ $i -le 100 ] #do # (( sum+=i )) # (( i++ )) #done #[ "$sum" -ne 0 ] && printf "$sum\n"示例:检测网站状态#!/bin/bash read -p "请输入一个网址:" web #判断是否输入网址 [ -z "$web" ] && echo "输入错误" && exit #if [ $# -ne 1 ]; then # echo "请输入一个网址" # exit 1 #fi while true do if [ `curl -o /dev/null --connect-timeout 5 -s -w "%{http_code}" $web|egrep -w "200|301|302"|wc -l` -ne 1 ] 对传入的url进行状态过滤 如果不等于1则表示状态不对 then echo "$web is error" else echo "$web is ok" fi sleep 3 done
2021年06月26日
355 阅读
3 评论
0 点赞
2021-06-21
Shell case条件语句的应用实践
语法格式case "变量" in 值1) 指令1 ;; 值2) 指令2 ;; *) 指令3 ;; esac示例#!/bin/bash read -p "请输入[1-3]数字:" a case "$a" in 1) echo "1" ;; 2) echo "2" ;; 3) echo "3" ;; *) echo "请输入[1-3]" esac示例2#!/bin/bash [ $# -ne 1 ] && echo "Uasge: $0 {start|stop|restart|relad|status}" && exit function start(){ ps aux | grep [n]ginx | grep -q master if [ $? -eq 0 ]; then echo "nginx启动成功" else /usr/local/nginx/sbin/nginx &>/dev/null if [ $? -eq 0 ]; then echo "nginx启动成功" else echo "nginx启动失败" fi fi } function stop(){ ps aux | grep [n]ginx | grep -q master if [ $? -eq 0 ]; then /usr/local/nginx/sbin/nginx -s stop >/dev/null sleep 2 ps aux | grep [n]ginx | grep -q master if [ $? -ne 0 ]; then echo "niginx关闭成功" else echo "nginx关闭失败" fi echo "nginx关闭成功" fi } case "$1" in start) start ;; stop) stop ;; restart) # ps aux | grep [n]ginx | grep -q master # if [ $? -eq 0 ];then # /usr/local/nginx/sbin/nginx -s stop &>/dev/null # sleep 2 # /usr/local/nginx/sbin/mginx &>/dev/null # if [ $? -eq 0 ]; then # echo "nginx 重启成功" # else # echo "nginx重启失败" # fi # else # /usr/local/nginx/sbin/nginx &>/dev/null # if [ $? -eq 0 ];then # echo "nginx重启成功" # else # echo "nginx重启失败" # fi # fi stop start ;; reload) stop start ;; status) ps aux | grep [n]ginx | grep -q master if [ $? -eq 0 ]; then echo "nginx is up" else echo "nginx is down" fi ;; *) echo "Uasge: $0 {start|stop|restart|relad|status}" exit esac
2021年06月21日
227 阅读
0 评论
0 点赞
2021-06-20
shell if语句
if 语句格式单分支结构#第一种 if <条件表达式> then 指令 fi #第二种 if <条件表达式>; then 指令 fi实例1#/bin/bash read -p "请输入两个整数:" a b if (( $a == $b )) then echo "两个数相等" fi双分支结构if <条件表达式> then 指令1 else 指令2 fi示例2#第一种写法 #!/bin/bash read -p "请输入两个整数:" a b if [ $# -ne 2 ] || [[ ! $a =~ [0-9]+$ ]] || [[ ! $a =~ [0-9]+$ ]]; then echo "输入错误,请输入两个整数!" fi if (( $a == $b )) then echo "$a=$b" else echo "$a!=$b" fi #第二种写法 #!/bin/bash read -p "请输入两个整数:" a b #判断输入是否为空 [ -z "$a" ] || [ -z "$b" ] && { echo "输入错误,不能为空值,请输入两个整数" exit 1 } #判断是否为整数 expr $a + 10 &>/dev/null RETAL1=$? expr $b + 10 &>/dev/null RETAL2=$? test $RETAL1 -eq 0 -a $RETAL2 -eq 0 || { echo "输入错误,请输入两个整数" exit 2 } if [ "$a" -eq "$b" ] then echo "$a=$b" else echo "$a!=$b" fi多分支结构if <条件表达式> then 指令1 elif <条件表达式> then 指令2 elif <条件表达式> then 指令3 else 指令4 fi示例3#!/bin/bash read -p "请输入成绩:" a [ -z "$a" ] && { echo "输入错误,数值为空" exit 1 } [[ ! "$a" =~ ^[0-9]+$ ]] && echo "输入错误,请输入数字" && exit 2 if [ "$a" -gt 90 ] then echo "优秀" elif [ "$a" -ge 80 -a "$a" -lt 90 ] then echo "良好" elif [ "$a" -ge 70 -a "$a" -lt 80 ] then echo "一般" elif [ "$a" -ge 60 -a "$a" -lt 70 ] then echo "及格" else echo "不及格" fi
2021年06月20日
375 阅读
0 评论
0 点赞
2021-04-04
Shell 脚本的条件测试与比较
{mtitle title="条件测试常用语法"/}Shell脚本条件测试语法说明test利用test命令进行条件测试[]通过[]进行条件测试[[]]通过[[]]进行条件测试(())通过(())进行条件测试test语法test -f file && echo true || echo false #存在输出true,不存在输出false[]语法[ -f /tmp/123.txt ] && echo 1 || echo 0 #存在输出1,不存在输出0[[]]语法[[ -f /tmp/123.txt ]] &&echo 1 || echo 0 #存在输出1,不存在输出0文件测试表达式{mtitle title="常用的文件测试符"/}文件测试操作符说明-d 文件文件存在且为目录为真,及测试表达式成立-f 文件文件存在且为普通文件为真,及测试表达式成立-e 文件文件存在即为真,则表达式成立,不区别文件和目录-r 文件文件存在且可读为真,及测试表达式成立-w 文件文件存在且可写为真,及测试表达式成立-x 文件文件存在且可执行为真,及测试表达式成立-s 文件文件存在且文件大小不为0为真,及测试表达式成立-L 文件文件存在且为链接文件为真,及测试表达式成立f1 -nt f2文件f1比文件f2新则为真,即表达式成立,根据文件的修改时间来计算f1 -ot f2文件f1比文件f2旧则为真,即表达式成立,根据文件的修改时间来计算字符串测试表达式{mtitle title="字符串测试符"/}字符串测试符说明-n "字符串"若字符串不为“0”,则为真,即表达式成立-z "字符串"若字符串为“0”,则为真,即表达式成立"串1" = "串2"若字符串1等于字符串2,则为真,即表达式成立"串1" != "串2"若字符串1不等于字符串2,则为真,即表达式成立整数二次元比较符在[]以及test中使用的比较符在(())和[[]]中使用的比较符说明-eq==或者=相等-ne!=不相等-gt>大于-ge>=大于等于-lt<小于-le<=小于等于root@cs:/server/scripts# a1=98;a2=64 root@cs:/server/scripts# [ "$a1" -eq "$a2" ] && echo 1 || echo 00 root@cs:/server/scripts# [ "$a1" -ne "$a2" ] && echo 1 || echo 01 root@cs:/server/scripts# [ "$a1" -gt "$a2" ] && echo 1 || echo 01 root@cs:/server/scripts# [ "$a1" -lt "$a2" ] && echo 1 || echo 00逻辑操作符{mtitle title="逻辑操作符"/}在[]和test中使用的操作符在[[]]和(())中使用的操作符说明-a&&and,与,两端为真,则结果为真-o双竖线or,或,两端一个为真,则结果为真!!not,非,两端相反,则结果为真使用-a和&&时当左边为真,右边为假时,结果为假。当左边为假,右边为真时,结果为假。当左边为真,右边为真时,结果为真。当左边为假,右边为假时,结果为假。root@cs:/server/scripts# [ 5 -eq 6 -a 7 -ne 5 ] && echo 1 || echo 0 #5等于7?假 7不等于5? 真 结果为假 输出0 0 root@cs:/server/scripts# [ 5 -lt 6 -a 7 -ne 5 ] && echo 1 || echo 0 #5<7?真 7不等于5? 真 结果为真 输出1 1使用-o 或 双竖线时当左边为真,右边为假时,结果为真。当左边为假,右边为真时,结果为真。当左边为真,右边为真时,结果为真。当左边为假,右边为假时,结果为假。root@cs:/server/scripts# [ 5 -lt 6 -o 7 -ne 5 ] && echo 1 || echo 0 1 root@cs:/server/scripts# [ 5 -eq 6 -o 7 -ne 5 ] && echo 1 || echo 0 1 root@cs:/server/scripts# [ 5 -gt 6 -o 7 -lt 5 ] && echo 1 || echo 0 0root@cs:/server/scripts# f1=/etc/rc.local;f2=/etc/services root@cs:/server/scripts# echo -ne "$f1 $f2\n" /etc/rc.local /etc/services root@cs:/server/scripts# [ -f "$f1" -a -f "$f2" ] && echo 1 || echo 0 1 root@cs:/server/scripts# [ -f "$f1" -a -f "$f222" ] && echo 1 || echo 0 0 root@cs:/server/scripts# [ -f "$f1" -o -f "$f222" ] && echo 1 || echo 0 1 root@cs:/server/scripts# [ -f "$f111" -o -f "$f222" ] && echo 1 || echo 0 0 root@cs:/server/scripts# [ -f "$f1" ] && [ -f "$f2" ] && echo 1 || echo 0 1 root@cs:/server/scripts# a="test";b="echo" root@cs:/server/scripts# echo -eq "$a $b\n" -eq test echo\n root@cs:/server/scripts# echo -ne "$a $b\n" test echo root@cs:/server/scripts# [[ ! -n "$a" && "$a" = "$b" ]] && echo 0 || echo 1 1 root@cs:/server/scripts# [[ -z "$a" && "$a" != "$b" ]] && echo 0 || echo 1 1 root@cs:/server/scripts# [[ -z "$a" || "$a" != "$b" ]] && echo 0 || echo 1 0 root@cs:/server/scripts# [[ -z "$a" -o "$a" != "$b" ]] && echo 0 || echo 1 -bash: syntax error in conditional expression -bash: syntax error near `-o'root@cs:/server/scripts# m=21;n=38 root@cs:/server/scripts# [ $m -gt 20 -a $n -lt 30 ] && echo 1 || echo 0 0 root@cs:/server/scripts# [ $m -gt 20 ] && [ $n -lt 30 ] && echo 1 || echo 0 0 root@cs:/server/scripts# [ $m -gt 20 ] || [ $n -lt 30 ] && echo 1 || echo 0 1逻辑操作符输入或通过命令行输入一个数字,如果传入的数字等于1,就打印1,输入2,就打印2,输入其他数字就退出程序root@cs:/server/scripts# cat ljczf.sh #!/bin/bash read -p "please input num:" num [ "$num" = "1" ] && { echo 1 exit 0 } [ "$num" = "2" ] && { echo 2 exit 0 } [ "$num" != "1" -a "$num" != "2" ] && { echo error exit 1 }传参方式 root@cs:/server/scripts# cat ljczf1.sh #!/bin/bash echo "plase input num" num=$1 [ "$num" = "1" ] && { echo 1 exit 0 } [ "$num" = "2" ] && { echo 2 exit 0 } [ "$num" != "1" -a "$num" != "2" ] && { echo error exit 1 }比较两个整数的大小root@cs:/server/scripts# cat int.sh #!/bin/bash read -p "pls input two num:" num1 num2 [ -z "$num1" -a -z "$num2" ] && { echo error exit 1 } expr $num1 + 1 &>/dev/null RETVAL_n1=$? expr $num2 + 1 &>/dev/null RETVAL_n2=$? [ $RETVAL_n1 -eq 0 -a $RETVAL_n2 -eq 0 ] || { echo "pls input two num again" exit 2 } [ $num1 -lt $num2 ] && { echo "$num1 < $num2" exit 0 } [ $num1 -eq $num2 ] && { echo "$num1 = $num2" exit 0 } [ $num1 -gt $num2 ] && { echo "$num1 > $num2" } root@cs:/server/scripts# sh int.sh pls input two num:6 3 6 > 3 root@cs:/server/scripts# sh int.sh pls input two num:3 6 3 < 6 root@cs:/server/scripts# sh int.sh pls input two num:6 6 6 = 6 root@cs:/server/scripts# sh int.sh pls input two num:dd ff pls input two num again root@cs:/server/scripts# cat int1.sh #!/bin/bash num1=$1 num2=$2 [ $# -ne 2 ] && { echo "USAGE:$0 num1 num2" exit 1 } expr $num1 + 1 &>/dev/null RETVAL_n1=$? expr $num2 + 1 &>/dev/null RETVAL_n2=$? [ $RETVAL_n1 -eq 0 -a $RETVAL_n2 -eq 0 ] || { echo "pls input two num again" exit 2 } [ $num1 -lt $num2 ] && { echo "$num1 < $num2" exit 0 } [ $num1 -eq $num2 ] && { echo "$num1 = $num2" exit 0 } [ $num1 -gt $num2 ] && { echo "$num1 > $num2" } root@cs:/server/scripts# sh int1.sh 6 3 6 > 3 root@cs:/server/scripts# sh int1.sh 3 6 3 < 6 root@cs:/server/scripts# sh int1.sh 3 3 3 = 3 root@cs:/server/scripts# sh int1.sh dd ff pls input two num again
2021年04月04日
339 阅读
0 评论
0 点赞
2021-04-03
Shell:变量数值计算(下)
bc 命令用法如果没有安装bc,用下面命令进行安装centos systemctl intall -y bcUnbunt apt-get install -y bcroot@cs:/server/scripts# echo 3+5|bc 8 root@cs:/server/scripts# echo 3-5|bc -2 root@cs:/server/scripts# echo 3.6-5.2|bc -1.6 root@cs:/server/scripts# echo 3.6*5.2|bc 18.7 root@cs:/server/scripts# echo "scale=2;355/113"|bc 3.14 root@cs:/server/scripts# echo "scale=6;355/113"|bc 3.141592计算1-10的结果root@cs:/server/scripts# echo `seq -s "+" 10`=`seq -s "+" 10|bc` 1+2+3+4+5+6+7+8+9+10=55 root@cs:/server/scripts# echo `seq -s "+" 10`=$((`seq -s "+" 10`)) 1+2+3+4+5+6+7+8+9+10=55 root@cs:/server/scripts# echo `seq -s '+' 10`=$(echo $[`seq -s "+" 10`]) 1+2+3+4+5+6+7+8+9+10=55 awk实现计算root@cs:/server/scripts# echo "5.3 6.9"|awk '{print ($1+$2)}' 12.2 root@cs:/server/scripts# echo "5.3 6.9"|awk '{print ($1-$2)}' -1.6 root@cs:/server/scripts# echo "5.3 6.9"|awk '{print ($1*$2)}' 36.57 root@cs:/server/scripts# echo "5.3 6.9"|awk '{print ($1/$2)}' 0.768116$[]实现运算root@cs:/server/scripts# i=8 root@cs:/server/scripts# i=$[i+6] root@cs:/server/scripts# echo $i 14 root@cs:/server/scripts# echo $[i-5] 9 root@cs:/server/scripts# echo $[i*5] 70 root@cs:/server/scripts# echo $[i**5] 537824 root@cs:/server/scripts# echo $[i/5] 2 root@cs:/server/scripts# echo $[i%5] 4基于Shell 变量输入read 命令的运算实践root@cs:/server/scripts# cat test3.sh #!/bin.bash read -p "please input two:" 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))" echo "a++:$((a++))" echo "a=$a" echo "++a:$((++a))" echo "a=$a" echo "b--:$((--b))" echo "b=$b" echo "--b:$((--b))" echo "b=$b"执行结果root@cs:/server/scripts# sh test3.sh please input two:2 2 a+b=4 a-b=0 a*b=4 a/b=1 a**b=4 a%b=0 a++:2 a=3 ++a:4 a=4 b--:1 b=1 --b:0 b=0完善的代码root@cs:/server/scripts# cat test3.sh #!/bin.bash read -t 15 -p "please input two:" a b [ ${#a} -le 0 ]&&{ echo "the first num is null" exit 1 } [ ${#b} -le 0 ]&&{ echo "the first num is null" exit 1 } expr $a + 1 &>/dev/null REVTAL_A=$? expr $b + 1 &>/dev/null REVTAL_B=$? if [ $REVTAL_A -ne 0 -o $REVTAL_B -ne 0 ];then echo "one of the num is not num,pls input again." exit 1 fi 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))" echo "a++:$((a++))" echo "a=$a" echo "++a:$((++a))" echo "a=$a" echo "b--:$((--b))" echo "b=$b" echo "--b:$((--b))" echo "b=$b"运算结果root@cs:/server/scripts# sh test3.sh please input two:12 12 a+b=24 a-b=0 a*b=144 a/b=1 a**b=8916100448256 a%b=0 a++:12 a=13 ++a:14 a=14 b--:11 b=11 --b:10 b=10用传参方式进行运算
2021年04月03日
310 阅读
0 评论
0 点赞
2021-04-03
Shell:变量数值计算(上)
算数运算符{mtitle title="Shell常见的算数运算符"/}算数运算符意义+、-加法(正号)、减法(负号)*、/、%乘、除、取余**幂运算++、--增加及减少!、&&、双竖线逻辑非(取反)、逻辑与、逻辑或<、<=、>、=>比较符号(小于、小于等于、大于、大于等于)==、!=、=比较符号(等于、不相等、对于字符串“=”也可以表示等于)<<、>>向左移,向右移~、单竖线、&、^按位取反、按位异或、按位与、按位或=、+=、-=、*=、/=、%=赋值运算符{mtitle title="Shell中常见的算数运算命令"/}运算符与运算命令意义(())用于整数运算的常用命令let用于整数运算,类似于(())expr可用于整数运算bcLinux中的一个计算器程序(适合整数和小数运算)$[]用于整数运算awkawk既可以整数运算,也可以小数运算declare定义变量值和属性,-i参数可以用于定义整形变量,做运算(())运算用法root@cs:/server/scripts# echo $((8+2)) 10 root@cs:/server/scripts# echo $((8-2)) 6 root@cs:/server/scripts# echo $((8*2)) 16 root@cs:/server/scripts# echo $((8/2)) 4 root@cs:/server/scripts# echo $((8%2)) 0 root@cs:/server/scripts# echo $((8**2)) 64 root@cs:/server/scripts# ((i=6)) root@cs:/server/scripts# ((i=i*2)) root@cs:/server/scripts# echo $i 12 root@cs:/server/scripts# a=$((100*(100+1)/2)) root@cs:/server/scripts# echo $a 5050 root@cs:/server/scripts# echo $((100*(100+1)/2)) 5050 root@cs:/server/scripts# echo $((3<8)) 1 #1表示为真 root@cs:/server/scripts# echo $((1>8)) 0 #0表示为假{message type="warning"}PS:上面的数字及变量必须为整数,不能用小数和字符串{/message}(())运算的Shell脚本示例root@cs:/server/scripts# cat test.sh #!/bin/bash a=6 b=12 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))" echo "a++:$((a++))" echo "a=$a" echo "++a:$((++a))" echo "a=$a" echo "b--:$((--b))" echo "b=$b" echo "--b:$((--b))" echo "b=$b"其执行结果root@cs:/server/scripts# sh test.sh a+b=18 a-b=-6 a*b=72 a/b=0 a**b=2176782336 a%b=6 a++:6 a=7 ++a:8 a=8 b--:11 b=11 --b:10 b=10通过命令行传参方式,实现混合运算root@cs:/server/scripts# cat test1.sh #!/bin/bash a=$1 b=$2 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))" echo "a++:$((a++))" echo "a=$a" echo "++a:$((++a))" echo "a=$a" echo "b--:$((--b))" echo "b=$b" echo "--b:$((--b))" echo "b=$b"其执行结果root@cs:/server/scripts# sh test1.sh 8 4 a+b=12 a-b=4 a*b=32 a/b=2 a**b=4096 a%b=0 a++:8 a=9 ++a:10 a=10 b--:3 b=3 --b:2 b=2实现输入两个数进行加减乘除的计算器利用read命令读入功能root@cs:/server/scripts# cat jsq.sh #!/bin/bash print_usage(){ print "please enter an integer\n" exit 1 } read -p "Please input first number:" a if [ -n "`echo $a|set 's/[0-9]//g'`" ];then print_usage fi read -p "Please input first operators:" b if [ "${b}" != "+" ]&&[ "${b}" != "-" ]&&[ "${b}" != "*" ]&&[ "${b}" != "/" ];then print_usage exit 2 fi read -p "Please input first number:" c if [ -n "`echo $c|set 's/[0-9]//g'`" ];then print_usage fi echo "${a}${b}${c}=$((${a}${b}${c}))" 其输入结果为 root@cs:/server/scripts# sh jsq.sh Please input first number:6 Please input first operators:+ Please input first number:4 6+4=10 root@cs:/server/scripts# sh jsq.sh Please input first number:8 Please input first operators:- Please input first number:4 8-4=4利用命令行传参方式root@cs:/server/scripts# sh jsq1.sh 8 + 5 8+5=13 root@cs:/server/scripts# sh jsq1.sh 8 - 5 8-5=3 root@cs:/server/scripts# sh jsq1.sh 8 \* 5 #*号要转义 8*5=40 root@cs:/server/scripts# sh jsq1.sh 8 / 5 8/5=1let 运算用法root@cs:/server/scripts# i=8 root@cs:/server/scripts# i=i+8 root@cs:/server/scripts# echo $i i+8 root@cs:/server/scripts# unset i root@cs:/server/scripts# i=8 root@cs:/server/scripts# let i=i+8 root@cs:/server/scripts# echo $i 16提示:let i=i+8等同于 ((i=i+8)),不过后者效率高一些{mtitle title="示例"/}root@cs:/server/scripts# cat 04_03_test.sh #!/bin/bash CheckUrl(){ #定义一个函数 timeout=5 #定义wget访问的超出时间 fails=0 #初始化访问网站失败的次数记录变量,若失败达到两次,报警 success=0 #初始化访问网站成功的次数记录变量,如果为1 则表示成功,退出 while true #持续循环检测 do wget --timeout=$timeout --tries=1 https://www.xiaobai666.top -q -O /dev/null #wget访问测试 if [ $? -ne 0 ] #如果上述wget不成功,即返回值不为零,执行if语句 then let fails=fails+1 #失败加1 else let success+=1 #成功加1 fi if [ $success -ge 1 ] #如果成功次数大于等于1 then echo success exit 0 fi if [ $fails -ge 2 ] #如果失败的次数大于等于2 then Critical="sys is down." echo "$Critical|tee|mail -s $Critical" #3285884651@qq.com exit 2 fi done } CheckUrl执行结果root@cs:/server/scripts# sh 04_03_test.sh success root@cs:/server/scripts# sh -x 04_03_test.sh + CheckUrl + timeout=5 + fails=0 + success=0 + true + wget --timeout=5 --tries=1 https://www.xiaobai666.top -q -O /dev/null + '[' 0 -ne 0 ']' + let success+=1 + '[' 1 -ge 1 ']' + echo success success + exit 0expr命令用法root@cs:/server/scripts# expr 2 + 2 4 root@cs:/server/scripts# expr 2 - 2 0 root@cs:/server/scripts# expr 2 \* 2 4 root@cs:/server/scripts# expr 2 / 2 1expr配合变量计算root@cs:/server/scripts# i=5 root@cs:/server/scripts# i=$(expr $i + 6) root@cs:/server/scripts# echo $i 11 root@cs:/server/scripts# i=`expr $i + 6` root@cs:/server/scripts# echo $i 17{message type="warning"}PS:expr $i +6=$(expr $i + 6),不能用小数和字符串{/message}通过expr判断变量或者数字是否为整数root@cs:/server/scripts# i=8 root@cs:/server/scripts# expr $i + 6 &>/dev/null root@cs:/server/scripts# echo $? 0 root@cs:/server/scripts# i=skfhs root@cs:/server/scripts# expr $i + 6 &>/dev/null root@cs:/server/scripts# echo $? 2也可以通过echo $? 判断上个命令是否运行成功通过read 读入持续等待例子cat expr.sh #!/bin/bash while true do read -p "Please input:" num expr $num + 0 >/dev/null 2>&1 [ $? -eq 0 ]&& echo int || echo chars done 执行结果 root@cs:/server/scripts# sh expr.sh Please input:123 int Please input:ersfs chars Please input:hf chars Please input:456456 int将前文的混合运算,判断传参的个数及通过expr判断传入的参数是否为整数root@cs:/server/scripts# cat test2-2.sh #!=/bin/bash print_usage(){ echo "please input two number:" exit 1 } [ $# -ne 2 ] && { echo $"USAGE $0 NUM1 NUM2" exit 1 } a=$1 b=$2 expr $a + 1 >/dev/null RETVAL_A=$? expr $b + 1 >/dev/null RETVAL_B=$? if [ $RETVAL_A -ne 0 -o $RETVAL_B -ne 0 ] then echo "Please input two num" exit 2 fi 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))" echo "a++:$((a++))" echo "a=$a" echo "++a:$((++a))" echo "a=$a" echo "b--:$((--b))" echo "b=$b" echo "--b:$((--b))" echo "b=$b"执行结果root@cs:/server/scripts# sh test2-2.sh 6 3 a+b=9 a-b=3 a*b=18 a/b=2 a**b=216 a%b=0 a++:6 a=7 ++a:8 a=8 b--:2 b=2 --b:1 b=1通过espr计算字符串的长度root@cs:/server/scripts# char="are you ok" root@cs:/server/scripts# expr length "$char" 10 root@cs:/server/scripts# echo ${#char} 10 root@cs:/server/scripts# echo ${char}|awk '{print length($0)}' 10 root@cs:/server/scripts# echo ${char}|wc -L 10找出字符数不大于5的单词root@cs:/server/scripts# cat test2-3.sh #!/bin/bash for i in dhf gsh df adfs asdjkja do if [ `expr length $i` -le 5 ] then echo $i fi done执行结果root@cs:/server/scripts# sh test2-3.sh dhf gsh df adfs
2021年04月03日
491 阅读
0 评论
0 点赞