随便找了一个shell脚本的题目。
打印出第十行的内容。
解法一:循环,不过效率最差,排在了最后
# Read from the file file.txt and output the tenth line to stdout.
#!/bin/bash
declare count=0
cat file.txt | while read line
do
if [ $count = 9 ];then
echo $line
exit 0
fi
let count=$count+1
done
解法二:sed,最快速,也最简洁
# Read from the file file.txt and output the tenth line to stdout.
#!/bin/bash
sed -n 10p file.txt