脚本合集
-
通过键盘给定两个数字 ,输出其中较大的值
!/bin/bash if [ $1 -gt $2 ];then echo $1 else echo $2 fi
-
通过命令行参数给定一个用户名,判断其ID是偶数还是奇数
#!/bin/bash id=$(id -u $1) if [ $[id%2] == "0" ];then echo "This is double number ID." else echo "This is not double number ID." fi
-
通过命令行参数给定两个文件文件名,如果某文件不存在,则结束脚本执行;都存在时返回每个文件的行数,并说明其中行数较多的文件
#!/bin/bash if [ $# -lt 2 ];then echo "Your argument least is two number." exit 3 fi wc_file1=$(cat $1 | wc -l) wc_file2=$(cat $2 | wc -l) if [ -e $1 -a -e $2 ];then if [ "$wc_file1" -gt "$wc_file2" ];then echo "$1 line number is bigger" else echo "$2 line bumber is bigger" fi else echo "Your have a file is not in path." exit 3 fi
-
分别使用for 、while 、until语句完成如下要求
求100以内所有偶数之和,100以内所有奇数之和
-
for语句
#!/bin/bash for i in `seq 1 100`;do if [ $[i%2] = 0 ];then let sum+=$i else [ $[i%2] = 1 ] let sum1+=$i fi done echo $sum echo $sum1
-
while语句
#!/bin/bash # i=0 while [ $i -ge 0 ] && [ $i -le 100 ];do if [ $[i%2] = 0 ];then let sum+=$i elif [ $[i%2] = 1 ];then let sum1+=$i fi let i++ done echo $sum echo $sum1
-
until语句
#!/bin/bash # i=0 until [ $i -gt 100 ];do if [ $[i%2] = 0 ];then let sum+=$i else let sum1+=$i fi let i++ done echo $sum echo $sum1
创建10个用户,user101-user110,密码同用户名
for语句
#!/bin/bash
#
for i in `seq 101 110`;do
useradd user$i
echo "user$i" | passwd user$i --stdin 2&>/dev/null
done
while语句
#!/bin/bash
#
userid=101
while [ $userid -le 110 ];do
useradd user$userid
echo "user$userid" | passwd user$userid --stdin 2&>/dev/null
let userid++
done
until语句
#!/bin/bash
#
id=101
until [ $id -gt 110 ];do
useradd user$id
echo "user$id" | passwd $user$id --stdin 2&>/dev/null
let id++
done
打印九九乘法表
for语句
#!/bin/bash
#
for i in `seq 1 9`;do
for j in `seq 1 $i`;do
echo -n -e "${j}X${i}=$[$j*$i]\t"
done
echo
done
while语句
#!/bin/bash
#
i=1
while [ $i -le 9 ];do
j=1
while [ $j -le $i ];do
echo -n -e "${j}X${i}=$[${j}*${i}]\t"
let j++
done
echo
let i++
done
until语句
#!/bin/bash
#
i=1
until [ $i -gt 9 ];do
j=1
until [ $j -gt $i ];do
echo -n -e "${j}X${i}=$[${j}*${i}]\t"
let j++
done
echo
let i++
done
逆序打印九九乘法表
for 语句
#!/bin/bash
#
for ((i=9;i>=1;i--));do
for (( j=i;j<=9;j++));do
echo -n -e "${i}X${j}=$[$i*$j]\t"
done
echo
done
while语句
#!/bin/bash
i=9
while [ $i -ge 1 ];do
j=$i
while [ $j -le 9 ];do
echo -n -e "${i}X${j}=$[${i}*${j}]\t"
let j++
done
echo
let i--
done
until语句
#!/bin/bash
#
i=9
until [ $i -lt 1 ];do
j=$i
until [ $j -gt 9 ];do
echo -n -e "${i}X${j}=$[${i}*${j}]\t"
let j++
done
echo
let i--
done
分别求100以内所有偶数之和,以及所有奇数之各
#!/bin/bash #
for i in `seq 1 2 100`;do
sum=$[$sum+$i]
done
for i in `seq 2 2 100`;do
sum2=$[$sum2+$i]
done
echo $sum
echo $sum
计算当前系统上的所有用户的id之和
#!/bin/bash for i in `cut -d: -f3 /etc/passwd`;do
sum=$[$sum+$i]
done
echo $sum
通过脚本参数传递一个目录给脚本,而后计算目录下所有文本文件的行数之和,并说明此文件的总数
#!/bin/bash read -p "Please enter your directory:" file
[ -z $file ] && echo “error" && exit 3
for i in `find $file -type f`;do
wc=`cat $i | wc -l`
for i in $wc;do
sum=$[$sum+$i]
done
done
echo $sum
while方式求100的整数和
declare -i sum=0
declare -i i=1
while [ $i -le 100 ]; do
sum=$[$sum+$i]
let i++
done
echo $sum
until方式求100的整数和
declare -i sum=0
declare -i i=1
until [ $i -gt 100 ]; do
sum=$[$sum+$i]
let i++
done
echo $sum
每隔3秒钟到系统上获取已经登录用户的用户信息,如果logstash用户登录了,则记录于日志中
#!/bin/bash
# while true;do
if who | grep "logstash" &>/dev/null;then
echo "logstsh user login system finished." > /tmp/userlog.txt
break
fi
sleep 3
done
找出ID为偶数的用户,显示其用户名、ID及默认shell
#!/bin/bash
#
while read line;do
userid=$(echo $line | cut -d: -f3)
username=$(echo $line | cut -d: -f1)
usershell=$(echo $line | cut -d: -f7)
if [ $[$userid%2] -eq 0 ];then
echo “$username ,$userid,$usershel.”
fi
done < /etc/passwd
写一个服务框架脚本,定义变量为`$lockfile=/var/lock/subsys/script_name,要求此脚本可接受start、stop、restart、status四个参数之一,如果参数非此四个,则提示使用帮助信息后退出,如果是start则创建lockfile,并显示启动,stop则删除lockfile,并显示停止,restart先删除此文件,再创建此文件,而后显示重启完成,如果是status,如果lockfile存在,则显示running,否则显示为stop
#!/bin/bash
#
lockfile=/var/lock/subsys/$(basename $0)
case $1 in
start)
touch $lockfile
echo "$localfile create sucess.!"
;;
stop)
rm -f $lockfile
echo "$lockfile is removed."
;;
restart)
if [ -e $lockfile ];then
rm -f $lockfile
else
touch $lockfile
fi
echo "$lockfile restart scuessed."
;;
status)
if [ -e $lockfile ];then
echo "$lockfile is running."
else
echo "$lockfile is stop."
fi
;;
*)
echo "Usage : {start|stop|status|restart},pleae try agin."
exit 8
esac
脚本服务框架使用函数写:
#!/bin/bash
#
# chkconfig: - 50 50
# description: test service script
#
prog=$(basename $0)
lockfile=/var/lock/subsys/$prog
start() {
if [ -f $lockfile ]; then
echo "$prog is running yet."
else
touch $lockfile
[ $? -eq 0 ] && echo "start $prog finshed."
fi
}
stop() {
if [ -f $lockfile ]; then
rm -f $lockfile
[ $? -eq 0 ] && echo "stop $prog finished."
else
echo "$prog is not running."
fi
}
status() {
if [ -f $lockfile ]; then
echo "$prog is running"
else
echo "$prog is stopped."
fi
}
usage() {
echo "Usage: $prog {start|stop|restart|status}"
}
case $1 in
start)
start ;;
stop)
stop ;;
restart)
stop
start ;;
status)
status ;;
*)
usage
exit 1 ;;
esac
函数直接或间接调用自身(一般情况下不使用);
10!=10*9!=10*9*8!=10*9*8*7!=...
n
n*(n-1)!=n*(n-1)*(n-2)!=
#!/bin/bash
#
fact() {
if [ $1 -eq 0 -o $1 -eq 1 ]; then
echo 1
else
echo $[$1*$(fact $[$1-1])]
fi
}
fact $1
1,1,2,3,5,8,13,21,...
#!/bin/bash
#
fab() {
if [ $1 -eq 1 ]; then
echo -n "1 "
elif [ $1 -eq 2 ]; then
echo -n "1 "
else
echo -n "$[$(fab $[$1-1])+$(fab $[$1-2])] "
fi
}
for i in $(seq 1 $1); do
fab $i
done
echo
使用函数实现Ping一个主机,来测试主机的在线状态,主机地址传递给函数主程序:测试172.16.1.1-172.16.67.1范围内的各主机的在线状态
#!/bin/bash
#
host_ping() {
ping -w 1 -c 1 $1 &> /dev/null
if [ $? -eq 0 ];then
return 0
else
return 5
fi
}
for ((i=1;i<=1;i++));do
for ((j=1;j<=10;j++));do
host_ip=172.16.$i.$j
if [ $i -eq 1 ] && [ $j -eq 8 ];then
break
else
host_ping $host_ip
if [ $? -eq 0 ];then
echo "$host_ip is up."
let up++
else [ $? -eq 5 ]
echo "$host_ip is down"
let down++
fi
fi
done
done
echo " The host online Count is $up."
echo "The host not online Count is $down."
打印NN乘法表,要使用函数来实现
#!/bin/bash
NN() {
for ((i=1;i<=$1;i++));do
for ((j=1;j<=$i;j++));do
echo -n -e "${j}X${i}=$[${j}*${i}] "
done
echo
done
}
NN $1
定义一个数组,数组中的元素是/var/log目录下所有以.log结尾的文件,统计其下标为偶数的文件中的行数之和
#!/bin/bash
#
declare -a files
declare -i line
files=(/var/log/*.log)
for i in $(seq 0 $[${#files[*]}-1] ); do
lines=$(cat ${files[$i]} | wc -l)
if [ $[$i%2] -eq 0 ];then
let line+=$lines
fi
done
echo $line
注意:在脚本中使用数组时,必须先使用declare -a | -A 定义其是一个数组,如果不定义,他会将把有的值保存至一个下标中。这个实例中去数组的赋值时,不能对其/var/log/*.log加前后引号,如果使用了,他会将把有的查询到的内容,全部放到一个下标中。
写一个脚本,完成如下功能:
1、提示用户输入一个可执行命令的名称
2、获取此命令所依赖到的所有库文件列表
3、复制命令至某目标目录(/mnt/sysroot)下的对应的路径中
4、复制此命令依赖到的所有库文件至目标目录下的对应路径下
5、每次复制完成一个命令后, 不要退出,而是提示用户继续输入要复制的其它命令,并重复完成上述所有功能,直接用户输入quit退出脚本
#!/bin/bash
#
mkpath() {
[ ! -e /mnt${1%/*} ] && mkdir -p /mnt${1%/*} &> /dev/null
}
cpfile() {
cp $1 /mnt${1%/*}/
}
until [[ $com == "quit" ]];do
read -p "Pleas enter you command: " com
while ! which $com &> /dev/null;do
read -p "Command is Error!!!,Pleas try agein :" com
done
compath=$(which $com | grep -Eo "/[^ ]+")
mkpath $compath
cpfile $compath
for i in $(ldd $compath | grep -Eo "/[^ ]+");do
mkpath $i
cpfile $i
done
done
写一个脚本,实现:能探测C类、B类或A类网络中的所有主机是否在线;
#!/bin/bash
#
cping() {
local i=1
while [ $i -le 5 ]; do
if ping -W 1 -c 1 $1.$i &> /dev/null; then
echo "$1.$i is up"
else
echo "$1.$i is down."
fi
let i++
done
}
bping() {
local j=0
while [ $j -le 5 ]; do
cping $1.$j
let j++
done
}
aping() {
local x=0
while [ $x -le 255 ]; do
bping $1.$x
let x++
done
}
写一个脚本,脚本可以接受外部发来的中断信号,然后清理其生产的临时文件
#!/bin/bash
#
declare -a hosttmpfiles
trap 'mytrap' INT
mytrap() {
echo "Quit"
rm -f ${hosttmpfiles[@]}
exit 1
}
for i in {1..50}; do
tmpfile=$(mktemp /tmp/ping.XXXXXX)
hosttmpfiles[${#hosttmpfiles[*]}]=$tmpfile
if ping -W 1 -c 1 172.16.$i.1 &> /dev/null; then
echo "172.16.$i.1 is up" | tee $tmpfile
else
echo "172.16.$i.1 is down" | tee $tmpfile
fi
done
rm -f ${hosttmpfiles[@]}
写一个脚本,定义数组,并调用
#!/bin/bash
# test.sh
orig=($(cat file.sh))
for i in $(seq 0 $[${#orig[@]}-1]);do
if [[ ${orig[$i]} == "saturn" ]];then
echo "${orig[$i]}"
fi
done