Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"ksh -c" and spawned-shell variables

Status
Not open for further replies.

DoraC

Programmer
May 7, 2002
98
US
Hi,

If I'm understanding this correctly, executing the command

Code:
ksh -c "command";

from the command line spawns a new k-shell, and runs the command "command" in that shell. This seems to work fine, unless my "command" statement involves variables... for example:

Code:
ksh -c "ls | while read -r
do
    echo $REPLY;
done;"

the output is a lot of blank lines, while running the same command w/o the "ksh -c" yields non-blank output. My knowledge is a little hazy when it comes to subshells... Can anybody provide a technical explanation as to why this is happening, and how - using "ksh -c" - I can get variables such as my $REPLY to show output?

Thanks!
dora
 
Dora:

On my Solaris 7 box, I changed two things:

1) Eliminated the double quotes.
2) Added the REPLY variable to the read.


ksh -c ls | while read -r REPLY
do
echo $REPLY;
done;

I left ";" in although they're not necessary.

Regards,


Ed
 
Thanks for the reply!

This leads me to one more question... is it possible for me to execute a compound command (like the "while" above) from a single command line? something like:

Code:
ls | while read -r REPLY do echo $REPLY done;

This doesn't work as is - it appears as though the shell expects an end-line character... I'm asking because I need to pass strings containing Unix commands around within some Java programs (i know, i know - non-portable) and it's easier to do this if I don't have to worry about things such as end-lines.

Incidentally, I wrapped the commands in my initial example in quotes because the command serves as input to a Java program, and it's easier to have the entire thing become "arg[0]" instead of stringing all the input parameters together inside the program. I tried combining the quotes with the additional "REPLY" word, and still got only blank output.

Sigh, i feel clueless about this.

Thanks,
dora
 
Dora:

As long as you require using unix shell constructs such as whiles, fors, and ifs, I think you'll have to live with the newlines. Could you possible use unix commands which don't require \n such as:

ksh -c "find . -name \"*.txt\" -print"

If you still require a while loop, perhaps I can simplify the build method. The following awk line:

echo ""|awk ' { printf("ls|while read -r REPLY\ndo\necho $REPLY\ndone\n") } '

outputs

ls | while read -r REPLY
do
echo $REPLY
done

as soon as you get the command the way you want it, pipe it to the shell:

echo ""|awk ' { printf("ls|while read -r REPLY\ndo\necho $REPLY\ndone\n") } '|ksh

I'm not sure if that works being called from java or not.

Sorry, but that's about the best I can do.

Regards,

Ed
 
Thanks Ed, your awk suggestion will(does) work...

:)
dora
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top