文件结尾语法错误 shell 脚本

文件结尾语法错误 shell 脚本
#!/bin/bash

source conf.ini

inotifywait -m -e create /$1path |
  while read file; do

if ($(echo $word | head -c 1)"=$1 then
    echo $file
cd $1inputpath
ls -l |grep $file*
zcat $file* > /tmp/$file
sort /tmp/$file > /tmp/$file.sorted
cd $1outputpath
 ls -l |grep $file
sort  $file > /tmp/$file.origsorted
diff /tmp/$file.origsorted  /tmp/$file.sorted
 if [ $? -eq 0 ];
 then
   echo OK
else
echo FAIL
echo $file
fi

fi
      done

我收到以下错误:en do fthe file:

./FoldersegCompare: line 8: unexpected EOF while looking for matching `"'
./FoldersegCompare: line 32: syntax error: unexpected end of file

答案1

我不知道您想用这个脚本做什么,但第一次看它时,我发现错误是由于这一行引起的:

if ($(echo $word | head -c 1)"=$1 then

将其设为:

if [ "$(echo $word | head -c 1)" = "$1" ]; then

if-then使用条件构造进行检查时,请使用test( [) 或[[( bash-ism)。查看help test更多信息。

当你这样做时if ($(echo $word | head -c 1)"=$1 then

  • (之后if"之前都有语法错误=,也许你的意思是"$(echo $word | head -c 1)"

  • 此外,由于您没有使用test(或[[),因此=只是建议您进行变量赋值。因此您需要[test)或[[来确保您正在比较事物

  • 另外,你错过了一个;之前then,这是换行符的简写(或者你可以放在then下一行)

另外,从您完成任务(我不知道是什么)所用的命令来看,您的脚本对我来说似乎效率不高(说实话)。也许您应该浏览一下这里和其他网站上发布的脚本答案,了解在什么情况下应该使用哪种工具,提出一个新问题,看看其他人建议如何解决您的问题,当然还要使用缩进。

答案2

纠正以下问题并再次检查脚本这里或者查看下面的修正

   1  #!/bin/bash
   2  
   3  source conf.ini
   4  
   5  inotifywait -m -e create /$1path |
   6    while read file; do
   7  
   8  if ($(echo $word | head -c 1)"=$1 then
          ^––SC1009 The mentioned parser error was in this simple command.
                                   ^––SC1073 Couldn't parse this double quoted string.
   9      echo $file
  10  cd $1inputpath
  11  ls -l |grep $file*
  12  zcat $file* > /tmp/$file
  13  sort /tmp/$file > /tmp/$file.sorted
  14  cd $1outputpath
  15   ls -l |grep $file
  16  sort  $file > /tmp/$file.origsorted
  17  diff /tmp/$file.origsorted  /tmp/$file.sorted
  18   if [ $? -eq 0 ];
  19   then
  20     echo OK
  21  else
  22  echo FAIL
  23  echo $file
  24  fi
  25  
  26  fi
  27        done
      ^––SC1072 Expected end of double quoted string. Fix any mentioned problems and try again.

例如

#!/bin/bash

source conf.ini

inotifywait -m -e create /"$1path" |
while read -r file; do
  if ("$(echo "$word" | head -c 1)"="$1"); then
    echo "$file"
    cd "$1inputpath" || exit
    find . -maxdepth 1 -type f -name "$file"
    zcat "$file"* > /tmp/"$file"
    sort /tmp/"$file" > /tmp/"$file".sorted
    cd "$1outputpath" || exit
    find . -maxdepth 1 -type f -name "$file"
    sort  "$file" > /tmp/"$file".origsorted
    diff /tmp/"$file".origsorted  /tmp/"$file".sorted
    if [ $? -eq 0 ];
    then
      echo OK
    else
      echo FAIL
      echo "$file"
    fi
  fi
done

如果你得到这样的结果

./FoldersegCompare: line 5: inotifywait: command not found

只需安装

sudo apt-get install inotify-tools

相关内容