腳本合集
-
通過(guò)鍵盤(pán)給定兩個(gè)數(shù)字 ,輸出其中較大的值
!/bin/bash if [ $1 -gt $2 ];then echo $1 else echo $2 fi
-
通過(guò)命令行參數(shù)給定一個(gè)用戶(hù)名丐吓,判斷其ID是偶數(shù)還是奇數(shù)
#!/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
-
通過(guò)命令行參數(shù)給定兩個(gè)文件文件名,如果某文件不存在术健,則結(jié)束腳本執(zhí)行粘衬;都存在時(shí)返回每個(gè)文件的行數(shù)色难,并說(shuō)明其中行數(shù)較多的文件
#!/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語(yǔ)句完成如下要求
求100以?xún)?nèi)所有偶數(shù)之和冒掌,100以?xún)?nèi)所有奇數(shù)之和
-
for語(yǔ)句
#!/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語(yǔ)句
#!/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語(yǔ)句
#!/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
創(chuàng)建10個(gè)用戶(hù)股毫,user101-user110,密碼同用戶(hù)名
for語(yǔ)句
#!/bin/bash
#
for i in `seq 101 110`;do
useradd user$i
echo "user$i" | passwd user$i --stdin 2&>/dev/null
done
while語(yǔ)句
#!/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語(yǔ)句
#!/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語(yǔ)句
#!/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語(yǔ)句
#!/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語(yǔ)句
#!/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 語(yǔ)句
#!/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語(yǔ)句
#!/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語(yǔ)句
#!/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以?xún)?nèi)所有偶數(shù)之和,以及所有奇數(shù)之各
#!/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
計(jì)算當(dāng)前系統(tǒng)上的所有用戶(hù)的id之和
#!/bin/bash for i in `cut -d: -f3 /etc/passwd`;do
sum=$[$sum+$i]
done
echo $sum
通過(guò)腳本參數(shù)傳遞一個(gè)目錄給腳本趣席,而后計(jì)算目錄下所有文本文件的行數(shù)之和宣肚,并說(shuō)明此文件的總數(shù)
#!/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的整數(shù)和
declare -i sum=0
declare -i i=1
while [ $i -le 100 ]; do
sum=$[$sum+$i]
let i++
done
echo $sum
until方式求100的整數(shù)和
declare -i sum=0
declare -i i=1
until [ $i -gt 100 ]; do
sum=$[$sum+$i]
let i++
done
echo $sum
每隔3秒鐘到系統(tǒng)上獲取已經(jīng)登錄用戶(hù)的用戶(hù)信息霉涨,如果logstash用戶(hù)登錄了惭适,則記錄于日志中
#!/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為偶數(shù)的用戶(hù)腥沽,顯示其用戶(hù)名鸠蚪、ID及默認(rèn)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
寫(xiě)一個(gè)服務(wù)框架腳本,定義變量為`$lockfile=/var/lock/subsys/script_name,要求此腳本可接受start盾舌、stop妖谴、restart、status四個(gè)參數(shù)之一膝舅,如果參數(shù)非此四個(gè)仍稀,則提示使用幫助信息后退出,如果是start則創(chuàng)建lockfile,并顯示啟動(dòng)遥巴,stop則刪除lockfile,并顯示停止享幽,restart先刪除此文件值桩,再創(chuàng)建此文件,而后顯示重啟完成斯入,如果是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
腳本服務(wù)框架使用函數(shù)寫(xiě):
#!/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
函數(shù)直接或間接調(diào)用自身(一般情況下不使用);
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
使用函數(shù)實(shí)現(xiàn)Ping一個(gè)主機(jī)帝美,來(lái)測(cè)試主機(jī)的在線狀態(tài)悼潭,主機(jī)地址傳遞給函數(shù)主程序:測(cè)試172.16.1.1-172.16.67.1范圍內(nèi)的各主機(jī)的在線狀態(tài)
#!/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乘法表,要使用函數(shù)來(lái)實(shí)現(xiàn)
#!/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
定義一個(gè)數(shù)組皆疹,數(shù)組中的元素是/var/log目錄下所有以.log結(jié)尾的文件略就,統(tǒng)計(jì)其下標(biāo)為偶數(shù)的文件中的行數(shù)之和
#!/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
注意:在腳本中使用數(shù)組時(shí)表牢,必須先使用declare -a | -A 定義其是一個(gè)數(shù)組初茶,如果不定義浊闪,他會(huì)將把有的值保存至一個(gè)下標(biāo)中。這個(gè)實(shí)例中去數(shù)組的賦值時(shí)折汞,不能對(duì)其/var/log/*.log加前后引號(hào)爽待,如果使用了,他會(huì)將把有的查詢(xún)到的內(nèi)容鸟款,全部放到一個(gè)下標(biāo)中何什。
寫(xiě)一個(gè)腳本等龙,完成如下功能:
1蛛砰、提示用戶(hù)輸入一個(gè)可執(zhí)行命令的名稱(chēng)
2、獲取此命令所依賴(lài)到的所有庫(kù)文件列表
3荠诬、復(fù)制命令至某目標(biāo)目錄(/mnt/sysroot)下的對(duì)應(yīng)的路徑中
4、復(fù)制此命令依賴(lài)到的所有庫(kù)文件至目標(biāo)目錄下的對(duì)應(yīng)路徑下
5方椎、每次復(fù)制完成一個(gè)命令后辩尊, 不要退出涛浙,而是提示用戶(hù)繼續(xù)輸入要復(fù)制的其它命令康辑,并重復(fù)完成上述所有功能,直接用戶(hù)輸入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
寫(xiě)一個(gè)腳本轿亮,實(shí)現(xiàn):能探測(cè)C類(lèi)疮薇、B類(lèi)或A類(lèi)網(wǎng)絡(luò)中的所有主機(jī)是否在線;
#!/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
}
寫(xiě)一個(gè)腳本我注,腳本可以接受外部發(fā)來(lái)的中斷信號(hào)按咒,然后清理其生產(chǎn)的臨時(shí)文件
#!/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[@]}
寫(xiě)一個(gè)腳本酸休,定義數(shù)組利凑,并調(diào)用
#!/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