bash編程筆記


1、腳本該如何些
 hello.sh
 命名,執行權限,如何執行
2、在寫bash腳本時,如果傳參數
[root@mail bash]# cat  hello.sh
#!/bin/bash
# first bash shell script
echo "Hello World!"
echo $$
echo "\$0 是什么???是指腳本名稱" $0
echo "\$1 這是腳本的第1個位置參數:"$1
echo "\$2 這是腳本的第2個位置參數:"$2
echo "\$3 這是腳本的第3個位置參數:"$3
echo "\$4 這是腳本的第4個位置參數:"$4
echo "\$5 這是腳本的第5個位置參數:"$5
echo "\$6 這是腳本的第6個位置參數:"$6
echo "\$# 是指當前腳本的位置參數個數"  $#

3、學習條件判斷語句if
if 條件
then
    條件成立要執行的命令
else
    條件不成立要執行的命令
fi
*******if關鍵字后面跟的是“條件“  , “條件“ 實際是一個命令,if會判斷命令的返回值,返回為0,則條件成立,返回值為非零,則條件不成立


4、腳本執行的返回值,由腳本執行過程中,最后一條執行的命令的返回值來決定

[root@mail bash]# cat  checkuser.sh
#!/bin/bash
# test user
if id $1 &> /dev/null
then
        echo "user exist"
else
        echo "no this user"
fi

5、腳本異常處理功能
[root@mail bash]# cat checkuser.sh
#!/bin/bash
# test user

if test $# -ne 1
then
        echo "ERROR!"
        echo "Syntax: $0 <username>"
        exit 3
fi
if id $1 &> /dev/null
then
        echo "user exist"
else
        echo "no this user"
        exit 2
fi

6、總結if語句語法
if condition
then
    command
fi
--------
if condition
then
    command
else
    command
fi
-----------
if condition1
then
    command
elif condition2
then
    command
elif conndition3
then
    command
else
    command
fi
------------------

7、if語句的練習
1)
[root@mail bash]# cat checkfiletype.sh
#!/bin/bash

if ! [ -e $1 ]
then
        echo $1 is not exist
elif [ -d $1 ]
then
        echo "$1 is directory"
elif [ -L $1 ]
then
        echo "$1 is a symbolic link"
elif [ -b $1 ]
then
        echo "$1 is block special"
elif test -c $1
then
        echo "$1 is character special"
elif test -p $1
then
        echo "$1 is a named pipe"
elif test -S $1
then
        echo "$1 is socket file"
elif test -f $1
then
        echo "$1 is regular file"
else
        echo "unknown"
fi
----
2)
[root@mail bash]# cat authuser.sh
#!/bin/bash


if test $# -ne 2
then
        echo "Syntax: $0 <username> <password>"
        exit 2
fi

#if [ "$1" = "root" ] && [ "$2" = "123" ]
if [ "$1" = "root" -a "$2" = "123" ]
then
        echo "root user auth successed"
elif [ "$1" = "pg" ] && [ "$2" = "123" ]
then
        echo "pg user auth successed"
else
        echo "unknow user"
fi

3)
[root@mail bash]# cat testping.sh
#!/bin/bash

if ping -c 1 -W 1 $1 &> /dev/null
then
        echo "link ok"
else
        echo "no link"
fi



8、case語句,專用於對字符串做判斷
case  變量名  in
        value1)
            command1
        ;;
        value2)
            command2
        ;;
        value3)
            command3
        ;;
        *)
            command4
        ;;
esac



[root@mail bash]# cat  case.sh
#!/bin/bash

case "$1" in
        root|pg)
                echo "$1 ok"
        ;;
        *)
                echo "fail"
        ;;
esac

#case $1 in
#       root)
#               echo "$1 ok"
#       ;;
#       pg)
#               echo "$1 ok"
#       ;;
#       *)
#               echo "fail"
#       ;;
#esac


[root@mail bash]# cat   authuser_case.sh
#!/bin/bash

case "$1:$2" in
        root:123)
                echo "$1 ok"
        ;;
        pg:123)
                echo "$1 ok"
        ;;
        *)
                echo "unknow"
        ;;
esac

++++++++++++++++++++++++++++++++++++++++++
循環


1)while語句語法:
n=1
while 條件       #while后所寫的條件是命令
do
    條件成立,執行的操作
    ((n++))
done
[root@mail bash]# cat   while.sh
#!/bin/bash

count=1
while test $count -le 100
do
        echo $count
        count=$[$count+1]
        #((count++))
done
# 變量初始值(水缸為空),條件(水缸沒滿), 變量自增(挑一桶水,做一個記號,如自增1)

[root@mail bash]# cat ./while2.sh
#!/bin/bash

while :
do
        echo "haha"
done



2)for語句語法:

[root@mail bash]# cat ./for1.sh
#!/bin/bash
for((n=1;n<=100;n++))
do
        echo $n
done


[root@mail bash]# cat ./for2.sh
#!/bin/bash

for n in 1 2 3 4 5 6 7 8 9 10
do
        echo $n
done


for 變量名 in value1 value2 value3 value4
do
    echo $變量名
done

 
[root@mail bash]# cat  for3.sh
#!/bin/bash

for file in /*
do
        echo $file
done


[root@mail bash]# cat  pingfor.sh
#!/bin/bash

for((i=1;i<=254;i++))
do
        (if ping -c 1 -W 1 192.168.1.$i &> /dev/null
        then
                echo 192.168.1.$i
        fi) &
        trap 'echo "收到!";exit 0' 2
done


[root@mail bash]# cat   pingfor2.sh
#!/bin/bash

for i in `seq 1 254`
do
        if ping -W 1 -c 1 192.168.1.$i
        then
                echo 192.168.1.$i
        fi
done

#for i in {1..254}
#do
#       if ping -W 1 -c 1 192.168.1.$i
#       then
#               echo 192.168.1.$i
#       fi
#done






[root@mail bash]# cat ./while3.sh
#!/bin/bash

while :
do
        echo 1
        sleep 3
done
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
定義一個名為array1的數組
方法1:
元素分別為aa bb cc dd 共4個元素
[root@mail bash]# array1=(aa bb cc dd)
[root@mail bash]# echo ${array1[0]} 通過下標0,來訪問第一個數組元素
aa
[root@mail bash]# echo ${array1[1]} 通過下標1,來訪問第二個數組元素
bb
總結:數組的標是從0到數組元素個數減1
[root@mail bash]# echo ${array1[2]}
cc
[root@mail bash]# echo ${array1[4]}

[root@mail bash]# echo ${array1[3]}
dd
[root@mail bash]# echo ${array1[*]} 顯示所有數組元素
aa bb cc dd
[root@mail bash]# echo ${#array1[*]} 顯示數組元素個數
4



echo $[${#array1[*]}-1]
數組array1的最大下標值。

方法2:
先聲明數組,再定義其每個元素的值
[root@mail bash]# declare -a array2 聲明一個數組
[root@mail bash]# array2[0]=aaa  定義第一個數組元素的值
[root@mail bash]# array2[1]=bbb  。。。。
[root@mail bash]# array2[2]=ccc  。。。。
[root@mail bash]# array2[3]=ddd  。。。。
[root@mail bash]# echo ${array2[*]}
aaa bbb ccc ddd
[root@mail bash]# echo ${array2[0]}
aaa
[root@mail bash]# echo ${array2[1]}
bbb
[root@mail bash]# echo ${array2[2]}
ccc
[root@mail bash]# echo ${array2[3]}
ddd
[root@mail bash]#


例子:

[root@mail bash]# cat  100array.sh
#!/bin/bash


#array100=(`for((n=1;n<=100;n++));do echo $RANDOM;done`)
declare -a array1000
for((n=0;n<=99;n++))
do
        array1000[$n]=$RANDOM
done
echo ${array1000[*]}
echo ${#array1000[*]}
[root@mail bash]#

================腳本位置參數============
[root@mail bash作業2答案]# set -- "1a 2a" 3a 4a 5a
[root@mail bash作業2答案]# echo $1
1a 2a
[root@mail bash作業2答案]# echo $2
3a
[root@mail bash作業2答案]# echo $3
4a
[root@mail bash作業2答案]# echo $4
5a
[root@mail bash作業2答案]# echo $#
4
[root@mail bash作業2答案]# echo $*
1a 2a 3a 4a 5a
[root@mail bash作業2答案]# echo $@
1a 2a 3a 4a 5a
[root@mail bash作業2答案]# for i in "$@";do echo $i ;done
1a 2a
3a
4a
5a
[root@mail bash作業2答案]# for i in "$*";do echo $i ;done
1a 2a 3a 4a 5a

寫一個腳本,作用是將腳本的所有位置參數(輸入的位置參數均是整數)相加,並打印出相加的結果。
方法1:
]# cat  10.sh
#!/bin/bash

count=0
#for i
for i in "$@"
do
        count=$[$count+$i]
done
echo $count
[root@mail bash作業2答案]# ./10.sh  1 2 3 4 5 6
21

方法2:
]# cat 10.sh
count=0
m=$#
for((n=1;n<=m;n++))
do
        count=$[$count+$1]
#       echo $1
        shift 1

done
echo $count

=================函數==============
定義函數的目的:將有特定功能的代碼塊,定義一個函數名稱,在要使用此代碼塊時,直接應用函數名,而不用再些一邊此代碼塊

1、如下定義函數的方法
函數名() {
    函數體(代碼塊)
}

[root@mail bash作業2答案]# cat  fun1.sh
#!/bin/bash

myls() {
        ls
}
[root@mail bash作業2答案]# cat  10.sh
#!/bin/bash

total(){
        count=0
        m=$#
        for((n=1;n<=m;n++))
        do
                count=$[$count+$1]
                shift 1

        done
        echo $count
}
2、使用函數:
方法一:
在腳本中直接定義函數,在定義過后,直接寫函數名調用函數


]# cat  10.sh
#!/bin/bash

total(){   此處為函數定義
        count=0
        m=$#
        for((n=1;n<=m;n++))
        do
                count=$[$count+$1]
                shift 1

        done
        echo $count
}
total $1 $2 $3 此處為通過函數名直接使用函數

再比如:
 /etc/init.d/httpd  腳本中定義了start() {command1;command2;}
腳本后面 case "$1" in
        start)
            start 此處則是使用函數名調用函數
        ;;
        ...
        ...
    esac

方法2:
在一個文件中專門定義好函數,在另一個腳本中source  /函數文件,然后直接把函數名當命令使用。
比如:
vim /etc/init.d/httpd
# Source function library.
. /etc/rc.d/init.d/functions 此文件定義了大多數重要的系統函數,系統函數名一般是以雙下划線開頭命名

在腳本后面,直接使用了/etc/rc.d/init.d/functions中定義好的status函數

取消函數:
unset -f 函數明
]# unset -f total
取消變量:
unset 變量名

執行順序:別名-特殊內建命令-函數-內建命令-外部命令

======bash的變量操作==========
[root@mail test]# file=1a.sh 定義一個普通變量
[root@mail test]# echo ${file} 打印一個普通變量的值
1a.sh
[root@mail test]# echo ${#file} 打印一個變量的值的字符個數
5
[root@mail test]# echo ${file:2} 略去變量的前2個字符
.sh
[root@mail test]# echo ${file: -3} 取出變量的后3個字符
.sh
[root@mail test]# file=/dir1/dir2/dir3/file.sh 定義一個普通變量
[root@mail test]# echo ${file:6:4} 略去前6個,往右數4個取出
dir2


[root@mail test]# echo ${file}
/dir1/dir2/dir3/file.sh
[root@mail test]# echo ${file: -10} 取出后10個字符
r3/file.sh
[root@mail test]# echo ${file: -10: 3} 取出后10個字符,往右數並取出3個字符
r3/

[root@mail test]# echo ${file: -3} 取出后3個字符
.sh
[root@mail test]# echo ${file:0:${#file}-3}  
/dir1/dir2/dir3/file

------------例子-------------------
[root@mail test]# ls
1a.sh  1e.sh  2d.sh  3c.sh  4b.sh  5a.sh  5e.sh
1b.sh  2a.sh  2e.sh  3d.sh  4c.sh  5b.sh
1c.sh  2b.sh  3a.sh  3e.sh  4d.sh  5c.sh
1d.sh  2c.sh  3b.sh  4a.sh  4e.sh  5d.sh
[root@mail test]# for file in ./*;do mv $file ${file:0:${#file}-3};done
[root@mail test]# ls
1a  1c  1e  2b  2d  3a  3c  3e  4b  4d  5a  5c  5e
1b  1d  2a  2c  2e  3b  3d  4a  4c  4e  5b  5d


---------------------
[root@mail test]# echo ${file}
/dir1/dir2/dir3/file.sh
[root@mail test]# echo ${file%dir*} 從右往左數到第一個dir字符串,把dir及其右邊的字符都刪除
/dir1/dir2/
[root@mail test]# echo ${file%/*} 從右往左數到第一個/字符串,把/及其右邊的字符都刪除
/dir1/dir2/dir3
[root@mail test]# echo ${file}
/dir1/dir2/dir3/file.sh
[root@mail test]# echo ${file%%i*}從右往左數到最后一個i字符串,把i及其右邊的字符都刪除
/d

[root@mail test]# echo ${file}
/dir1/dir2/dir3/file.sh
[root@mail test]# echo ${file#*i} 從左往右數,數到第一個i字符,把i及其左邊的字符都刪除
r1/dir2/dir3/file.sh

[root@mail test]# echo ${file##*/}從左往右數,數到最后一個/字符,把/及其左邊的字符都刪除
file.sh

[root@mail test]# basename /dir1/dir2/dir3/file.sh
file.sh
----------------------
[root@mail test]# ls
1a.txt  1e.docs  2d.sh  3c.sh  4b.sh  5a.sh  5e.sh
1b.sh   2a.sh    2e.sh  3d.sh  4c.sh  5b.sh
1c.sh   2b.sh    3a.sh  3e.sh  4d.sh  5c.sh
1d.sh   2c.sh    3b.sh  4a.sh  4e.sh  5d.sh

[root@mail test]# for i in ./*;do mv $i ${i%.*};done
[root@mail test]# ls
1a  1c  1e  2b  2d  3a  3c  3e  4b  4d  5a  5c  5e
1b  1d  2a  2c  2e  3b  3d  4a  4c  4e  5b  5d


************************
[root@mail test]# echo ${file} 定義一個變量file
/dir1/dir2/dir3/file.sh
[root@mail test]# echo ${file:-abc}  :-的意思是變量file如果被定義,則還是原始變量的值,如果變量沒有被定義,則臨時將字符串abc賦值給變量file
/dir1/dir2/dir3/file.sh
[root@mail test]# unset file 將變量file取消
[root@mail test]# echo ${file:-abc}
abc
[root@mail test]# echo ${file}

[root@mail test]#

:- 在一般情況下可以等價寫為-
echo ${file:-abc} 等價寫為 echo ${file-abc}
但是當 file="" 賦值為空時,會有如下結果
[root@mail test]# file=""
[root@mail test]# echo ${file-"abc"}  -認為變量被定義了

[root@mail test]# echo ${file:-"abc"} :-認為變量沒有被定義,於是臨時賦值為新的字符串
abc



[root@mail test]# file=/dir1/dir2/dir3/file.txt
[root@mail test]# echo ${file:+abc} 表示如果file被定義過,則重新臨時賦值為abc,如果沒有被定義過,則不重新定義
abc
[root@mail test]# echo ${file}
/dir1/dir2/dir3/file.txt

[root@mail test]# echo ${file2} 如:file2變量沒有被定義過,則不重新定義

[root@mail test]# echo ${file2:+abc}

[root@mail test]#

:+ 在一般情況下可以等價寫為+
echo ${file:+abc} 等價寫為 echo ${file+abc}
但是當 file="" 賦值為空時會不一樣




[root@mail test]# echo ${file4}

[root@mail test]# echo ${file4:=abc} :=表示如果變量沒有被定義,則永久賦值為新的字符串abc,如果變量已經被定義,則操持原始值
abc
[root@mail test]# echo ${file4}
abc
[root@mail test]#


[root@mail test]# echo ${file7:?is not set} 表示如果file7沒有被定義,則提示:?后面的字符串。
bash: file7: is not set
[root@mail test]# file7="123" 如果file7已經被定義,則不做提示
[root@mail test]# echo ${file7:?is not set}
123



[root@mail pg]# echo -en "\\033[0;39m"
[root@mail pg]# echo -en "\\033[0;33m"
[root@mail pg]# echo -en "\\033[0;35m"
[root@mail pg]# echo -en "\\033[0;36m"
[root@mail pg]# echo -en "\\033[0;37m"
[root@mail pg]# echo -en "\\033[0;38m"
[root@mail pg]# echo -en "\\033[0;39m"
[root@mail pg]# echo -en "\\033[0;40m"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM