新增兩個 shell script 方便多檔案的 git status 和 git add

複習以前學過的, 也學習以前不知道的 shell script 檔案處理工具用法.

 

1. status.sh

if [ $# == 2 ]
then
    PARSER=$1
    TARGET=$2

    git status | grep $PARSER | grep "modified" | grep -v ".bak" | awk '{print $3}' > $TARGET
    git status | grep $PARSER | grep -v "modified" | grep -v ".bak" | awk '{print $2}' >> $TARGET
    echo >> $TARGET
    
    if [ "$?" == "0" ]
    then
        echo "*** Success! check result ***"
    else
        echo "*** [error] *** "
    fi
    
    cat $TARGET

else
    echo "*** [error] MUST input one parser and one target ***"
fi

 

# ----------- below is for svn -------------
#svn status | grep "M" | grep -v ".bak" | awk '{print $2}' > svn_status
#echo >> svn_status

1-1. $# 代表呼叫此 shell script 附帶的參數數目.

1-2. $1 代表呼叫此 shell script 附帶的第一個參數

1-3. grep -v "xxx" 代表去除 xxx 的行

1-4. awk '{print $3}' 取出第三欄

1-5. >> 代表附在檔案後面

1-6. echo >> $STATUS 新增一檔尾空白行, 為了接下來的 add.sh

1-7. if [ "$?" == "0" ] 判斷如果上一個指令成功

 

2. add.sh

if [ $# == 1 ]
then
    SOURCE=$1

    while read line
    do
        name+=" "$line
    done < $SOURCE
    
    git add $name
    
    if [ "$?" == "0" ]
    then
        echo "*** Success! ***"
    else
        echo "*** [error] *** "
    fi
    
    git status | sed -n "/Changes to be committed:/,/Untracked files:/p"

else
    echo "*** [error] MUST input one source ***"
fi

2-1. sed -n "/AAA/,/BBB/p"

取出 AAA (含) 行以下到 BBB (含) 行以上的所有內容

 

Reference:

1. http://stackoverflow.com/questions/22221277/bash-grep-between-two-lines-with-specified-string

取出 AAA (含) 行以下到 BBB (含) 行以上的所有內容

太強大, 沒研究

2. http://stackoverflow.com/questions/4487328/how-to-use-grep-to-match-multiple-strings

grep 多個關鍵字

3. https://www.cyberciti.biz/faq/howto-grep-text-between-two-words-in-unix-linux/

取出 AAA (含) 行以下到 BBB (含) 行以上的所有內容

add.sh 參考此

 

 

arrow
arrow
    全站熱搜

    kk 發表在 痞客邦 留言(0) 人氣()