您尚未登录。

#1 2020-05-31 11:50:22

nsfoxer
会员
注册时间: 2020-04-17
帖子: 20

shell脚本if不进入判断

原本想要批量修改一个文件,跳过其中的4-12行,却一直不跳过。让人很纠结。代码如下

#!/bin/sh

echo $1
read  -p "回车继续"

file=${1}.bak
# weapon=${1%%.*}
linenum=1
while read line
do
         if [[ $linenum > 3 ]]; then
               if [[ $linenum < 13 ]]; then
                       echo "跳过第$linenum行"
                       linenum=$(($linenum+1))
                       continue
               fi
         fi

         linenum=$(($linenum+1))
done < $1

读取一个任意文件,从未进入4-12行的判断,希望可以帮助看一下问题
bash 版本5.0.16(1)-release (x86_64-pc-linux-gnu),在centos下,bash版本4.2.46(2)-release (x86_64-redhat-linux-gnu)一样出现问题,可能是语法错误?

离线

#2 2020-05-31 11:59:40

依云
会员
所在地: a.k.a. 百合仙子
注册时间: 2011-08-21
帖子: 8,384
个人网站

Re: shell脚本if不进入判断

shellcheck 了解一下?

离线

#3 2020-05-31 12:29:40

nsfoxer
会员
注册时间: 2020-04-17
帖子: 20

Re: shell脚本if不进入判断

以前并不知道,感谢大佬

In 1.sh line 11:
        if [[ $linenum > 3 ]]; then
                       ^-- SC2071: > is for string comparisons. Use -gt instead.


In 1.sh line 12:
                if [[ $linenum < 13 ]]; then
                               ^-- SC2071: < is for string comparisons. Use -lt instead.

离线

页脚