2014年4月7日 星期一

2014 / 04 / 08 (Tue) 課堂筆記 Shell Script



做為shell interpreter所在,做為第一行,一定要寫
#!/bin/bash 

執行shell script方法

<1> 直接執行
先給予權限 chomd +x *.sh
直接執行 ./*.sh

一、建立一個shell,印出hello World

#!/bin/bash
echo -e "Hello \t World \a \n"


二、建立一個shell script,輸入並印出firstname , lastname 

#!/bin/bash
read -p "Please input your firstname: " firstname
read -p "Pleash input your lastname: " lastname
echo "$firstname $lastname"

三、變數取用範例 - 輸入檔名,建立三個檔案,檔名+日期,共三種。

#!/bin/bash
echo -e "I will use 'touch' command to create 3 files."
read -p "Please input your filename : " filename

date1=$(date --date='2 days ago' +%Y%m%d)
date2=$(date --date='1 days ago' +%Y%m%d)
date3=$(date +%Y%m%d)

file1=${filename}${date1}
file2=${filename}${date2}
file3=${filename}${date3}

touch $file1 $file2 $file3

四、if-then-else條件判斷式

ead -p "Please input (Y/N):" yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
   echo "OK,continue"
elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
   echo "Oh,interrupt!"
else
   echo "I don't know what your choice is"



沒有留言:

張貼留言