shell编程程序实例
1、计算器
#!/bin/bash
#test
#by authors hdc 2018
function add(){ #实现加法
c=$[ $a + $b ] #相加
echo $c
}
function sub(){ #实现减法
c=$[$a - $b]
echo $c
}
function mul(){ #实现乘法
c=$[ $a * $b ]
echo $c
}
function devide(){ 实现除法
if [ $b -eq 0 ];then #判断分母是否为0
echo "分母不能为0"
read -p "请重新输入第二个操作数:" b
fi
c=$[ $a / $b ]
echo $c
}
i="ddf"
while [[ -n "$i" ]] #循环条件,即判断是否为空字符串,不是返回真
do
read -p "请输入要执行打操作:" i #人机交互,输入
if [[ -z "$i" ]];then #循环条件,即判断是否为空字符串,是返回真
echo "结束运行"
exit
fi
read -p "Please input the number1:" a
read -p "Please input the number2:" b
case $i in
add )
add
;;
sub )
sub
;;
mul )
mul
;;
devide )
devide
;;
* )
echo "输入错误,请重新输入!"
;;
esac
done
2、auto_scp.sh
#!/bin/bash
#auto scp files for client
#by authors hdc 2018
if [ ! -f ip.txt ];then
echo -e "\033[31m please create ip.txt Files\033[0m"
cat <<EOF
192.168.112.130
192.168.112.131
EOF
exit
fi
if
[ -z "$1" ];then
echo "\033[31m $0 command ,example{src_files|src_dir des_dir}\033[0m"
exit
fi
count=`cat /home/hdc/user/ip.txt | wc -l`
rm -rf ip.txt.swp
i=0
while ((i<$count))
do
i=`expr $i + 1`
sed "${i}s/^/&${i} /g" ip.txt >>ip.txt.swp
IP=`awk -v I="$i" '{if(I==$1)print $2}' ip.txt.swp`
scp -r $1 root@${IP}:$2
done
3、auto_deny_ip.sh
#!/bin/sh
#auto drop ssh failed IP address
#hdc 2018
SEC_FILE=/var/log/secure
IP_ADDR=`tail -n 1000 /var/log/secure | grep "Failed password"| egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}" | sort -nr |uniq -c | awk '$1>=4 {print $2}'`
IPTABLE_CONF=/etc/sysconfig/iptables
for i in echo $IP_ADDR
do
cat $IPTABLE_CONF | grep $i >/dev/null
if
[ $? -ne 0 ];then
sed -i "/lo/a -A INPUT -s $i -m --state NEW -m tcp -p tcp --dport 22 -j DROP" $IPTABLE_CONF
else
echo "已经存在"
fi
done
`service iptables restart`
shell编程程序实例的更多相关文章
- Shell 编程(实例二)
创建一个脚本,为指定硬盘创建分区 1.列出当前系统所有磁盘,让用户选择,如果选择quit则退出脚本:如果选择错误,则重新选择 2.当用户选择完成后,询问是否继续 3.抹除选择磁盘上的分区,为其创建三个 ...
- Shell 编程(实例一)
创建一个脚本 admin.sh 完成如下作用: -h | --help : 提供帮助信息 --add Users : 完成用户添加 -del User | --delete : 完成用户删除 -v ...
- Shell编程实例
一.简介 从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更 ...
- Linux shell编程02 shell程序的执行 及文件权限
第一个shell脚本 1. shell编程的方式 交互式shell编程 非交互式shell编程:执行的语句存放到一个文件 shell脚本:可以任意文件名,建议扩展名为sh 2. ...
- [ SHELL编程 ] shell编程中数值计算方法实例
SHELL编程中经常会涉及到数值计算,有时候对于这些计算命令使用场景容易忘记或者混淆,这里针对常用的命令做个总结.主要包括let.bc.expr.(())等. 1.let 使用格式:let 表达式,表 ...
- [ SHELL编程 ] echo和printf使用实例
本文主要描述Linux系统中echo和printf命令的使用方法,包括命令参数的含义.使用技巧. 1.echo 了解一个命令我们首先要知道它能做什么,它有哪些参数,参数的含义,可以实现我们哪方面 ...
- Shell程序实例集锦一
2007-12-13 07:51:40 标签:实例 程序 Shell 休闲 职场 Shell程序实例集锦一 前言:下面这些hell实例都是自己写的或者用过的一些Shell小程序.整理整理. ...
- Shell编程菜鸟基础入门笔记
Shell编程基础入门 1.shell格式:例 shell脚本开发习惯 1.指定解释器 #!/bin/bash 2.脚本开头加版权等信息如:#DATE:时间,#author(作者)#mail: ...
- shell编程基础练习
shell作为一个人机交互的接口,在Linux中发挥着很大的作用,而shell编程则是减轻系统工程师工作的利器,这里总结一下shell编程的主要内容(趁着程序运行的空档). 本文的基本结构是shell ...
随机推荐
- Python_009(函数,命名空间)
一.函数 1.函数格式: #定义函数: def function(): 函数体 return 返回值 #调用函数: function() ret = function() 这是得到返回值. 这里是用关 ...
- selenium 浏览器无界面模式运行
以Chrome浏览器为例: 方法一: from selenium.webdriver import Chrome, ChromeOptions opt = ChromeOptions() # 创建Ch ...
- Period POJ - 1961
Period POJ - 1961 时限: 3000MS 内存: 30000KB 64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 For each ...
- Linux shell 归纳之 ~/. 是什么意思
假设用户名目录是:/home/test ~> cat ~/.profile ~ 是代表用户名目录/home/test/ .是代表隐藏文件, profile 就是home/test目录下的隐藏文件
- 给网页标题添加icon小图标
so easy: 给网页标题添加icon小图标: 直接在html文件的head部分添加下面代码(注意href的路径): <link rel="icon" type=" ...
- Javascript中this、prototype、constructor的理解(转载)
http://www.cnblogs.com/phpmix/articles/1734031.html
- 不间断电源(UPS)
UPS电源一般指不间断电源 UPS(Uninterruptible Power System/Uninterruptible Power Supply),即不间断电源,是将蓄电池(多为铅酸免维护蓄电池 ...
- Not a Number (NaN)
NaN can be produced by: 1. 0/0 2. Inf - Inf 3. Inf/Inf 4. 0*Inf 5. rem(x,y), where y=0 or x=Inf
- Learn Python the hard way, ex39 列表的操作
#!/usr/bin/python #coding:utf-8 ten_things = "apples oranges crows telephone light sugar" ...
- 应用安全-Web安全-越权漏洞整理
login->register GetPhone->GetPasswd GetPwd->GetPassword 遍历https://xx.com/contacts/new?user_ ...