主頁(yè) > 知識(shí)庫(kù) > 自己常用的一些shell腳本分享

自己常用的一些shell腳本分享

熱門(mén)標(biāo)簽:呼叫中心市場(chǎng)需求 AI電銷(xiāo) 鐵路電話系統(tǒng) 服務(wù)外包 百度競(jìng)價(jià)排名 Linux服務(wù)器 地方門(mén)戶(hù)網(wǎng)站 網(wǎng)站排名優(yōu)化

自己寫(xiě)了一下小的shell實(shí)例,雖然很小,但所有的大的程序都是由小的模塊堆積起來(lái)的,程序員一定要懂得一種腳本的書(shū)寫(xiě),而我,只會(huì)在linux下工作,所以就只能寫(xiě)linux的shell腳本了,呵呵,本文會(huì)陸續(xù)更新,給自己加油!

1.模擬linnux登錄shell

復(fù)制代碼 代碼如下:

#/bin/bash
echo -n "login:"
read name
echo -n "password:"
read passwd
if [ $name = "cht" -a $passwd = "abc" ];then
echo "the host and password is right!"
else echo "input is error!"
fi

2.比較兩個(gè)數(shù)大小
復(fù)制代碼 代碼如下:

#/bin/bash
echo "please enter two number"
read a
read b
if test $a -eq $b
then echo "NO.1 = NO.2"
elif test $a -gt $b
then echo "NO.1 > NO.2"
else echo "NO.1 NO.2"
fi

3.查找/root/目錄下是否存在該文件
復(fù)制代碼 代碼如下:

#/bin/bash
echo "enter a file name:"
read a
if test  -e /root/$a
then echo "the file is exist!"
else echo "the file is not exist!"
fi

4.for循環(huán)的使用
復(fù)制代碼 代碼如下:

#/bin/bash
clear
for num in 1 2 3 4 5 6 7 8 9 10
do
    echo "$num"
done

5.
復(fù)制代碼 代碼如下:

#/bin/bash
echo "Please enter a user:"
read a
b=$(whoami)
if test $a = $b
then echo "the user is running."
else echo "the user is not running."
fi

6.刪除當(dāng)前目錄下大小為0的文件
復(fù)制代碼 代碼如下:

#/bin/bash
for filename in `ls`
do
    if test -d $filename
    then b=0
    else   
       a=$(ls -l $filename | awk '{ print $5 }')
            if test $a -eq 0
             then rm $filename
             fi
        fi     
done

7.如果/export/um_lpp_source下有文件,那么將其文件系統(tǒng)大小改為3G
復(fù)制代碼 代碼如下:

#/bin/bash
while line=`ls /export/um_lpp_source`
do
        if test $line=""
        then  echo "NULL"
             sleep 1
    else echo $line
                chfs -a size=3G /export/um_lpp_source
                 exit 0
        fi
done

 
8.測(cè)試IP地址
復(fù)制代碼 代碼如下:

#/bin/bash
for i in  1 2 3 4 5 6 7 8 9
do
    echo "the number of $i computer is "
    ping -c 1 192.168.0.$i
done

9.如果test.log的大小大于0,那么將/opt目錄下的*.tar.gz文件
復(fù)制代碼 代碼如下:

#/bin/sh
a=2
while name="test.log"
do
        sleep 1
        b=$(ls -l $name | awk '{print $5}')
        if test $b -ge $a
        #then echo "OK"
    then `cp /opt/*.tar.gz .`
        exit 0
        fi
done

10.打印讀取的內(nèi)容,為下面的例子做準(zhǔn)備
復(fù)制代碼 代碼如下:

#/bin/bash
while read name
do
echo $name
done

11.從0.sh中讀取內(nèi)容并打印
復(fù)制代碼 代碼如下:

#/bin/bash
while read line
do
    echo $line
done 0.sh

12.讀取a.c中的內(nèi)容并做加1運(yùn)算
復(fù)制代碼 代碼如下:

#/bin/bash
test -e a.c
while read line
do
    a=$(($line+1))
done a.c
echo $a

13.普通無(wú)參數(shù)函數(shù)
復(fù)制代碼 代碼如下:

#/bin/bash
p ()
{
    echo "hello"
}
p

14.給函數(shù)傳遞參數(shù)
復(fù)制代碼 代碼如下:

#/bin/bash
p_num ()
{
    num=$1
    echo $num
}
for n in $@
do
    p_num $n
done

15.創(chuàng)建文件夾
復(fù)制代碼 代碼如下:

#/bin/bash
while :
do
    echo "please input file's name:"
    read a
    if test -e /root/$a
    then
         echo "the file is existing Please input new file name:"
    else
        mkdir $a
        echo "you aye sussesful!"
        break
    fi
done

16.獲取本機(jī)IP地址
復(fù)制代碼 代碼如下:

#/bin/bash
ifconfig | grep "inet addr:" | awk '{ print $2 }'| sed 's/addr://g'

17.查找最大文件
復(fù)制代碼 代碼如下:

#/bin/bash
a=0
for  name in *.*
do
     b=$(ls -l $name | awk '{print $5}')
    if test $b -ge $a
    then a=$b
         namemax=$name
     fi
done
echo "the max file is $namemax"

18.查找當(dāng)前網(wǎng)段內(nèi)IP用戶(hù),重定向到ip.txt文件中
復(fù)制代碼 代碼如下:

#/bin/bash
a=1
while :
do
    a=$(($a+1))
    if test $a -gt 255
    then break
    else
        echo $(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')
        ip=$(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')
        echo $ip >> ip.txt
    fi
done

19.打印當(dāng)前用戶(hù)
復(fù)制代碼 代碼如下:

#/bin/bash
echo "Current User is :"
echo $(ps | grep "$$" | awk '{print $2}')

20.case語(yǔ)句練習(xí)
復(fù)制代碼 代碼如下:

#!/bin/bash
clear
echo "enter a number from 1 to 5:"
read num
case $num in
    1) echo "you enter 1"
    ;;
    2) echo "you enter 2"
    ;;
    3) echo "you enter 3"
    ;;
    4) echo "you enter 4"
    ;;
    5) echo "you enter 5"
    ;;
    *) echo "error"
    ;;
esac

21.yes/no返回不同的結(jié)構(gòu)
復(fù)制代碼 代碼如下:

#!/bin/bash
clear
echo "enter [y/n]:"
read a
case $a in
    y|Y|Yes|YES) echo "you enter $a"
    ;;
    n|N|NO|no) echo "you enter $a"
    ;;
    *) echo "error"
    ;;
esac

22.內(nèi)置命令的使用

復(fù)制代碼 代碼如下:

#/bin/bash

    clear
        echo "Hello, $USER"
        echo
       
        echo "Today 's date id `date`"

        echo

        echo "the user is :"
        who
        echo

        echo "this is `uname -s`"
        echo

        echo "that's all folks! "

23.打印無(wú)密碼用戶(hù)

復(fù)制代碼 代碼如下:

#/bin/bash
echo "No Password User are :"
echo $(cat /etc/shadow | grep "!!" | awk 'BEGIN { FS=":" }{print $1}')

24.
復(fù)制代碼 代碼如下:

#/bin/bash

    clear
        echo "Hello, $USER"
        echo
       
        echo "Today 's date id `date`"

        echo

        echo "the user is :"
        who
        echo

        echo "this is `uname -s`"
        echo

        echo "that's all folks! "

25.檢查端口號(hào)是否已啟動(dòng)

復(fù)制代碼 代碼如下:

#!/bin/bash
n=1
echo "檢查xxx服務(wù)..."
while true
do
        if test $n -gt 20
        then
                echo "xxx服務(wù)啟動(dòng)失敗"
                break
        fi
               
        sleep 5
        n=$(($n+1))
        port=`netstat -antp | grep "0.0.0.0:8080"`
        if [ ${#port} -gt 3 ]; then
                echo "xxx服務(wù)已經(jīng)啟動(dòng)"
                break;
        fi
done

您可能感興趣的文章:
  • shell腳本語(yǔ)言的使用(超全超詳細(xì))
  • Linux Shell腳本系列教程(二):終端打印命令詳解
  • Linux shell傳遞參數(shù)實(shí)現(xiàn)原理及代碼實(shí)例
  • shell腳本4種執(zhí)行方式
  • linux反彈shell的原理詳解
  • 入門(mén)shell腳本基礎(chǔ)及原理

標(biāo)簽:蘭州 崇左 黃山 衡水 湘潭 湖南 仙桃 銅川

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《自己常用的一些shell腳本分享》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢(xún)

    • 400-1100-266