shell 指令碼基礎

2021-09-20 22:47:35 字數 3952 閱讀 9823

!/bin/bash

判斷172.40.51.0網段有多少是開關機狀態

x=0y=0

for i inseq 254

doping -c1 -i0.1 -w1 172.40.51.$i

if [ $? -eq 0 ];then

echo "172.40.51.$i is up"

let x++

else

echo "172.40.51.$i is down"

let y++

fi done

echo "開機的有$x個,關機的有$y個。"

1加到100的和

tmp=0

for i in  

dolet tmp=$i+$tmp

done

echo "總和:$tmp"

隨機猜獎

u=0num=$[ random%100+1 ]

while :

doread -p "[1-100]隨即猜:" ca

if [ $ca -eq $num ];then

echo 「恭喜中獎」

exit

elif [ $ca -gt $num ];then

echo 「猜大了」

else

echo 「猜小了」

filet u++

done

echo "你猜了$u次"

閹割版if

case "$1" in

-n)touch $2;;

-e)vim $2;;

-c)cat $2;;

-r)rm -rf $2;;

*)echo "usage:$0 (-n|-e|-c|-r) 檔名"

esac

加法計算機

tmp=0

while :

doread -p "請輸入乙個數[0結束]:" shu

let tmp+=$shu

[ $shu -eq 0 ] && break

done

echo "總和$tmp"

99乘法表

for i in

dofor j inseq $i

doecho -n " $i$j=$[ij]"

done

done

棋盤for i in

do for j in

dohe=$[i+j]

if [ $[$he%2] -eq 0 ];then

echo -en "\033[45m \033[0m" 

else

echo -en "\033[47m \033[0m"

fidone

done

使用者建立

read -p "請建立使用者:" user

echo "必須建立使用者"

if [ -z "$user" ];then

while :

doread -p "請建立使用者:" user

[ $? -ne 0 ] && exit

[ -n "$user" ]&& break

done

fistty -echo

read -p "請建立密碼:" pass

stty echo

pass=$

useradd "$user"

echo "$pass" |passwd --stdin "$user"

自動 遠端操作
rm /root/.ssh/known_host

i=172.25.0.10

expect << eof

set timeout 30

spawn ssh $i

expext yes

expect password

expect #

expect # 最後一條不執行

eofi=uptime |awk -f: ''

if [ $? -eq 0 ];then

echo "cpu 1-15分鐘的負載的值是:$i"

fi y=ifconfig eth0 |awk '/rx packets/'

if [ $? -eq 0 ];then

##echo "rx 接受的資料流量是:$y"

fi u=ifconfig eth0 |awk '/tx packets/'

if [ $? -eq 0 ];then

echo "tx 傳送的資料流量是$u"

fi p=free |awk '/mem:/'

if [ $? -eq 0 ];then

echo " 記憶體剩餘:"$p"k"

fi q=df|awk '/\//'

if [ $? -eq 0 ];then

echo "根分割槽的剩餘容量:"$q"k"

fi w=who |awk ''

if [ $? -eq 0 ];then

echo "已登陸的使用者:$w"

fi a=ps aux |wc -l

if [ $? -eq 0 ];then

echo "程序數量:$a"

fi s=rpm -qa |wc -l

if [ $? -eq 0 ];then

echo "已裝包數量:$s"

fi

原始碼安裝ngix
ls / |awk '/nginx.*.gz/'

if [ $? -eq 0 ];then

tar -xf nginx-1.8.0.tar.gz

else

echo "沒有nginx-1.8.0.tar.gz包"

exit

ficd nginx-1.8.0

yum clean>/dev/null

yum repolist

yum -y install gcc pcre-deve openssl-devel>/dev/null

xi()

xi &

if [ $? -eq 0 ];then

./configure >/dev/null

fimake>/dev/null

make install>/dev/null

cd ..

kill $!

i=ls /usr/local/nginx/

if [ $? -eq 0 ];then 

echo ok

else

echo 失敗 

finginx的啟動服務

case $1 in

start)

/usr/local/nginx/sbin/nginx;;

stop)

/usr/local/nginx/sbin/nginx -s stop;;

restart)

/usr/local/nginx/sbin/nginx -s stop

/usr/local/nginx/sbin/nginx;;

status)

netstat -ntulp |grep nginx;;

*)echo "請正確輸入";;

esac

拒絕訪問失敗的ip
awk '/failed/' /var/log/secure >> ip.txt

if [ -d ip.txt ];then

echo ip.txt沒有內容

else

u=cat ip.txt

for i in $u

doiptables -i input -s 172.25.0.$i -j drop 

donefi

shell指令碼基礎

執行shell指令碼有兩種方法 1 作為可執行程式 將上面的 儲存為 test.sh,並 cd 到相應目錄 chmod x test.sh 使指令碼具有執行許可權 test.sh 執行指令碼 注意,一定要寫成 test.sh,而不是 test.sh,執行其它二進位制的程式也一樣,直接寫 test.s...

shell指令碼基礎

shell定義 shell是命令解析器,將使用者的輸入的指令轉化為機器可以執行的程式。和c語言不同,指令碼有自己的語法。比較常用的格式是 bin bash或者 bin sh 如 這是乙個判斷輸入字元型別的程式 bin bash read key case in a z echo upperlette...

Shell指令碼基礎

1 shell是使用者與核心進行互動操作的一種介面,目前最流行的shell稱為bash shell 2 shell也是一門程式語言 解釋型的程式語言 即shell指令碼 3 乙個系統可以存在多個shell,可以通過cat etc shells命令檢視系統中安裝的shell,不同的shell可能支援的...