Linux shell 學習

Shell 特性:

     history 保存在 ~/.bash_hostory,保留1000條:定義在

          echo $HISTSIZE

     !!: 上一條命令

     !$: 上一條命令的最後一個參數

     !950: 第950條命令

     !c: 歷史中最近的以 c 開頭的命令

     tab: 命令補全

     alias: 別名   unalias 

     *,?: 通配符  正則表達式

     |: 管道符

     >,>>: 重定向跟追加

     2>: 錯誤重定向   2&1>: 整齊跟錯誤重定向

     sleep,fg,bg,jobs: fg後面加個數字ID

     

Shell 變量:

     env: 查看系統變量

     set: 系統跟自定義變量

     變量中帶有特殊字符,需要用單引號 : b='ls /tmp'

     變量中引用命令,用反引號 : myvim=`which vim`

     變量之間的連接,用雙引號 : c="$a"12

     export : 變量全局聲明

     local : 變量局部定義

     unset d : 刪除一個變量的值

Shell 環境變量:

    系統級別:

     /etc/profile : /etc/profile.d/*.sh

     /etc/bashrc :

          1) /etc/profile.d/*.sh

          2) PS1='[\u@\h-\t \w(W)]\$ '

          3) umask 修改

               umask.sh : umask 0012

      用戶級別:

     .bash_history : 記錄歷史命令

     .bash_logout : 退出的時候所做的事

     .bash_profile : 用戶自己環境變量配置文件

     .bashrc : root用戶用到的別名

     

Shell 基本用法之 cut :

     cat /etc/passwd | cut -d ':' -f 1 | head -n5  (以:分割的第一段數據的前5條)

     head -n2 /etc/passwd | cut -c2  (截取第二個字符)

     head -n2 /etc/passwd | cut -c1-5   (截取第1-5個字符)

Shell 基本用法之 sort :

     head -n5 /etc/passwd | sort : 升序

     head -n5 /etc/passwd | sort -t: -k3 -n : 使用純數字以第三個字段來排序

     head -n5 /etc/passwd | sort -t: -k3,5 -r : 從第三到第五字段之間的字符串進行反向排序

     cut -d : -f1 /etc/passwd | sort -n -u : -nu 會把字符串都看成0 ,並且去重

    

Shell 基本用法之 wc:

     wc -l 1.txt 2.txt : 列出有幾行

     wc -w (word)   : 輸出以空格爲分割的多少個字

     echo "12345" | wc -m : 輸出多少字符

Shell 基本用法之 uniq 和 tee:

     uniq +文件 : 去重

     sort +文件 | uniq -c : 只有相互挨着的兩行相同纔會去重,-c是可以計算重複的行數

     echo "1213" | tee +文件 : 重定向並且把結果顯示在屏幕上

      

Shell 基本用法之 tr和 split:

     head -n2 /etc/passwd | tr '[a-z]' '[A-Z]' : 把小寫轉化爲大寫

     -d : 刪除某個字符; -s : 去除重複的字符

     

     split -b500 passwd : 將文件以500bytes 爲一個文件分割,默認不指定目標文件,則會產生xaa,xab,xac... 文件來顯示

     split -l 10 +文件: 將文件以10行爲單位來進行分割

     split -b 500 + 文件 123: 指定目標文件名,則會以123aa,123ab,123ac...來顯示

     

Shell 基本用法之 grep : 

     grep :

          1) . -c : 打印符合要求的行數

          2) . -n : 輸出行號

          3) . --color : 顯示顏色

          4) . -v : 打印不符合要求的行

          5) . -A+數字 : 顯示符合要求的行以及下面n行

          6) . -B+數字 : 顯示符合要求的行以及上面n行

          7) . -C+數字 : 顯示符合要求的行以及上下各n行

          8) . -rh : 訪問目錄 grep -rh "iptables" /etc/*

          9) . --include : 包含某個文件

                    把一個目錄下,過濾所有*.php 文檔中含有eval 的行

                    grep -r --include="*.php" 'eval' /data/

     egrep = grep -E

Shell 基本用法之 sed :

     sed '10'p -n 1.txt : 加上-n 打印出符合規則的行

     sed -n '/root/'p 1.txt : 打印包含特殊字符的行

     -r : sed 不能識別 +| {} () 等符號,需要脫義字符\或者-r 

          sed -n -r '/ro+/'p 1.txt

          sed -n '/ro\+/'p 1.txt

     -e : 實現多個任務,也可以用 ; 來實現

          sed -e '/root/p' -e '/body/p' -n 1.txt

     d : 刪除指定行

          sed '/root/d' 1.txt ; sed '1d' 1.txt ; sed '1,10d' 1.txt

     s : 替換

          sed '1,2s/ot/to/g' 1.txt

     替換兩個字符串的位置,其中\3\2\1分別代表三個()的內容

          head -n2 1.txt | sed -r 's/(root)(.*)(bash)/\3\2\1/'

     a:指定行後面添加

          sed '2,4a abcd' xx.txt 在2到4行添加 abcd

     -i : 直接修改文件

          sed -i 's/ot/to/g' 1.txt

Shell 基本用法之 awk:

     1. awk -F ':' '{print $1}' 1.txt

     2. awk -F ':' '{OFS="#"} {print $1,$2,$3,$4}' 1.txt 就是 

               awk -F ':' '{print $1#$2#$3#$4}' 1.txt

     3. awk '/oo/' 1.txt  : 匹配oo

     4. awk -F ':' '$1 ~/oo/' 1.txt  : 在第一段匹配oo

     5. 條件操作符 ==,>,<,!=,>=,<=

          第三段爲0 : awk -F ':' '$3=="0"' 1.txt

          第三段大於等於500 : awk -F ':' '$3>=500' 1.txt

          第七段不是'/sbin/nologin' : awk -F ':' '$7!="/sbin/nologin"' 1.txt

          

     6. awk 內置變量NF (段數) NR (行數)

          head -n3 1.txt | awk -F ':' '{print NF}'

     7. 數學計算:

          awk -F ':' '{OFS=":"}{$7=$3+$4; print $0}' 1.txt

     8. awk -F ':' '{(tot=tot+$3)}; END {print tot}' 1.txt

     9. awk -F ':' '{if ($1=="root") print $0}' 1.txt

Shell 基本語法:

1) #!/bin/bash 指定用bash 來運行

2) if 語句

     if condition ; then

          command

     fi

     

     if condition ; then

          command

     else

          command

     fi

     

     if condition ; then

          command

     elif condition; then

          command

     else

          command

     fi

3) [ 條件 ] 

     -d : 是否爲目錄

     -f :  是否爲普通文件

     -l :  是否爲連接文件

     -e : 是否存在

     -n : 是否不爲0

     -z : 是否爲0

     -r (-w -x):是否有權限

     

4) for 跟 while

     for i in `seq 1 5` do

          echo $i

     done

     

     while condition; do

          command

     done

     死循環:

     while :; do

          command

          sleep 3

     done

     continue; break; exit;

5) date 命令

     1) 2015-08-25 : date +%Y-%m-%d

     2) 15-08-25 : date +%y-%m-%d

     3) date +%F

     4) 21:45:15 : date +%H:%M:%S

     5) 21:45:15 : date +%T

     6) 時間戳 : date +%s

     7) date -d @1440510413

     8) 一天後 : date -d "+1day"

     9) 一天前 : date -d "-1day"

     10) 一月前 : date -d "-1month"

     11) 一分鐘前 : date -d "-1min"

     12) 星期 : date +%w(%W) 小寫表示周幾,大寫表示本年的第幾周

6) 數學運算

     sum = $[$a+$b] (+-*/)

     小數:echo "scale=2;10/3" | bc    輸出3.33

  

7) 交互

     read -p "Please input a number :" x

8) sh -x xx.sh查看執行過程

9)  case 語法

     case 變量 in 

     value1)

          command

          ;;

     value2)

          command

          ;;

     *)

          command

          ;;

     esac

10) Shell 數組

     a=(1,2,3,4)

     數組元素個數:echo ${#a[@]}

     讀取單個元素:echo ${a[2]}

     打印整個數組:echo ${a[@]}  或者 echo ${a[*]}

     刪除數組:unset a    unset a[1]

     數組分片:echo ${a[@]:0:3} 從下表0往後3個

     數組替換:echo ${a[@]/3/100} 將數組第三個換成100


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