1. 程式人生 > 其它 >Springboot 實現Redis 同資料來源動態切換db

Springboot 實現Redis 同資料來源動態切換db

INITIAL_SETUP_KS_FILE="/root/initial-setup-ks.cfg"

1. tail 取檔案最後一行

user_line_string=`tail -n 1 $INITIAL_SETUP_KS_FILE`
echo $user_line_string

2. $? 與 -ne

grep $root_label  $INITIAL_SETUP_KS_FILE
if [ $? -ne 0 ];then
    #echo "not exist rootpw, delete root passwd
else

3. uniq 與 ''換行

uniq 刪除檔案中重複行,從頭到尾,上下兩行進行遍歷比較
uniq $INITIAL_SETUP_KS_FILE > initial-setup-ks.cfg.new && \
          rm -rf $INITIAL_SETUP_KS_FILE && mv initial-setup-ks.cfg.new $INITIAL_SETUP_KS_FILE
  #echo "exist rootpw"

4. 檔案 字串 按符號“:” 分割並擷取

`rootpw_line_string=`grep -n $root_label  $INITIAL_SETUP_KS_FILE | cut -d ":" -f 2``

5.判斷 字串是否包含自字串

if [[ $rootpw_line_string =~ $lock_label ]];then
     echo "root locked"

6.字串中 擷取 子字串

擷取 "--name=bruce --lock" 中的 "bruce"
user_name_r=${user_line_string#*--name=}
 user_name=${user_name_r%% *} && passwd -uf $user_name