shell 练习

1.用户输入日期
2.当前日期对比输入的日期
3.计算输入日期减去当前日期的时间

#!/bin/bash
read -p "please input the date you demobilizete (YYYYMMDD ex=> 20090422): " date1
date_d=$(echo $date1 | grep '[0-9]\{8\}') #正则表达式
if [ "$date_d" == "" ]; then
echo "you input the wrong date format......."
exit 1
fi
declare -i date_dem='date --date="$date1" +%s'  
declare -i date_now='date +%s'
declare -i date_total_s=$(($date_dem-$date_now))
declare -i date_d=$(($date_total_s/60/60/24))
if [ "$date_total_s" -lt "0" ]; then
echo "you had been demobiliation before : " $((-l*$date_d)) "ago"
else
declare -i date_h=$(($(($date_total_s-$date_d*60*60*24))/60/60))
echo "you will demoblize after $date_d days and $date_h hours."
fi


#报错
#please input the date you demobilizete (YYYYMMDD ex=> 20090422): 20120514
#./sh09.sh: line 8: declare: date --date="$date1" +%s: syntax error in expression (error token is "date="$date1" +%s")
#./sh09.sh: line 9: declare: date +%s: syntax error: operand expected (error token is "%s")
#./sh09.sh: line 10: -: syntax error: operand expected (error token is "-")
#./sh09.sh: line 11: /60/60/24: syntax error: operand expected (error token is "/60/60/24")
#./sh09.sh: line 12: [: : integer expression expected
#you will demoblize after 20120514 days and -482892336 hours.

Other:
#!/bin/bash 
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH  
echo -e "you should input 2 numbers, i will cross then! \n" 
read -p "first number:" firstnumber
read -p "second number:" secondnumber 
total=$(($firstnumber*$secondnumber)) 
echo -e "\n result of $firstnumber * $secondnumber is ==> $total"


这个有点不懂的地方
#!/bin/bash
read -p "input your filename:" fileuser
filename=${fileuser:-"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"
touch "$file2"
touch "$file3"


#!/bin/bash
echo -e "please input a filename,i will check the filename's type and permission. \n\n"
read -p "input a filename:" filename
test -z $filename && echo "you must input a filename " && exit 0
test ! -e $filename && echo " the filename '$filename' do not exist" && exit 0
test -f $filename && filetype="regulare file"
test -d $filename && filetype="directory"
test -r $filename && perm="readable"
test -w $filename && perm="$perm writable"
test -x $filename && perm="$perm executable"
echo "the filename: $filename is a $filetype"
echo "and the permission are : $perm"


#!/bin/bash
#read -p "please input y or n:" sign
#[ "$sign" == "Y" -o "$sign" == "y" ] && echo "OK,continue " && exit 0
#[ "$sign" == "N" -o "$sign" == "n" ] && echo "OH,interrupt" && exit 0
#echo "fuck you" && exit 0

read -p "please rape me :" rape
if [ "$rape" == "Y" ] || [ "$rape" == "y" ]; then
echo "ok , so fuck "
exit 0
fi
if [ "$rape" == "N" ] || [ "$rape" == "n" ]; then
echo "no , interrupt "
exit 0
fi
echo " i don't know whar your choice is " && exit 0 



#!/bin/bash
echo "the script name is  : $0 "
echo "total parameter number is : $# "
[ "$#" -lt 2 ] && echo "the number of parameter is less than 2. stop here." && exit 0
echo "all parameters is : '$@'"
echo "the 1st parameter is : $1 "
echo "the 2nd parameter is : $2 "


#!/bin/bash
echo "total parameter number is ==> $# "
echo "your whole parameter is ==> '$@'"
shift 
echo " total parameter number is ++> $# "
echo " your whole parameter is ++> '$@' "
shift 3
echo " total parameter number is ++> $# "
echo


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值