書寫一些簡單的shell指令碼

2021-10-04 06:12:55 字數 2985 閱讀 5622

1、檢查定時任務是否開啟

[root@shell 2.5]# cat sb01.sh 

#!/bin/bash

. /etc/init.d/functions

service=`ps -ef | grep crond | wc -l`

if [ $service -gt 1 ];then

echo `action "crond is running" /bin/true`

else

echo `action "crond is failed" /bin/false`

fi

2、檢查記憶體是否充足

[root@shell 2.5]# cat sb02.sh 

#!/bin/env bash

n=`free | awk -f ' ' 'nr==2 '`

if [ $n -gt 102400 ];then

echo "記憶體充足"

exit

else

echo "記憶體不足"

exit

fi

3、檢查web**url是否正常執行

[root@shell 2.5]# cat sb03.sh 

#!/bin/bash

source /etc/init.d/functions

read -p '請輸入測試網域名稱:' str1

error ()

error $str1

if ! ping -w3 -c1 $str1 &>/dev/null;then

echo "該**不存在或**格式書寫錯誤"

exit

fiwget --tries=1 -t 5 $strl &>/dev/null

if [ $? -eq 0 ];then

echo "正在連線$str1...."

sleep 1

action "$1連線正常" /bin/true

else

echo "正在連線$str1...."

sleep 1

action "$str1無法正常通訊" /bin/false

fi

4、rsync服務管理指令碼

[root@shell 2.5]# cat sb04.sh 

#!/bin/bash

source /etc/init.d/functions

pid_num=/var/run/rsyncd.pid

choice=$1

start()

stop()

restart()

case "$choice" in

start)

start

;;stop)

stop

;;restart)

restart

;;*) echo -e "\033[31musages: $0 \033[0m"

exit 1

esac

5、生成數列指令碼

[root@shell 2.5]# cat sb05.sh 

#!/bin/bash

source /etc/init.d/functions

num=1

while [ $num -le 10 ]

do echo $num

let num++

done

6、一到一百累加運算

[root@shell 2.5]# cat sb06.sh 

#!/bin/bash

source /etc/init.d/functions

num=1

sum=0

while [ $num -le 100 ]

do #echo $num

((sum=num+sum))

(( num++ ))

echo $sum 依次累加結果

done

echo $sum 最終結果

7、隨機生成十個檔案(因為生成名字會出現重複字母很有可能生成不夠)

[root@shell 2.5]# cat lj01.sh 

#!/bin/bash

[ -d /test ] || mkdir -p /test

a=`date +%n | md5sum | head -c10 | xargs echo | sed -nr "s#(.)#\1 #gp"`

b=$a

eval echo $b

for i in $b

do touch /test/$i.html

done

8、隨機生成十個檔案

[root@shell 2.5]# cat lj02.sh 

#!/usr/bin/env bash

o=/good

[ -d $o ] || mkdir -p /good

for i in

do test=`date +%n | md5sum | head -c10 | xargs echo`

touch $o/$test\_.html

done

9、惡搞指令碼,別噴我

[root@shell 2.5]# cat good3.sh 

#!/bin/bash

trap "" hup int tstp

while true

doread -p "你說誰是豬:" goodnb

case $goodnb in

"我是豬")

echo "對你的確是豬"

sleep 100

exit

;;*)

echo "呸,你居然說自己不是豬"

esac

done

簡單的一些shell指令碼學習 Unix命令列程式

學習 whoami 用途 顯示 登入名。解釋 與使用命令 who 並指定 am i 不同,whoami 命令在有 root 使用者許可權時也有效,因為它不檢查 etc utmp 檔案。date 用途 顯示和設定系統日期和時間。格式 date 選項 顯示時間格式 以 開頭,後面接格式 date 設定時...

shell指令碼的一些程式設計

shell的一般結構主要是函式和主過程。shell指令碼的形式是函式放在最前面,後面是命令語句。讀取的時候是從上往下,跳過函式先執行命令語句,需要用到函式時,直接呼叫。變數分為區域性變數,環境變數 內部變數。區域性是在這個shell裡面變數,在另外乙個shell裡面不可用。環境是所有得終端都可用的。...

shell指令碼的一些基礎操作

vim test.sh 建立乙個 sh指令碼 直譯器 編輯這個指令碼 bin bash 是使用bash 直譯器的意思 bin sh 是使用sh直譯器的意思 a 10 echo a bin bash test.sh 執行 test.sh 執行 sh test.sh 執行 echo 測試上乙個命令是否成...