Problem 為了利於測試,先前做過各個不同作業系統的自動登入( link )。隨著測試機逐日增加,為了節省時間與練習shellscript,因此決定寫一個script將設置自動登入的動作給簡單化。但面對不同的Linux,會有不同的自動登入方式,我也很直接的針對不同Distribution使用不同的做法。目前我所支援的作業系統為SLES11、RHEL5、RHEL6、CentOS5、CentOS6。 How to? 確認Linux Distribution 首先我們可以透過/etc/issue去判斷作業系統版本。以下是我收集的資訊: Welcome to SUSE Linux Enterprise Server 10 SP2 Welcome to SUSE Linux Enterprise Server 11 SP2 Red Hat Enterprise Linux Server release 6.0 Red Hat Enterprise Linux Server release 5.6 CentOS release 5.6 CentOS Linux release 6.0 作法是透過grep指令去判斷/etc/issue檔案是否有出現對應的OS Pattern。SLES判斷是否有SUSE;CentOS/RHEL則是判斷開頭字串與主要版本號;其它的顯示不支援。找到對應的Distribution後就呼叫對應的function。這裡設置OS_INFO_FILE變數為$1與將實際操作寫為各個function,主要是為了便於測試,不然系統檔改壞就要DRBL了。 OS_INFO_FILE = $1 if [ "$1" == "" ] ; then OS_INFO_FILE = / etc / issue fi if grep SUSE $OS_INFO_FILE > / dev / null; then setupAL_SLES elif grep -e "\(CentOS\)\|\(Red Hat\).*5\.*" $OS_INFO_FILE > / dev / null; then setupAL_RHEL5 elif grep -e ...