Why Does Command Run From Prompt But Not From Script?

I hope someone can shed any light on this mystery.

I am trying to run the following command:
su userID -c
remsh server -l userid -n "awk -F^ '\$4 == \"SMITH\"' /tmp/infromational/version74b/LIVE/TEMPORARY/ABCfiles/HLC_Database_File.bat|head -1" > /tmp/variant/45BV32/var/store13.logfnd

I know it's rather long but when I copy/paste it into a command line it runs great. But if I paste the same command into a file and run it as a script it never finishes and I have it hit CTRL +C to break out of it.

Does anyone have any idea why it works one way, but not the other?

Update: I also tried adding #!/bin/ksh or #!/bin/sh to the script, but that didn't change the outcome either. The script still continues to hang and never finish. :frowning:

Try putting full path to the commands.

instead of:

su userID -c

try

/usr/bin/su userID -c

Not just for su but for all the commands in the line.

Thanks for the suggestion Ikon. I tried it but it didn't work.

How about putting echo statements every other line, so you know where it's failing?

Thanks for the suggestion Shawn but there's only two lines and those lines are static (no variables, etc) so echoing them would not show anything different then what is already in the script.

If it is "hanging" it is probably waiting for a response to the "Password:" prompt. Are you running the script as the root user?

It's not being run as root but it is using IDs that have been configured with a trusted user status that permits communication between the servers without password prompts.

What is the output of set (or env) from inside the script and on the command line - any diffs?

A comparison of ENV from inside the script and the command line show no differences.

Post the output of cat -vet on your script...I'm leaning towards control characters embedded in your script.

cat -vet script

I found something interesting just now. I thought that perhaps the problem was with the AWK command. I don't use it much and it always seems to give me a hard time so I'm naturally skeptical of it.:o

So I took it out and replaced it with a grep command. Grep won't do what I need because it can't sort columns based on a delimiter but it's close enough to test this problem with. The new line was now:
su userID -c
remsh server -l userid -n grep SMITH /tmp/infromational/version74b/LIVE/TEMPORARY/ABCfiles/HLC_Database_File.bat|head -1 > /tmp/variant/45BV32/var/store13.logfnd

Now, at first this still hung until I changed the whole thing to a single line:
su userID -c "remsh server -l userid -n grep SMITH /tmp/infromational/version74b/LIVE/TEMPORARY/ABCfiles/HLC_Database_File.bat|head -1" > /tmp/variant/45BV32/var/store13.logfnd

That worked but I can't seem to make it the same when I use AWK because AWK requires quotes around itself that grep doesn't and remsh when used on the same line as the SU command requires quotes around itself as well. So it ends up looking like this:
su userID -c "remsh server -l userid -n "awk -F^ '\$4 == \"SMITH\"' /tmp/infromational/version74b/LIVE/TEMPORARY/ABCfiles/HLC_Database_File.bat|head -1"" > /tmp/variant/45BV32/var/store13.logfnd

but that many quotes just do not work in the script.
su: illegal option -- F
su: illegal option -- ^
-F^: bad option(s)

And if I remove the quotes from around the AWK command I get this error:
syntax error The source line is 1.
The error context is
>>> == <<<
awk: Quitting
The source line is 1.

And if I remove the quotes from around the remsh call instead I get this error:
su: illegal option -- l
su: illegal option -- n
usage: remsh host [ -l login ] [ -n ] command

The result of cat -vet are as follows:
su userID -c$
remsh server -l userid -n "awk -F^ '\$4 == \"SMITH\"' /tmp/infromational/version74b/LIVE/TEMPORARY/ABCfiles/HLC_Database_File.bat|head -1" > /tmp/variant/45BV32/var/store13.logfnd$

So no control characters but you seem to have isolated fault with awk command. I have made minor tweaks to it in red. Removed backslash in \$4 and the ones in \"SMITH\".

su userID -c \
"remsh server -l userid -n "awk -F^ '$4 == "SMITH"' /path/to/input/file | head -1"" > output_file

Thanks Shamrock. I tried your suggestion:
su userID -c \
"remsh server -l userid -n "awk -F^ '$4 == "SMITH"' /tmp/infromational/version74b/LIVE/TEMPORARY/ABCfiles/HLC_Database_File.bat|head -1"" > /tmp/variant/45BV32/var/store13.logfnd

Unfortunately it produced the following error:
su: illegal option -- F
su: illegal option -- ^
-F^: bad option(s)

Would it be possible to perform the same request using the SED command instead of AWK?