我有一個劇本/location/centers_password.sh在我下面有行代碼:使代碼通用
if [ "$DC" = "sl" ]
then
arg1=`echo -n $Center|wc -m`
if [ $arg1 -gt 3 ]
then
grep "^$Center" /location/.CenterPassword.file.sdi.sl
grep "^$Center" /location/.CenterPassword_xel.file.sdi.sl
else
grep "^$Center" /location/.CenterPassword.file.sl
grep "^$Center" /location/.CenterPassword_xel.file.sl
grep "^$Center" /location/.sftpPasswod.file.sl
fi
elif [ "$DC" = "ch" ]
then
arg1=`echo -n $Center|wc -m`
if [ $arg1 -gt 3 ]
then
grep "^$Center" /location/.CenterPassword.file.sdi.ch
grep "^$Center" /location/.CenterPassword_xel.file.sdi.ch
else
grep "^$Center" /location/.CenterPassword.file.ch
grep "^$Center" /location/.CenterPassword_xel.file.ch
grep "^$Center" /location/.sftpPasswod.file.ch
fi
什麼,我就是在這裏做什麼,當我執行這個代碼,它讀取數據中心名稱.CenterPassword.file.sdi.ch文件。如果我解釋更多,假設數據中心從'sl'開始讀取.CenterPassword.file.sdi.sl文件並獲取密碼,並假設數據中心從'ch'開始讀取.CenterPassword.file.sdi.ch並獲取密碼。
我的要求是使這個代碼通用。此文件應讀取主機名,如果主機名的最初兩個字母與.CenterPassword.file.sdi.ch相匹配,則最後兩個字母(在此示例中,應與'ch'匹配)應提取密碼。請幫助做到這一點。
像'grep「^ $ Center」/ location/.CenterPassword.file.sdi。$ DC'? – pacholik
@pacholik如果我們可以實現這一點,如果語句調用主機名和該主機名模式(前兩個字母)應與.CenterPassword.file.sdi.ch文件匹配最後兩個字符 – Ritesh