【问题标题】:for loop in if else conditions in bashbash中的if else条件中的for循环
【发布时间】:2015-03-20 00:27:47
【问题描述】:

大家好,我是编写 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循环语法的任何想法

【问题讨论】:

    标签: linux bash shell


    【解决方案1】:

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

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

    确实,您没有done 来终止循环。

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

    【讨论】:

    • 是的,但之后它显示第 40 行:lait++: command not found 我必须在其中添加 = 1
    • 从技术上讲,OP 可能会丢失 ;doI think the loop would work。我实际上并不推荐这个,但这是有趣的琐事。
    • @HassanSohail 这是一个不同的问题,但您只能在 arithmetic context 中进行算术运算。
    • @HassanSohail StackOverflow 通常在您针对每个发布的问题提出一个逻辑问题时效果更好。如果您遇到新问题,请尝试搜索 how do I increment a variable in bash,如果您没有找到任何内容,请提出新问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 2017-08-02
    • 2019-12-04
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多