在bash中if else条件中的for循环

时间:2015-03-20 00:27:48

标签: linux bash shell

大家好,我是新手写的bash脚本。

我有一项任务要做。我有一个文件,其中更新了节点名称和ips,我必须将那些不在我们所需目录中的节点的每个文件都放在更新的文件中并编辑它们的名称。

我必须从下到上给出输入意味着从最后一行到上一行,我的脚本将根据我的需要从下到上运行,这意味着我所需目录中缺少的那些条目。

我使用if else条件并且必须放置for循环来完成我的任务,直到它等于。我的脚本是

!/bin/bash

set -x

giosdir=$(find /usr/local/example-dir -maxdepth 1 -type f | wc -l)

lbdir=$(more /root/scripts/servers/new/example.txt |wc -l)

count=$(($lbdir-$giosdir))

lait2=1

l2=$(awk '{print $3}' < /root/scripts/servers/new/example.txt | tail -$lait2)

lait=1

newip=$(awk '{print $1}' < /root/scripts/servers/new/example.txt | tail -$lait)

if [ $nagiosdir -eq $lbdir ] ; then

echo " Nothing to do "

else

  if [ $giosdir -lt $lbdir ] ; then


   for((i=0;i<count;i++));do

    {


  cd /usr/local/

  cp example-Node-2.txt   $l2.txt

  sed -i 's/10.10.0.1/'$newip'/' $l2.txt

  sed -i 's/examole-Node-2.txt/'$l2'/' $l2.txt

  echo " Node is added successfull"

  lait2++;
   lait++;           

     }

  fi
fi

但我收到此错误

line 43: syntax error near unexpected token fi' 
line 43: fi '

我的剧本描述

1 first line is taking input from a directory that how many number
of files are there.

2 This line is taking input from a file that how many lines are
there

3 subtracting the numbers and the value would be an integer

4 declaring variable valu which is use in next line

5 this line taking input from a file and cut the 3rd column in which
nodes name are save

6 also a variable

7 taking an ip as an input from a file

8 if condition

关于for循环

的语法的任何想法

1 个答案:

答案 0 :(得分:1)

ShellCheck往往比bash更明确。在这种情况下,它说

for((i=0;i<count;i++));do
                       ^-- Couldn't find 'done' for this 'do'

事实上,你没有done来终止循环。

脚本还有其他几个问题,但这是主要问题。