考试:
知识点会错
题目会很多
考出错点
#!/bin/bash
if [ $1 !="jxcu" ]
then
echo $1
else
echo $2
fi
上面的代码有一个错,你发现了吗?
在Shell脚本中,用空格来区分不同命令,字符连在一起就是一条命令!
细节
练习
1.批量文件改名。
#!/bin/bash
cd /home
for i in text*
do
if [ -f $i ]
then mv ${i} ${i}.txt
fi
done
2.While,逐行读取文件
while read line
do
echo $line >> $1
done</home/text1.txt
读取指定行数
#!/bin/bash
i=0
while read line
do
let i++
if [ $i -el $2 ]
then echo $line >> $1
else echo "end of line"
break
fi
done</home/text1
3.case 语法
case $var in
condition 1)
command
;;
condition 2)
command
;;
*)
command
;;
esac
case 案例
#!/bin/bash
read -p "Enter your name " name
case $name in
root|Root)
echo "Hello root"
;;
shrek)
echo "Hello shreck"
;;
*)
echo "Who are you"
;;
esac