Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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
String 为什么在i';我在比较数组中的字符串?_String_Bash_Compare - Fatal编程技术网

String 为什么在i';我在比较数组中的字符串?

String 为什么在i';我在比较数组中的字符串?,string,bash,compare,String,Bash,Compare,我的示例代码在这里 #!/bin/bash file="output2.txt" numbers="$(cut -d',' -f2 output2.txt)" lines="$(cut -f2 output2.txt)" hours="$(cut -d',' -f1 output2.txt)" array_numbers=( $numbers ) lines_array=( $lines ) hours_array=( $hours ) difference=$1 let range=$1-10

我的示例代码在这里

#!/bin/bash
file="output2.txt"
numbers="$(cut -d',' -f2 output2.txt)"
lines="$(cut -f2 output2.txt)"
hours="$(cut -d',' -f1 output2.txt)"
array_numbers=( $numbers )
lines_array=( $lines )
hours_array=( $hours )
difference=$1
let range=$1-1000
for (( i = 0 ; i < ${#array_numbers[@]} ; i++ )) 
do
let num=$(( 10#${array_numbers[$i+1]} - 10#${array_numbers[$i]} ))
   if [ $num -gt $1 ]
     then
       echo ${lines_array[$i+1]} "and" ${lines_array[$i]} "has a difference more than $1"
   elif [ $num -ge 0 ] && [ $num -lt $range ] 
     then
       echo ${lines_array[$i+1]} "and" ${lines_array[$i]} "has a difference more than $1"
   elif [ $num -le $1 ]
     then
       if [${hours_array[$i+1]} != ${hours_array[$i]}]
       then
         echo ${lines_array[$i+1]} "and" ${lines_array[$i]} "has a difference more than one second"
       fi
  fi
done
我想做的是把差值作为参数,如果差值超过100毫秒,我想打印输出。零件

for (( i = 0 ; i < ${#array_numbers[@]} ; i++ )) 
    do
    let num=$(( 10#${array_numbers[$i+1]} - 10#${array_numbers[$i]} ))
          if [ $num -gt $1 ]
              then
               echo ${lines_array[$i+1]} "and" ${lines_array[$i]} "has a difference more than $1"
          elif [ $num -ge 0 ] && [ $num -lt $range ] 
              then
               echo ${lines_array[$i+1]} "and" ${lines_array[$i]} "has a difference more than $1"
它不会打印任何内容,因此我将最后一条elif语句放在代码中,但即使它打印小时数数组,也很好,在比较它们时,它不起作用。输出总是:

script.sh:第22行:[12:43:00:未找到命令

为什么它不接受这个比较,或者问题是关于我的bash版本


提前感谢您的帮助。

[
[
之后缺少空格是一个命令,因此需要将其与其参数分开

if [ ${hours_array[$i+1]} != ${hours_array[$i]} ]
这里需要一个空格(
[
是一个命令)


[
前后添加空格。它是
测试
内置命令的“别名”

您还应该在变量周围添加双引号
。因为如果它们是空的,bash将不会将它们识别为空单词

我通常使用双括号
[[
作为测试条件,这样更安全,更具特色

例如:

if [[ "${hours_array[$i+1]}" != "${hours_array[$i]}" ]]

我发现在这段代码中几乎没有什么东西可以更改

  • [
    之后和
    ]
    之前添加空格
  • 添加双引号,以便在变量为空的情况下,脚本不会抛出错误

    if [ "${hours_array[$i+1]}" != "${hours_array[$i]}" ]
    
  • 另外,当您到达最后一行时,
    $i+1
    将失败。因此,下面的内容会更好

    for (( i = 0 ; i < ${#array_numbers[@]} - 1 ; i++ ))
    
    ((i=0;i<${数组数[@]}-1;i++)的
    
    

谢谢你的帮助:)有趣的是,阅读代码所花的时间比在这里发布带格式的问题要少…(特别是因为你…:我已经知道这一点,但正如我所说的,有时失明对我们来说是一个大问题(!!):DAlso引用安全:
如果[“${hours\u数组[$I+1]}”!=“${hours\u数组[$I]}”“]
;这样一来,结束会产生错误的比较,而不是让
测试
抱怨语法错误。很好的建议,但有些多余——使用
[[[]]
会使引号变得不必要,至少在左边是这样。
if [[ "${hours_array[$i+1]}" != "${hours_array[$i]}" ]]
if [ "${hours_array[$i+1]}" != "${hours_array[$i]}" ]
for (( i = 0 ; i < ${#array_numbers[@]} - 1 ; i++ ))