Linux 腳本收集

括號的使用說明

參考:Double parenthesis with and without dollar

  • $(…) means execute the command in the parens and return its stdout.
$ echo "The current date is $(date)"
The current date is Mon Jul  6 14:27:59 PDT 2015
  • (…) means run the commands listed in the parens in a subshell. Example:
$ a=1; (a=2; echo "inside: a=$a"); echo "outside: a=$a"
inside: a=2
outside: a=1
  • $((…)) means perform arithmetic and return the result of the calculation. Example:
$ a=$((2+3)); echo "a=$a"
a=5
  • ((…)) means perform arithmetic, possibly changing the values of shell variables, but don’t return its result. Example:
$ ((a=2+3)); echo "a=$a"
a=5
  • ${…} means return the value of the shell variable named in the braces. Example:
$ echo ${SHELL}
/bin/bash
  • {…} means execute the commands in the braces as a group. Example:
$ false || { echo "We failed"; exit 1; }
We failed

有用腳本收集

文件讀取

讀取文件目錄的所有文件,按行讀取每個文件,判斷行文字是否包含特定字符串;如果包含,通過特殊字符來split並輸出想要的值。

#!/bin/sh
for filename in /home/okchem/mysqlbackup/*.sql; do
    while IFS= read line
    do
        # display $line or do somthing with $line
        if [[ $line == *"/ocf/"* ]]; then
            SUBSTRING=$(echo $line| cut -d'`' -f 2) # 用'`'來拆分,輸出數組第二個
            echo $SUBSTRING
        fi
    done <"${filename}.sql"
done
發佈了40 篇原創文章 · 獲贊 16 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章