Operating System - HP-UX
1832986 Members
2464 Online
110048 Solutions
New Discussion

using xargs and read from standard input

 
SOLVED
Go to solution
Billa-User
Regular Advisor

using xargs and read from standard input

hello,

is it possible to use command "xargs" , start a subprogramm and read in the sub programm from standard input ?
example below, it isn't possible to read from standard input in "sub programm". maybe any ideas to fix it ? command "xargs" exists in many shell scripts.

main programm:
#!/usr/bin/ksh
cat xargs.lis | xargs -l -x -t ./u_xargs-test.sh

sub programm:
#!/usr/bin/ksh
echo "this is script: `basename $0`"
echo "Number. Par: $#"
echo "Parameter: $*"

echo "Input: "
read input <= isn't possible to read from standard input

echo $input

regards,tom
11 REPLIES 11
Billa-User
Regular Advisor

Re: using xargs and read from standard input

sorry, i forget to describe the content of example file "xargs.lis" :

parameter1
parameter1 parameter2
parameter1 parameter2 parameter3

i called "main programm" : h_xargs-test.sh,
"sub programm" u_xargs-test.sh

regards,tom

regards,tom
James R. Ferguson
Acclaimed Contributor
Solution

Re: using xargs and read from standard input

Hi Tom:

One way is to change your subprogram to specifically associate STDIN with '/dev/tty' as:

#usr/bin/ksh
exec 0< /dev/tty # NOTE!
echo "this is script: `basename $0`"
echo "Number. Par: $#"
echo "Parameter: $*"
echo "Input: "
read input
echo $input

Regards!

...JRF...
Peter Nikitka
Honored Contributor

Re: using xargs and read from standard input

Hi,

besides JRF's solution, you can see it clearly, that such reading of stdin will not work plain, when you look on this equivalent form of your command:
xargs -l -x -t ./u_xargs-test.sh
Stdin is already redirected.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Billa-User
Regular Advisor

Re: using xargs and read from standard input

thank you very,very much, great solution.
Billa-User
Regular Advisor

Re: using xargs and read from standard input

hello,

i have a new question to this old thread:

i detect following case:

i use this statement (u_xargs-test.sh is a sub-programm):
xargs -l -x -t ./u_xargs-test.sh
when a error occurs in "./u_xargs-test.sh" or i want to stop program with "exit 1" the "loop" of xargs doesn't stop. also "set -e" in "h_xargs-test.sh" or "u_xargs-test.sh" has no impact.

how can i stop sub program , when i use the statement "xargs" ?

regards
James R. Ferguson
Acclaimed Contributor

Re: using xargs and read from standard input

Hi Tom:

> how can i stop sub program , when i use the statement "xargs" ?

I don't know of anyway to do this. The 'xargs' process invokes your script for bundles of '-l' size arguments --- in this case *one*. This means that a new process (your script) is instantiated for every line read from you input. Whether or not the script (process) succeeds or fails doesn't matter to 'xargs'; it merely reads another bundle of input.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: using xargs and read from standard input

Hi (again) Tom:

OK, duh! If you want your invoked process to terminate the 'xargs' statement, simply issue a 'kill ${PPID}' in your script.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: using xargs and read from standard input

>JRF: If you want your invoked process to terminate the 'xargs' statement, simply issue a 'kill ${PPID}'

You may want to check to see that your parent actually is xargs(1).
Billa-User
Regular Advisor

Re: using xargs and read from standard input

hello,

> You may want to check to see that your parent actually is xargs(1).

xargs terminates if it receives a return code of -1 from command or if
it cannot execute command. When command is a shell program, it should
explicitly exit (see sh(1)) with an appropriate value to avoid
accidentally returning with -1.

the "loop" of xargs continues , when i try to stop the sub-shell-program like

if [ ! -d directory ]
then
echo "dir not found"
exit 1
fi

so i have to change command "xargs" to a statement like "while" , isn't it ?
it is no so easy to change those commands :

nl -ba -w1 -s" " xargs.lis | xargs -l -x -t ./u_xargs-test.sh par1

regards
Dennis Handly
Acclaimed Contributor

Re: using xargs and read from standard input

>the "loop" of xargs continues, when I try to stop the sub-shell-program like
> exit 1

As documented, you need: exit -1
Billa-User
Regular Advisor

Re: using xargs and read from standard input

thx ! now the sub-shell-program stops with "exit -1"

i am sorry , i miss the exact meaning ...