What is Shell?

2022/07/10


簡介

與Windows、Mac OS、Android 等操作系統不一樣,它們都由一家公司開發,所有的核心軟體和基礎庫都由一家公司開發。而Linux 不一樣,它是由多個組織機構開發,不同的組織機構為了發展自己的Linux 分支可能會開發出功能類似的軟件,它們各有優缺點,用戶可以自由選擇。Shell 就是這樣的一款軟件,不同的組織機構開發了不同的Shell,它們各有所長,有的佔用資源少,有的支持高級編程功能,有的兼容性好,有的重視用戶體驗。


常見的 Shell

✓ sh 的全稱是Bourne shell,sh UNIX上的標準shell,sh是第一個流行的Shell。
✓ csh,這個shell 的語法有點類似C語言,所以才得名為C shell ,簡稱為csh。
✓ tcsh 是csh的增強版,加入了命令補全功能。
✓ ash是一個簡單的輕量級的Shell,佔用資源少,適合運行於低內存環境,但是與下面講到的bash shell 完全兼容。
✓ bash 擴展了一些命令和參數,同時保持了對 sh 的兼容性,是各種Linux 發行版默認配置的shell。


檢視自己的 Shell

Shell 是一個程序,一般都是放在/bin或者/user/bin目錄下,當前Linux 系統可用的Shell 都記錄在/etc/shells文件中。 通過cat /etc/shells命令來查看當前Linux 系統的可用Shell:

                          
         # cat current available shells

         /work/kai> cat /etc/shells
         /bin/ash
         /bin/bash
         /bin/csh
         /bin/dash
         /bin/false
         /bin/ksh
         /bin/ksh93
         /bin/mksh
         /bin/pdksh
         /bin/sh
         /bin/tcsh
         /bin/true
         /bin/zsh
         /usr/bin/csh
         /usr/bin/dash
         /usr/bin/ksh
         /usr/bin/ksh93
         /usr/bin/mksh
         /usr/bin/passwd
         /usr/bin/pdksh
         /usr/bin/bash
         /usr/bin/tcsh
         /usr/bin/zsh
                       

如果要查看當前Linux 的默認Shell,那麼可以輸出 echo $shell:

                          
         # Enter echo $shell

         /work/kai> echo $shell
         /bin/tcsh
                       


實驗室計算可用的 Shells

若是每一個 input file 都手動更改或跑 g16 會花很多時間, 所以可以撰寫 sh 幫助計算。以下舉例一些簡單、計算可以用的 sh 檔, 詳細的檔案請到學長姊的路徑查看:

跑 input file 的 Shell

若想執行當前目錄下所有的 input files (.com/.gjf),可用此 Shell,以 input file = .gjf 為例:

                                
         #!/bin/bash                      # 宣告這個檔案內的語法使用 bash
         #
         #
         ls *.gjf  > Run.dat              
         g16_file=$(cat Run.dat)          
         
         for file in ${g16_file}
         do
            g16 ${file} &
            wait
         done
         #
                             

檢查波函數的 Shell

當計算完,可用此 Shell 檢查波函數 (stable/stable=singlet),以 stable=singlet 為例:

                                       
         #!/bin/bash
         #
         #
         ls *.gjf  > singlet.dat
         sed -i 's/\.gjf//g' singlet.dat
         g16_file=$(cat singlet.dat)
         mkdir -p singlet
         cd singlet
                   
         for file in ${g16_file}
         do
               cp ../${file}".chk" ${file}"_singlet.chk"
               cp ../${file}".gjf" ${file}"_singlet.gjf"
               wait
               if [[ ${file} == *'MP2'* ]]; then                      # Hartree-Fock calculation 檢查波函數用 HF
                  sed -i '4 s/MP2/HF/' ${file}"_singlet.gjf"
               elif [[ ${file} == *'B3LYP'* ]]; then                 # DFT calculation 檢查波函數保持
                  sed -i '4 s/B3LYP/B3LYP/' ${file}"_singlet.gjf"
               fi                                                    # 因我只用 MP2 and B3LYP 所以只列出兩個條件句 可自行新增
               wait
                 sed -i 's/opt.*freq/stable=singlet guess=read geom=allchk/' ${file}"_singlet.gjf"
               wait      
                 sed -i '4a \\' ${file}"_singlet.gjf"
               wait
                 sed -i '7,$d' ${file}"_singlet.gjf"
               wait
               g16 ${file}"_singlet.gjf" &
               wait
         done
         #
                                    

換理論方法計算的 Shell

若已用較低的理論方法或基底函數做過結構最佳化,想將其結構以其他方法計算,可用此 shell。 以下舉例將 MP2/aug-cc-pVDZ 最佳化結構,進行 MP2/aug-cc-pVTZ opt 計算,注意 Ar and Xe 須加 diffuse function, 因此須寫條件句進行計算:
template (模板)

                     
         #!/bin/bash
         #
         #
         ls *.com  > MP2_aptz.dat
         sed -i 's/\.com//g' MP2_aptz.dat
         g16_file=$(cat MP2_aptz.dat)
         mkdir -p MP2_aptz
         cd MP2_aptz
         
         for file in ${g16_file}
         do
              cp ../${file}".chk" ${file}".chk"
              wait
              if [[ ${file} == *'He'* ]] || [[ ${file} == *'Ne'* ]] || [[ ${file} == *'Kr'* ]]; then
                   cp /work/kai/NobleGas/template_MP2_pVTZ.com ${file}".com"
              elif [[ ${file} == *'Ar'* ]]; then
                   cp /work/kai/NobleGas/Ar_template_MP2_pVTZ.com ${file}".com"
              elif [[ ${file} == *'Xe'* ]]; then
                   cp /work/kai/NobleGas/Xe_template_MP2_pVTZ.com ${file}".com"
              else
                   echo " ${file} ERROR! "
              fi
             wait
              sed -i '4 s/freq/freq geom=allchk/' ${file}".com"
             wait
              sed -i 's/notdf/O C N/g' ${file}".com"
             wait
              sed -i '3 s/FileName/'${file}'/' ${file}".com"
             wait
              sed -i 's/pVDZ/pVTZ/g' ${file}".com"
             wait
              rename pVDZ pVTZ *
             wait
         done
         
          sh /work/kai/run_com.sh &
         wait
          rm /work/kai/run_com.dat
         echo "Done"
         #
                  

搭配 alias 使用

若為常用的 Shell 可將其放置在自己的 /work/ 並且搭配 alias 以便使用,以寫有 echo "test" 的 test.sh 為例:

                        
         /work/kai> sh test.sh
         test
         /work/kai> cd
         /home/kai> vi .tcshrc 
         /home/kai> cat .tcshrc 
         #!bin/tcsh 
                           
         cd /work/kai
         alias t     "sh /work/kai/test.sh"
                           
         /home/kai> source .tcshrc 
         /work/kai> t
         /work/kai> test
                           

Reference

幾種常見的Shell:sh、bash、csh、tcsh、ash
Rename all files in directory from $filename_h to $filename_half?