简体   繁体   English

for循环在bash中是否存在其他条件

[英]for loop in if else conditions in bash

Hi guys I am new in writing bash script. 嗨,大家好,我是写bash脚本的新手。

I have a task to do. 我有任务要做 I have a file in which node name and ips are updated, I have to make each file of those nodes which are not in our required directory and those are in updated file and also editing their name. 我有一个文件,其中节点名称和ips已更新,我必须制作不在我们所需目录中的那些节点的每个文件,并且在更新文件中的那些节点也要编辑它们的名称。

I have to give input from bottom to upward means from last line to upward and my script will run from bottom to upward according to my need means those entries which are missing in my required directory. 我必须从下至上给出输入,从最后一行到上进,脚本将根据需要从下到上运行,这意味着在所需目录中缺少的那些条目。

I use if else condition and have to place for loop to do my task until it equals. 我使用if else条件,必须放置for循环来执行我的任务,直到相等为止。 My script is 我的剧本是

!/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

but i am getting this error 但我收到此错误

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

Description of my script 我的脚本的描述

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

any idea about syntax of for loop 关于for循环语法的任何想法

ShellCheck tends to be more explicit than bash . ShellCheck往往比bash更明确。 In this case, it says 在这种情况下,它说

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

And indeed, you don't have a done to terminate the loop. 实际上,您还没有done终止循环的工作。

There are several other problems with the script, but this is the primary one. 该脚本还有其他几个问题,但这是主要的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM