1.创建一个脚本,当执行脚本执行时,脚本可以如下显示:
(1).你目前的身份是:
(2).你目前所在的目录是:
#!/bin/bash
#describe
# Answer who you are and where you are
echo "你目前的身份是:"`whoami`
echo "你目前所在目录是:"`pwd`
2.创建一个脚本,该脚本可以根据你输入的日期计算出你的生日还有多少天
#!/bin/bash
#describe:
# Calculate the number of days away from birthdays
#
#输入生日日期,格式为:MMDD
read -p "please input your birthday(the format is:MMDD)": birthday
#
#今年的日期,格式为:MMDD
today=`date +%m%d`
#
#判断今天是不是输入的日期
if [ "$birthday" == "$today" ];then
echo "today is your birthday."
#
#通过时间戳计算,先确定是哪一年,再计算两个时间戳之差,然后再转换成天数
#
#计算今年未过的生日时间
elif [ $birthday -gt $today ];then
year=`date +%Y`
total_day_s=$((`date --date="$year$birthday" +%s`-`date +%s`))
total_day=$(($total_day_s/60/60/24+1))
echo "It's still near your birthday:$total_day day"
#
#计算今年已过明年的生日时间
else
year=$((`date +%Y`+1))
total_day_s=$((`date --date="$year$birthday" +%s`-`date +%s`))
total_day=$(($total_day_s/60/60/24+1))
echo "It's still near your birthday:$total_day day"
fi
3.创建一个脚本,执行脚本后让用户输入一个数字,程序可以由1+2+3····一直加到shi用户输入的数字为止。
#!/bin/bash
#describe
# Calculate the sum of 1 to the input value
read -p "请输入一个正整数:" o
if [ $o -le 0 ];then
echo "输入错误"
else
seq $o|awk '{s+=$1}END{print "1到"$O"的和为:"s}'
fi
4.创建一个脚本,脚本作用如下:
(1).先查看/tmp/nebula这个名称是否存在
(2).如不存在则创建一个文件,建立后退出
(3).若存在则判断该名称是否是否是文件,若为文件则将其删除后创建一个同名目录,之后退出。
(4)如果存在且为目录则删除。
#!/bin/bash
#describe
# Create /tmp/nebula directory
#
#判断/tmp/nebula是否存在
if [ ! -e /tmp/nebula ];then
touch /tmp/nebula
#判断/tmp/nebula是否为文件
elif [ -f /tmp/nebula ];then
rm -f /tmp/nebula && mkdir -p /tmp/nebula
#判断/tmp/nebula是否是目录
else
rm -rf /tmp/nebula
fi
5.创建一个脚本,将/etc/passwd中以:为分隔符的第一个域取出,并在取出后的每一行中都以“The 1 account is ”root“ ”来表示,其中1表示行数。
#!/bin.bash
#describe
# Intercept /etc/passwd first column and Output in fixed statement format
#用awk截取以“ :”为分隔符的首列,输出时按固定语句格式输出
awk -F":" '{print "The "NR" account is " $1}' /etc/passwd
[root@mail ~]#