电商专业学习Linux第五天

经过约5天的时间,老师就将shell脚本讲完了,明天要正式讲C但是我仍然是一脸懵逼,不知所措。

经过这五天的接触也能感觉到,只有多多练习才有可能敲出来自己的东西,自己虽然从早到晚也在跟着老师敲命令,但是效率低下,也想快速提高效率。

今天老师主要给我们演示了三段命令,只有第三段的sed -i ‘s/old/new/g’ xx.txt是一个新命令,用来替换已有文件内的内容的,其他额度都是之前讲到过,我自己就是不能够把他们串到一起,甚至写出单个命令都有困难。

这是一段“万年历”命令,但是只能显示本月1月份的,输入其他月份会出错

printCal
{
year=$1
month=$2
allDay=0
i=1990
array=(0 31 0 31 30 31 30 31 31 30 31 30 31)
while [ $i -lt $year ]
do
if [[ `expr $i % 4` == 0 && $((i%100)) != 0 ]] || [ `expr $i % 400` == 0 ]
then
let allDay=allDay+366
else
let allDay=allDay+365
fi
let i++
done
if [[ `expr $year % 4` == 0 && $((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=allDa+array[i]
let i++
done
let allDay=allDay+1
#week是记录星期几
let week=allDay%7
echo “=====${year}–${month}=====”
printf “日\t一\t二\t三\t四\t五\t六\n”
blank=1
colum=0
while [ $blank -le $week ]
do
printf ” \t”
let blank++
let colum++
done
i=1
while [ $i -le ${array[$month]} ]
do
printf “$i\t”
let i++
let colum++
if [ `expr $colum % 7` == 0 ]
then
printf “\n”
fi
done
if [ `expr $colum % 7` != 0 ]
then
printf “\n”
fi
}
if [ $# -eq 0 ]
then
year=`date +%Y`
month=`date +%m`
elif [ $# -eq 1 ]
then
echo “软件正在完善中…”
elif [ $# -eq 2 ]
then
year=$1
month=$2
else
echo “参数过多!!!”
fi
printCal $year $month

这是老师带我们写的一段vi命令,据说这个命令对以后写c命令有帮助,不明觉厉,

fileName=$1
year=`date +%Y`
month=`date +%m`
day=`date +%d`
createTime=”$year\/$month\/$day”
if [ $# -eq 0 ]
then
echo “请输入文件名!!!”
elif [ $# -eq 1 ]
then
if [ -d ./${fileName} ]
then
echo “该文件是目录文件!!!”
else
length=${#fileName}
let start=length-2
suffix=${fileName:start:2}
if [ $suffix = “.c” ]
then
if [ -e ./${fileName} ]
then
currentTime=`date +%k:%M:%S`
lastChangeTime=”lastChangeTime:${createTime}-${currentTime}”
sed -i “” “s/lastChangeTime\.*/${lastChangeTime}/g” ./${fileName}   #一般-i后面的“”双引号可以去掉
vi ./${fileName}
else
cp ~/prepare.txt ./prepare.c
mv prepare.c ${fileName}
sed -i “” “s/fileName/${fileName}/g” ./${fileName}            #一般-i后面的“”双引号可以去掉
sed -i “” “s/createTime/${createTime}/g” ./${fileName}      #一般-i后面的“”双引号可以去掉
sed -i “” “s/lastTime/${createTime}/g” ./${fileName}            #一般-i后面的“”双引号可以去掉
currentTime=`date +%k:%M:%S`
lastChangeTime=”lastChangeTime:${createTime}-${currentTime}”
sed -i “” “s/lastChangeTime\.*/${lastChangeTime}/g” ./${fileName}    #一般-i后面的“”双引号可以去掉
vi ./${fileName}
fi
fi
fi
else
echo “参数过多!!!”
fi

之前有位朋友提到过让我在笔记中做注释,今天仔细一看,不是不写,而是不知道怎么写,写什么,如果一定需要注释,我只好给自己注释:所有命令再敲个十几二十遍,最后还要能换着花样敲,才算合格,因为我实在不知该如何应用,而且明天就是C了,这些内容只能课后抽空自己练习。

    原文作者:雨打梨花闭门寒
    原文地址: https://www.jianshu.com/p/30b57c0f418b
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞