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

【leetcode-shell】Tenth Line

2017-06-21 15:01 197 查看
随便找了一个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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: