1

I have a set of piped commands that work on the command line, but do not produce output when run within a script.

The command is:

STRNG=$( ip mroute show | tr -d "()," | awk ' {print "/usr/sbin/smcroute -a eth3", $1, $2, "vtun0 vtun1"}' ); echo "$STRNG"`

And the output is:

/usr/sbin/smcroute -a eth3 192.0.1.19 224.1.1.1 vtun0 vtun1
/usr/sbin/smcroute -a eth3 192.0.1.18 224.1.1.1 vtun0 vtun1

However, if I put the very same command line into a script, I get no output from the echo "$STRNG" command.

What I'm trying to do is execute every line in $STRNG as a command, but for whatever reason it appears $STRNG doesn't contain any text in the script, whereas $STRNG does contain text when run from the command line. I'm sure this is due to limited bash understanding.

Could someone help me with this?

2
  • what does the script look like? (the first line, at least) Commented Jul 21, 2010 at 19:01
  • 6
    try to run it with -x : /bin/bash -x your_script.sh It will dump debug informations. Commented Jul 21, 2010 at 19:03

4 Answers 4

1

Is one of the commands in your pipeline an alias? If so, you'll need to do

shopt -s expand_aliases

in order for bash to expand it in your script....generally this is only enabled by default in interactive shells.

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

Comments

0

As Scharron said, run it with sh -x. Other than that:

  • Is ip in the script's $PATH?
  • Do you use a different locale ($LANG or $LC_???) in your script, so the output of commands gets translated to another language?

Comments

0

I would break it into smaller pieces to debug it.

I.e., first have a script that does this:

ip mroute show

Run the script. If that produces output then tack on more.

ip mroute show | tr -d "(),"

ip mroute show | tr -d "()," | awk ' {print $0 } '

ip mroute show | tr -d "()," | awk ' {print "/usr/sbin/smcroute -a eth3", $1, $2, "vtun0 vtun1"}'

Comments

0

I want to thank everyone for their help - these will be good things to look for in the future.

The first line of my script was different from the command line:

/usr/sbin/smcroute -k; /usr/sbin/smcroute -d

It turns out that I was getting different results from ip mroute show each time, possibly because multicast packets had not yet arrived on the interface. Adding a sleep 1 after the first line and before the ip mroute show chain fixed it.

I wouldn't have found it if not for Fosco's help, and I also didn't know how to debug or expand aliases before.

Comments

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.