随便找了一个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

本文介绍两种使用Shell脚本从文件中提取并打印第10行内容的方法。第一种方法通过循环逐行读取文件内容,第二种方法则利用sed命令实现更高效简洁的操作。
141

被折叠的 条评论
为什么被折叠?



