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
标签:shell,name,SETUP,INITIAL,KS,user,FILE
From: https://www.cnblogs.com/Bruce-blog/p/16952826.html