您的位置:首页 > 运维架构 > Shell

shell 练习

2014-10-18 22:31 68 查看
1.用户输入日期2.当前日期对比输入的日期3.计算输入日期减去当前日期的时间
#!/bin/bashread -p "please input the date you demobilizete (YYYYMMDD ex=> 20090422): " date1date_d=$(echo $date1 | grep '[0-9]\{8\}') #正则表达式if [ "$date_d" == "" ]; thenecho "you input the wrong date format......."exit 1fideclare -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" ]; thenecho "you had been demobiliation before : " $((-l*$date_d)) "ago"elsedeclare -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:~/binexport PATH echo -e "you should input 2 numbers, i will cross then! \n" read -p "first number:" firstnumberread -p "second number:" secondnumber total=$(($firstnumber*$secondnumber)) echo -e "\n result of $firstnumber * $secondnumber is ==> $total"

这个有点不懂的地方#!/bin/bashread -p "input your filename:" fileuserfilename=${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/bashecho -e "please input a filename,i will check the filename's type and permission. \n\n"read -p "input a filename:" filenametest -z $filename && echo "you must input a filename " && exit 0test ! -e $filename && echo " the filename '$filename' do not exist" && exit 0test -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 :" rapeif [ "$rape" == "Y" ] || [ "$rape" == "y" ]; thenecho "ok , so fuck "exit 0fiif [ "$rape" == "N" ] || [ "$rape" == "n" ]; thenecho "no , interrupt "exit 0fiecho " i don't know whar your choice is " && exit 0

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

#!/bin/bashecho "total parameter number is ==> $# "echo "your whole parameter is ==> '$@'"shift echo " total parameter number is ++> $# "echo " your whole parameter is ++> '$@' "shift 3echo " total parameter number is ++> $# "echo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: