Operating System - HP-UX
1834134 Members
1928 Online
110064 Solutions
New Discussion

Re: run shell script and display/print resultant commands

 
SOLVED
Go to solution
Donald Thaler
Super Advisor

run shell script and display/print resultant commands

is there a unix statement that will allow me to run a shell file and show/print the statements that are generated without executing the statements ?
16 REPLIES 16
James R. Ferguson
Acclaimed Contributor
Solution

Re: run shell script and display/print resultant commands

Hi Donald:

At the beginning of your script do:

#!/usr/bin/sh -vxn

Drop the '-n' to keep tracing while actually executing.

See the 'sh-posix' manpages.

Regards!

...JRF...
Adam W.
Valued Contributor

Re: run shell script and display/print resultant commands

James, I didn't start this, but thanks for that. I was not aware of this little trick! Wish I could give ya points..... Thanks James.
There are two types of people in the world, Marines and those who wish they were.
Donald Thaler
Super Advisor

Re: run shell script and display/print resultant commands

the resultant output doesn't show me the statements with the variable substitutions. If there was an entry in the script for $NEW_SID, i see $NEW_SID instead of the entry 'TEST' ?
Steven Schweda
Honored Contributor

Re: run shell script and display/print resultant commands

Are you looking at the "-v" output or the
"-x" output?
James R. Ferguson
Acclaimed Contributor

Re: run shell script and display/print resultant commands

Hi (again) Donald:

> the resultant output doesn't show me the statements with the variable substitutions.

True, this is what you get. If you want additional tracing/debugging then I think you are going to have to add your own.

Regards!

...JRF...
Donald Thaler
Super Advisor

Re: run shell script and display/print resultant commands

i was looking at the xvn output...
Steven Schweda
Honored Contributor

Re: run shell script and display/print resultant commands

> i was looking at the xvn output...

Well, with "-n", the commands are not
executed, so variable assignments don't
happen, and "-x", which shows what gets
executed, does nothing. Without "-n", things
happen, and "-x" shows something.

dy # cat xv.sh
#!/bin/sh
var='fred'
echo "$var"

dy # sh xv.sh
fred <-- Normal output.

dy # sh -v xv.sh
#!/bin/sh
var='fred' <-- "-v" output.
echo "$var" <-- "-v" output.
fred

"-v" shows what the shell has got before it
interprets it. So, "-v" shows "$var", not
"fred". Adding "-n" stops the "echo" from
acting, but you still see the (unevaluated)
statements:

dy # sh -vn xv.sh
#!/bin/sh
var='fred'
echo "$var"

"-x" shows what the shell executes:

dy # sh -x xv.sh
+ var=fred
+ echo fred
fred
dy # sh -xn xv.sh
dy #

If you want to see evaluated variables, then
you need to execute the script. To me, using
"-n" and "-x" together makes little sense.

Never trust the advice you get here, I always
say.
Steven Schweda
Honored Contributor

Re: run shell script and display/print resultant commands

And, of course, you can use "-v" and "-x"
together:

dy # sh -vx xv.sh
#!/bin/sh
var='fred'
+ var=fred
echo "$var"
+ echo fred
fred

The "+ " prefix helps to identify the "-x"
output, but the mixed output can get
confusing when more complex statements are
used (conditional and looping statements,
"eval", and so on).
James R. Ferguson
Acclaimed Contributor

Re: run shell script and display/print resultant commands

Hi (again)"

> Steven: Never trust the advice you get here, I always say.

Well, now, the OP stated: "...allow me to run a shell file and show/print the statements that are generated without executing the statements"

So, what does that suggest to you? Oh, never mind, your cynical remarks far outweigh the insights you *could* share.

...JRF...
Steven Schweda
Honored Contributor

Re: run shell script and display/print resultant commands

> [...] Oh, never mind, [...]

So why _would_ it make sense to use "-x" with
"-n"? My insight is insufficient to explain
that.
Dennis Handly
Acclaimed Contributor

Re: run shell script and display/print resultant commands

>Steven: So why _would_ it make sense to use "-x" with "-n"?

That's correct, it didn't make sense to me either. The only way it would make sense is that you remember "-xvn" as one string and take out the "n" if you want to execute. (Or add "x" and remove "n".)
James R. Ferguson
Acclaimed Contributor

Re: run shell script and display/print resultant commands

Hi:

> Dennis: The only way it would make sense is that you remember "-xvn" as one string and take out the "n" if you want to execute. (Or add "x" and remove "n".)

Yes, thank you Dennis. That indeed was my point, although by inference rather than consise statement.

Regards!

...JRF...
Donald Thaler
Super Advisor

Re: run shell script and display/print resultant commands

Ferguson... you said

Well, now, the OP stated: "...allow me to run a shell file and show/print the statements that are generated without executing the statements"

What does this mean.... what OP.... and what would be the options that would be used to get the generated script without execution ???

James R. Ferguson
Acclaimed Contributor

Re: run shell script and display/print resultant commands

Hi (again) Donald:

> ...what OP...

OP = Original Poster (you). I was responding to the remark "...Never trust the advice you get here..." by quoting your original post's sepcifications.

In all, you could do:

# sh -vx file

...to execute and trace

OR:

# sh -vn file

...to NOT execute but list [no so useful unless there is a syntax error]

As I said before, I think to interpret for syntax only AND print the statements generated while prohibiting actual execution, you are going to have to add your own debugging displays.

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: run shell script and display/print resultant commands

> What does this mean.... what OP....

Original Poster (that is, you)?

> and what would be the options that would be
> used to get the generated script without
> execution ???

"-v" shows you what the shell sees. "-x"
shows you what the shell does. In general,
the shell can't know what it will do unless
it does it.

If you wish to test a script which contains
a dangerous command, you can often do it by
inserting an "echo". For example, instead
of:
rm "${fred}"
use:
echo rm "${fred}"

Then if you like the looks of the resulting
commands, remove the "echo".

It's possible to make that a run-time
decision, too, but it can get messy.

Or, you can write a script which writes a
script, and you can inspect that before
running it, and so on. (Many things are
possible, but my reading your mind to learn
what problem you're really trying to solve is
not one of them.)
OldSchool
Honored Contributor

Re: run shell script and display/print resultant commands

something like the following might be of use

#!/bin/sh
ECHO=echo
$echo ls -l

when done debugging, set ECHO=,
or paramaterize it perhaps