-1

Hi guys I am new in writing bash script.

I have a task to do. I have a file in which node name and IP addresses 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 value 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 address as an input from a file

  8. if condition

any idea about syntax of the for loop?

3

Among all the other things, you do not have a done terminating the for-loop, between the } and the fi. And you don't need to have the curly brackets in this context.

The ++ operator in bash requires arthmatic expansion, so you would need to use ((lait++)).

The $nagiosdir -eq $lbdir would compare integers so using strings gives an error:

$ test hi -eq hi
-bash: test: hi: integer expression

You would want to use $nagiosdir = $lbdir instead.

There are other things, but these would be the syntax errors in the script.

1

Terminate the for-loop by a done. (And you can remove those curly brackets in that for loop.)

0

I don't know about your script , but i debug your script:

#!/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" = "$lbdir" ] ; then
echo " Nothing to do "
else
  if [ "$giosdir" <= "$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++;           
   done;
  fi;
fi;
  • yes guys its working great – Hassan Sohail Mar 20 '15 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.