12.8
本章习题
(
要看答案请½鼠标移动到『答:』底下的空白处,按下左键圈选空白处即可察看
)
底下½为实作题,请自行撰写
出程序喔!
.
请½立一支
script
,当你执行该
script
的时候,该
script
可以显示:
1.
你目前的身份
(
whoami ) 2.
目前所在的目录
(
pwd)
#!/bin/bash
echo -e "Your name is ==> $(whoami)"
echo -e "The current directory is ==> $(pwd)"
.
请自行½立一支程序,该程序可以用来计算『你还有几天可以过生日』啊?
#!/bin/bash
read -p "Pleas input your birthday (MMDD, ex> 0709): " bir
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