shell編程

SHELL 編程

shell 基礎命令
#cd /home 進入 '/home' 目錄
cd .. 返回上一級目錄
cd../.. 返回上兩級目錄
ls 查看目錄中文件
ls -a 顯示隱藏文件
ls -l 顯示詳細信息
ls -lrt 按時間顯示文件
pwd 顯示工作目錄
mkdir newfile 創建'newfile'目錄
mv dir1 dir2 移動/重命名一個目錄
rm -rf file1 刪除'feil1'文件以及目錄
cat file1 從第一個字節開始正向查看文件內容
head -2 file1 查看一個文件的前兩行
more file1 查看一個長文件內容
tac file1 從最後一行開始反響查看一個文件的內容
tail -n 20 file1 查看最後20條記錄;
grep str /tmp/test 在文件'/tmp/test' 中查找‘str’
grep ^err /usr/local/greenplum/log/20190412.log 在 文件‘/usr/local/greenplum/log/20190412.log’中查找以'err' 開始的
grep [0-9] /data/gpmaster/gpseg-1/postgresql.conf 在文件中查找包含數字的行
find / -name file1 從 / 開始 查找指定文件名的文件或者目錄
find / -user username 查找屬於用戶'username'的文件目錄
bzip2 file 壓縮file 文件
bunzip2 file.bz2 解壓文件
gzip file 壓縮文件
gunzip file.gz 解壓文件
tar -cvf test.tar file 將file 打包成 test.tar
yum -y install [package] 下載並安裝一個rpm包
yum -y update 更新當前系統中安裝的所有的rpm包
yum remove [package] 刪除一個 rpm包
yum list 列出當前系統中安裝的所有包
yum search [package] 在rpm 倉庫中搜索軟件包
yum clean [package]清楚緩存目錄 /var/cache/yum 下的軟件包
yum clean headers 刪除所有頭文件
yum clean all 刪除所有的緩存包和頭文件
su - 切換用戶
shutdown -r now 當前重啓
shutdown -h now 當前關機
top 羅列使用cpu資源最多的任務
pstree 以屬性圖顯示程序
passwd 修改密碼
df -h 查看磁盤

1、shell 入門
1、shell 腳本 以#!/bin/sh 開頭 其中 #! 用來告訴系統後面的參數用於執行該文件的程序
一般編寫完成後 需要 chmod +x filename.sh 這樣纔可以執行 ./filename 來運行
同時腳本文件 以.sh 爲後綴
2、變量定義和引用
在SHELL中 所有的變了都是字符串組成同時不需要對變量聲明;賦值給一個變量就可以。
#/bin/bash
RZ='www.ruozedata.com'
DAT=date

        輸出變量
        echo ${RZ}
        echo ${DATE}

        靜態變量
        K=V 'V' "V"

        動態變量
      K=`V`

        其中 =前後不可能有空格

        引用:

        $KA 
        ${K}A

        3、傳遞參數
        腳本獲取參數格式 $0 $1 $2 $3 .... 其中 $1爲傳遞的第一個參數 而$0接受的是 ./parameter.sh 這個文件
        #!/bin/bash

        echo $1
        echo $2
        echo "個數:$#"
        echo "PID: $$"

        #ps -ef |grep 5480
        #ps -ef |grep 5480|grep -v grep

4、數組--在shell中 用() 來表示的數組,數組元素之間用空格來分隔,由此 可以定義如下形式:
--數組可以存放多個值,shell 只支持一維組(不支持多維數組)

     array=(1,2,3,4,5,6)    -----=號不能有空格,必須緊挨這數組名和數組元素
     定義數組
     arr=(1,2,3,4,5)

     shell 是弱類型,他並不要所有數組元素類型必須相同;
     arr=(60,88,"https://blog.51cto.com/965726")

  #!/bin/bash

        arr=(ruoze jepson xingxing dashu xiaoshiqi xiaohai)

        echo ${arr[@]}
        echo ${arr[*]}
        echo ${arr[4]}
        echo {#arr[@]}

        # chmod +x array.sh   授權

5、if 判斷
語法格式
if condition;then
statement(s)
fi
例子:
#if.sh
#!/bin/bash

     A="abc"
     B="jepson"

     if [ $a == $b ]; then
         echo "=="
    else

         echo "!="

    fi

==前後空格
[ $a == $b ]

6、循環
while 循環 當添加滿足時 while 重複地執行一組語句, 當條件不滿足時 就退出while 循環
#!/bin/bash

        i=1
        sum=0
        while (i <= 100)
        do
           ((sum +=i))
             ((i++))
        done
      echo "the test $sum"

    for 循環用法

    for ((exp1;exp2;exp3))
    do
      statements
    done
    -----其中  exp1、exp2、exp3 是表達式,其中 exp2是判斷條件,for 循環依據 Exp2 的結果來決定是否繼續席下次循環;
    statements 是 循環體語句,可以有一條,或者多條;
    do 和 done 是shell 中關鍵字;
    #!/bin/bash

    sum=0

    for ((i=1;i<=100;i++))

    do
       ((sum +=i))
    done

echo "then sum $sum"

i++ ==>>i=i+1

7、分割
vi spilt.sh
#!/bin/bash

S="ruoze,jepson,xingxing,dashu,xiaoshiqi,xiaohai"
OLD_IFS="$IFS"
IFS=","
arr=($S)
IFS="OLD_IFS"

for x in ${arr[*]}
do
echo $x
done

#chmod +x ./spilt.sh
#./spilt.sh
ruoze
jepson
xingxing
dashu
xiaoshiqi
xiaohai


8、awk
awk options program file
1、 -F fs 指定行中劃分數據字段的字段分隔符;
2、-f programFile 從指定文件中讀取代碼數據;
3、 -v var=value 定義awk 程序一個變量以及默認值
$0代表整個文本行
$1代表文本行中的第一個數據字段,$2代表第二個字段,以此類推

$ cat test.txt
This is a test, this is the first line of the test.
This is a test, this is the second line of the test.
Hahahaha!
$ awk '{print $1}' test.txt
This
This
Hahahaha!

#vi awk.log
a,b,c
1,2,3
4,5,6
#cat awk.log |awk '{ print $1 }'
a,b,c
1,2,3
4,5,6
#cat awk.log| awk -F ',' '{ print $1}'
a
1
4
#cat awk.log| awk -F ',' '{ print $3}'
c
3
6
#cat awk.log |awk -F"," 'NR >1{ print $3}'
3
6

9、替換

vi sed.log

a b c
1 2 3

sed -i 's/a/aa' sed.log 用於將 a 替換成 aa
cat sed.log
aa b c
1 2 3

#sed -i "s/b/w" sed.log
#cat sed.log
wbb b c
1 2 3

前面加
#sed -i "s/^/uuuu&/g" sed.log
cat sed.log
uuuwww w c
uuu1 2 3

後面加
#sed -i "s/$/&uuu/g" sed.log
cat sed.log
uuwww w cuuu
uuul 2 3uuu

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章