shell命令代碼
1. ==萬年歷==
題目和思路:
輸入查詢年份2016,再輸入查詢?cè)路?
1>1990.1.1星期一
2>查詢范圍1990.1.1之后查詢
3>閏年判斷條件(year%4==0&&year%100!=0)||(year%400==0)
思路:計(jì)算1990.1.1~2016.8.31所有的天數(shù)
十月 2016
日 一 二 三 四 五 六
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
2.
1>輸入方式:
(1)mycal 默認(rèn)的當(dāng)月月歷
(2)mycal 2016 整個(gè)年份
(3)mycal 2016 10
1.判斷輸入的參數(shù)的個(gè)數(shù)
0:
1:以后開發(fā)
2:
寫一個(gè)函數(shù)
2.具體考慮
0:year=`date +%Y` month=`date +%m`
2:year=$1 month=$2
3.考慮計(jì)算實(shí)際天數(shù)
1990.1.1~2016.10.1
(1)是算1990.1.1~2015.12.31
(2)2016.1.1~2016.9.30
calWhat()
{
year=$1
month=$2
if [ $year -lt 1990 ] || [ $month -lt 1 ] || [ $month -gt 12 ]
then
echo "時(shí)間不符..."
else
i=1990
allDay=0
array=(0 31 28 31 30 31 30 31 31 30 31 30 31)
while [ $i -lt $year ]
do
if [ `expr $i % 4` == 0 -a `expr $i % 100` != 0 ] || [ `expr $i % 400` == 0 ]
then
let allDay+=366
else
let allDay+=365
fi
let i++
done
if [ `expr $year % 4` == 0 -a `expr $year % 100` != 0 ] || [ `expr $year % 400` == 0 ]
then
array[2]=29
else
array[2]=28
fi
i=1
while [ $i -lt $month ]
do
let allDay+=array[i]
let i++
done
let week=allDay%7
let week=(week+1)%7 #當(dāng)月的第一天
echo -e "\\t\\t\\t${year}/${month}"
echo -e "日\\t一\\t二\\t三\\t四\\t五\\t六"
colum=0
i=1
while [ $i -le $week ]
do
printf " \\t"
let i++
let colum++
done
i=1
while [ $i -le ${array[$month]} ]
do
printf "${i}\\t"
let colum++
if [ `expr $colum % 7` == 0 ]
then
printf "\\n"
fi
let i++
done
printf "\\n"
fi
}
if [ $# -eq 0 ]
then
argument1=`date +%Y`
argument2=`date +%m`
calWhat ${argument1} ${argument2}
elif [ $# -eq 1 ]
then
echo "盡情期待..."
elif [ $# -eq 2 ]
then
argument1=$1
argument2=$2
calWhat ${argument1} ${argument2}
else
echo "參數(shù)錯(cuò)誤"
fi
2.自動(dòng)產(chǎn)生當(dāng)天的文件夾屹堰,供每天寫代碼使用
/*
1.自動(dòng)產(chǎn)生當(dāng)天的文件夾,供每天寫代碼使用
file=`date +%Y/%m%d`
echo "${file}"
~/practice/2016/10/1012
思路:
1>判斷參數(shù)個(gè)數(shù)
xx.sh
xx.sh -y
xx.sh -m
xx.sh -d
2>
0:-d
1:
>1:提示錯(cuò)誤
3>如果參數(shù)是一個(gè)
-y
-m
-d
year=`date +%Y`
month=`date +%m`
day=`date +%m%d`
if [ $# -eq 0 ]
then
if [ -e ~/practice/$year/$month/$day ]
then
echo "${day}文件已存在"
else
mkdir -p ~/practice/$year/$month/$day
fi
cd ~/practice/$year/$month/$day
elif [ $# -eq 1 ]
then
case $1 in
"-y")
if [ -e ~/practice/$year ]
then
echo "${year}文件已存在"
else
mkdir -p ~/practice/$year
fi
cd ~/practice/$year
;;
"-m")
if [ -e ~/practice/$year/$month ]
then
echo "${month}文件已存在"
else
mkdir -p ~/practice/$year/$month
fi
cd ~/practice/$year/$month
;;
"-d")
if [ -e ~/practice/$year/$month/$day ]
then
echo "${day}文件已存在"
else
mkdir -p ~/practice/$year/$month/$day
fi
cd ~/practice/$year/$month/$day
;;
*)
printf "參數(shù)?"
printf "用法[-y] [-m] [-d]\\n"
;;
esac
else
echo "參數(shù)過多"
fi
3. 練習(xí)1:求1~100偶數(shù)和
#### 練習(xí)1:求1~100偶數(shù)和
求1~100偶數(shù)和
1 sum=0
2 i=1
3 while [ $i -le 100 ]
4 do
5 if [ `expr $i % 2` -eq 0 ]
6 then
7 let sum=sum+i
8 fi
9 let i++
10 done
11 echo "sum=${sum}"
12 echo "i=${i}"
3. 打印9X9乘法表
1X1=1
1X2=2 2X2=4
….
1X9=9 2X9=18 ….9X9=81
找規(guī)律:
1行1
2行2
n行n
1 row=1
2 colum=1
3 while [ $row -le 9 ]
4 do
5 colum=1
6 while [ $colum -le $row ]
7 do
8 let value=colum*row
9 printf "%dX%d=%02d " "$colum" "$row" "$value"
10 let colum++
11 done
12 printf "\\n"
13 let row++
14 done
4. 打印
*
***
*****
*******
row=1
while [ $row -le 4 ]
do
space=1
while [ $space -le $((4-row)) ]
do
printf " "
let space++
done
star=1
while [ $star -le $((2*row-1)) ]
do
printf "*"
let star++
done
let row++
printf "\\n"
done
5../xx.sh
$0:代表執(zhí)行的文件./xx.sh
$1:代表執(zhí)行的第一個(gè)參數(shù)
$2:代表執(zhí)行的第二個(gè)參數(shù)
......
$n:代表執(zhí)行的第n個(gè)參數(shù)
$#:代表執(zhí)行參數(shù)的個(gè)數(shù)书闸,不包括命令本身
0.判斷是否有參數(shù)咖杂,參數(shù)是否為1
1.判斷文件是否存在
2.如果存在,判斷文件是否為普通文件
3.改權(quán)限
4.運(yùn)行
5.讓這個(gè)命令在全局有效
chmod +x mysh
sudo cp mysh /bin/
if [ $# -eq 0 ]
then
echo "do what?"
elif [ $# -eq 1 ]
then
if [ -e $1 ]
then
if [ -f $1 ]
then
chmod +x $1
./$1
else
echo "$1 is not file"
fi
else
echo "$1:No such file or directory"
fi
else
echo "too much arguments"
fi
最后編輯于 :2017.12.04 05:30:54
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者