leetcode-shell-195. Tenth Line

来源:互联网 发布:软件著作是什么意思 编辑:程序博客网 时间:2024/06/05 19:17
How would you print just the 10th line of a file?

For example, assume that file.txt has the following content:

Line 1Line 2Line 3Line 4Line 5Line 6Line 7Line 8Line 9Line 10
Your script should output the tenth line, which is:
Line 10
题意:输出txt文件的第10行
思路:声明一个变量,记录行数,当行数=10的时候,输出当前行
最终shell脚本:
#! /bin/sh
filename=file.txt
i=1
cat $filename |while read line
do
if [ $i = 10 ] ;then
echo $line
fi
i=$(($i+1))
done
需要值得注意的是:写if判断语句的时候,注意空格。[空格$i空格=空格10空格]空格;
在shell脚本中变量自增不能使用i++这种方式,要$i取出值+1再变成值(前加$号)赋值给变量,$(($i+1))
0 0
原创粉丝点击