0

Hi guys I am new in writing bash script.

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.

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. 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

Stuart Siegler
  • 1,686
  • 4
  • 30
  • 38
Harry shah
  • 387
  • 1
  • 3
  • 9

1 Answers1

1

ShellCheck tends to be more explicit than 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.

There are several other problems with the script, but this is the primary one.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • yeah fine but after that it shows line 40: lait++: command not found i have to add = 1 in it – Harry shah Mar 20 '15 at 00:58
  • Technically, OP could lose the `;do` and [I think the loop would work](http://stackoverflow.com/questions/22619510/a-bash-loop-with-braces). I do not actually recommend this, but it's interesting trivia. – kojiro Mar 20 '15 at 01:01
  • @HassanSohail that is a different question, but you can only do arithmetic in an [arithmetic context](http://mywiki.wooledge.org/ArithmeticExpression). – kojiro Mar 20 '15 at 01:02
  • @HassanSohail StackOverflow generally works better when you ask one logical question per posted question. If you're stuck on something new, try a search for [how do I increment a variable in bash](http://google.com/search?q=how+do+I+increment+a+variable+in+bash) and if you don't find anything, ask a new question about that – that other guy Mar 20 '15 at 01:03