鳥哥的Linux私房菜基礎學習篇(第二版)第十三章課後習題與答案

 習題:php

一、請創建一支 script ,當你執行該 script 的時候,該 script 能夠顯示: 1. 你目前的身份 (用 whoami ) 2. 你目前所在的目錄 (用 pwd)。linux

答:bash

#!/bin/bashapp

echo -e "Your name is ==> `whoami`"ide

echo -e "The current directory is ==> `pwd`"spa

二、請自行創建一支程序,該程序能夠用來計算『您還有幾天能夠過生日』啊??ip

答:rem

#!/bin/bash字符串

read -p "Pleas input your birthday (MMDD, ex> 0709): " birget

now=`date +%m%d`

if [ "$bir" == "$now" ]; then

echo "Happy Birthday to you!!!"

elif [ "$bir" -gt "$now" ]; then

year=`date +%Y`

total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))

echo "Your birthday will be $total_d later"

else

year=$((`date +%Y`+1))

total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))

echo "Your birthday will be $total_d later"

fi

三、讓用戶輸入一個數字,程序能夠由 1+2+3... 一直累加到用戶輸入的數字爲止。

答:

#!/bin/bash

read -p "Please input an integer number: " number

i=0

s=0

while [ "$i" != "$number" ]

do

i=$(($i+1))

s=$(($s+$i))

done

echo "the result of '1+2+3+...$number' is ==> $s"

四、撰寫一支程序,他的做用是: 1.) 先查看一下 /root/test/logical 這個名稱是否存在; 2.) 若不存在,則創建一個檔案,使用 touch 來創建,創建完成後離開; 3.) 若是存在的話,判斷該名稱是否爲檔案,若爲檔案則將之刪除後創建一個目錄,文件名爲 logical ,以後離開; 4.) 若是存在的話,並且該名稱爲目錄,則移除此目錄!

答:

 #!/bin/bash

if [ ! -e logical ]; then

touch logical

echo "Just make a file logical"

exit 1

elif [ -e logical ] && [ -f logical ]; then

rm logical

mkdir logical

echo "remove file ==> logical"

echo "and make directory logical"

exit 1

elif [ -e logical ] && [ -d logical ]; then

rm -rf logical

echo "remove directory ==> logical"

exit 1

else

echo "Does here have anything?"

fi

五、咱們知道 /etc/passwd 裏面以 : 來分隔,第一欄爲帳號名稱。請寫一隻程序,能夠將 /etc/passwd 的第一欄取出,並且每一欄都以一行字符串『The 1 account is "root" 』來顯示,那個 1 表示行數。

答:

 

#!/bin/bash

accounts=`cat /etc/passwd | cut -d':' -f1`

for account in $accounts

do

declare -i i=$i+1

echo "The $i account is \"$account\" "

done

 

 本文所整理的答案出自 做者:鳥哥   原文地址:http://linux.vbird.org/linux_basic/fc4.php

轉載時,請務必附上上述做者和出處!

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息