1

I have created a script for updating some ipaddress in iptables. Hereby I'm describing the issues which I'm facing with that.

Issues:

  1. Comparison not happening between two variables within a script
  2. At the end of script i need to execute a command ie; service restart/stop etc and output of the commands should be visible while executing the script.

1) Here am comparing two variable strings:

BASE=172.31.0.0
CMD=172.31.1.0

if [[ "$CMD" == "$BASE" ]]; then
   echo "ip are same"
else
   echo "not matched"
fi

but there is no response/output while executing the above script. Here its not comparison is not happening..Kindly suggest a best solution to resolve this issue.

2) after executing the script I need to restart the iptables:

BASE=172.31.0.0
CMD=172.31.1.0

if [[ "$CMD" == "$BASE" ]]; then
   echo "ip are same"
else
   echo "not matched"
fi

service iptables restart
iptables -nvL

A script should display the output of the last two lines (commands). Kindly suggest me the best solution and how to do this in a best way.

7
  • 2
    Presentation award for this post. good luck Commented Feb 13, 2013 at 12:35
  • 4
    So ... exactly what shell are you using? bash, or sh? You know they're different, right? Because as-is, this script works in bash and produces an error in sh. Neither of which are what you're reporting. Commented Feb 13, 2013 at 12:45
  • Thanks for your reply, Am using bash script and I am trying to execute this script by ./script.sh. Commented Feb 13, 2013 at 14:37
  • You mean that you are getting no output and no error message? That sounds weird. Commented Feb 13, 2013 at 14:41
  • 2
    @user1565390 Add #!/bin/bash to the first line of your script, just to assert it will be executed with bash when you do ./script.sh. Commented Feb 13, 2013 at 16:14

1 Answer 1

1

That's very odd. This should work, so if it's not working you forgot to mention something important.

How is this script being executed? Do you simply type ./script or is it executed by some service (like cron)?

Here are some of suggestions to debug:

  1. Sanity check: see if bash works (perhaps your login shell isn't bash, so you didn't notice). Run this at the terminal:

    /bin/bash -c 'echo hello world'
    
  2. It prints hello world, right? How about this:

    /bin/bash -c 'BASE=172.31.0.0; CMD=172.31.1.0; if [[ "$CMD" == "$BASE" ]]; then echo "ip are same"; else echo "not matched"; fi'
    

    If any of the above doesn't work, you have a problem with your bash installation.

  3. Instead of executing your script with ./script.sh, run it like this:

    /bin/bash script.sh
    
  4. Nothing? Run this:

    file script.sh
    

    If it ends with something like "with CRLF line terminators", then cdarke nailed it: the file was created on Windows with an improper tool. Recreate it on Linux or use dos2unix. But anyway, I doubt it because with a CRLF-ending file I get this printed:

    bash: ./script.sh: /bin/bash^M: bad interpreter: No such file or directory
    

    Instead of nothing at all.

  5. Put those this line on the beginning of the file:

    set -x
    

    (below #!/bin/bash, if you have it). This ensures a debugging trace will be printed, showing each command as it is executed.

  6. If still there is nothing shown.. put this at your script (below set -x if you put it):

    touch /tmp/hi-this-is-strange
    

    Then check if there is a /tmp/hi-this-is-strange file after you run the script.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your timely reply...Can you please let me know how to remove "." within the ip-address... eg) 172.31.0.0 then by awk i need to get output as 1723100 how to do this by using "awk" tool.. Thanks in advance.
I think sed is more appropriate for this task: echo 172.31.0.0 | sed 's/\.//g'
@user1565390 Was any of of the points of my reply useful? I still don't understand what was your problem (and feel free to edit your question to add more info), but if my response solved it please accept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.