Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
bash算术表达式中的语法错误不一致:(错误标记为“integer1-integer2”)_Bash_Shell - Fatal编程技术网

bash算术表达式中的语法错误不一致:(错误标记为“integer1-integer2”)

bash算术表达式中的语法错误不一致:(错误标记为“integer1-integer2”),bash,shell,Bash,Shell,我已经编写了一个脚本来对一些大型平面文件进行子集,并将整数个子集(即$increment)作为用户输入变量。只有当这个输入参数是奇数整数时,我才观察到一个奇怪的行为(bash语法错误)。为了(稍微)提高清晰度,我在原始shell脚本的精简版本中复制了这个错误行为。我无法提供平面文件,但希望具有shell/bash专业知识的人能够通过查看代码和错误消息来诊断这里发生了什么(我已经运行了这个过程,没有发现任何严重的问题) 当$increment设置为偶数整数(例如8)时,shell脚本将无错误地执行

我已经编写了一个脚本来对一些大型平面文件进行子集,并将整数个子集(即$increment)作为用户输入变量。只有当这个输入参数是奇数整数时,我才观察到一个奇怪的行为(bash语法错误)。为了(稍微)提高清晰度,我在原始shell脚本的精简版本中复制了这个错误行为。我无法提供平面文件,但希望具有shell/bash专业知识的人能够通过查看代码和错误消息来诊断这里发生了什么(我已经运行了这个过程,没有发现任何严重的问题)

当$increment设置为偶数整数(例如8)时,shell脚本将无错误地执行,并为while循环的每次迭代输出所需的打印语句(请参见下面的“注意”)。下面是这些打印语句的一些示例输出:

Line of interest: span2=84688
Line of interest: span2=85225
Line of interest: span2=86323
...
但是,当$increment为奇数时(例如9),脚本在第48行“span2=$($line2-$last2))”处失败,错误语句为:

test_case.sh: line 48: 153026
153027-77419: syntax error in expression (error token is "153027-77419")
这很奇怪,因为前面的echo print语句输出“Line of interest:span2=75278”表明算术表达式在子shell中计算时没有错误,正好在失败的行之前。很明显,这里要减去的整数没有什么特别之处,但奇怪的是,例如,当表达式参数$line2等于“153027”时,当它输出“153026”时,错误消息显示为关闭1。不过,我不确定这是否/如何与语法错误相关

#!/bin/bash
set -e


increment=9
file1="path/to/file1"
file2="path/to/file2"
file3="path/to/file3"

# End index of header in first file
file1_start=2138
midpoint=$(( $file1_start + 1 ))

file1_wc=($(wc $file1))
file2_wc=($(wc $file2))
file3_wc=($(wc $file3))

# Get a line count for the three different flat text files, as an upper bound index
ceil1=${file1_wc[0]}
ceil2=${file2_wc[0]}
ceil3=${file3_wc[0]}

# Initialize end point indices
line="$(head -$midpoint $file1 | tail -1 | awk '{print $1;}')"
line2=$(grep -n -e "$line" $file2 | cut -f1 -d:)
line3=$(grep -n -e "$line" $file3 | cut -f1 -d:)

# Initialize starting point indices
last1=$midpoint
last2=$line2
last3=$line3

# Update "midpoint" index
midpoint=$(($midpoint+$ceil1/$increment))

while [ $midpoint -lt $ceil1 ]
do

 line="$(head -$midpoint $file1 | tail -1 | awk '{print $1;}')"
 line2=$(grep -n -e "$line" $file2 | cut -f1 -d:)
 line3=$(grep -n -e "$line" $file3 | cut -f1 -d:)

 # Calculate range of indices for subset number $increment
 span1=$(($midpoint-$last1))

 echo "Line of interest: span2=$(($line2-$last2))"
 # ***NOTE***: The below statement is where it is failing for odd $increment
 span2=$(($line2-$last2))

 span3=$(($line3-$last3))

 # Set index variables for next iteration of file traversal
 index=$(($index+1))
 last1=$midpoint
 last2=$line2
 last3=$line3

 # Increment midpoint index variable
 midpoint=$(($midpoint+$ceil1/$increment))

done
非常感谢您的反馈,提前感谢


更新:通过添加“set-x”并查看调用堆栈,我确定表达式

 line2=$(grep -n -e "$line" $file2 | cut -f1 -d:)
 line2=$(grep -n -e "$line" $file2 | cut -f1 -d:)
正在变灰不止一行。因此,在我上面提供的示例中,$line2等于“153026\n153027”,并且不是减法的合理参数,因此语法错误。解决此问题的一种方法是将管道连接到封头,例如

 line2=$(grep -n -e "$line" $file2 | cut -f1 -d: | head -1)
 line2=$(grep -n -e "$line" $file2 | cut -f1 -d: | head -1)

只考虑GRP.< /P> < P > nCeMAMI产生的第一行:通过添加“SET-X”并查看调用堆栈,确定表达式

 line2=$(grep -n -e "$line" $file2 | cut -f1 -d:)
 line2=$(grep -n -e "$line" $file2 | cut -f1 -d:)
正在变灰不止一行。因此,在我上面提供的示例中,$line2等于“153026\n153027”,并且不是减法的合理参数,因此语法错误。解决此问题的一种方法是将管道连接到封头,例如

 line2=$(grep -n -e "$line" $file2 | cut -f1 -d: | head -1)
 line2=$(grep -n -e "$line" $file2 | cut -f1 -d: | head -1)

<> >只考虑GRP.< /P>产生的第一行,你试着在代码中放<代码> SET-X/CODE >,这样你就可以看到它的执行了吗?这是调试shell脚本的常用方法。检查错误行中是否有非打印字符确保使用bash运行它:
bash your_script.sh
。你能展示你的bash版本吗
bash--version
dos2unix myScriptName
?修复了S.O.:-)上的1/2神秘问题。祝你好运。你可以回答你自己的问题。我只是给了你调试提示,我没有找到解决方案。谢谢!你的回答让我意识到我的一个价值观包含了一个额外的空间,不适合比较。