Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I want to detect online network/shell services in my Solaris. I write following script for this purpose:

compare_ser()
{
if [ "$1" != "" ]; then
echo "True" >> Solaris.txt
fi
}
export -f compare_ser
svcs network/shell | cut -d ' ' -f1 | grep "online" | xargs -n1 bash -c 'compare_ser $@'

when i run svcs network/shell | cut -d ' ' -f1 | grep "online" | xargs -n1 echo in terminal, I get the following output:

online
online

but my script don't show anything. What's its problem?

share|improve this question
    
If you're downvoting our answers it would be helpful for us to understand why. –  roaima Jul 14 at 20:11

4 Answers 4

up vote 0 down vote accepted

Use this:

svcs network/shell | cut -d' ' -f1 | grep "online" | xargs -n1 -I{} bash -c 'compare_ser {}'

The {} interpolates each value generated through xargs. Your $@ attempts to interpolate command line arguments - of which there are none.

share|improve this answer
    
Why the downvote without comment? –  roaima Jul 14 at 20:02

I believe, there is a shorter way:

svcs network/shell | awk '/online/ {system("bash -c \"compare_ser "$1"\"")}'

Dear Downvoters, can you explain your decision? Have you understand, what Linux really is? The script above is a working snippet and maybe there are million ways to do the same. So I ask for an explanation.

share|improve this answer
    
What's wrong with my answer? Can anyone explain this? Linux is very different but answers here not? –  A.B. Jul 14 at 20:10
    
Drive--by down-vote... ALL answers were down-voted... ;-) –  Fabby Aug 13 at 18:02

You may have to use double quotes for the string to resolve the positional parameters.

svcs network/shell | cut -d ' ' -f1 | grep "online" | xargs -n1 bash -c "compare_ser $@"
share|improve this answer
svcs network/shell | sed -n '/^online /c\
True' >> Solaris.txt

...should be pretty much the equivalent.

The reason your script doesn't show anything, though, is that shell function is shell code which must be executed by a shell which already knows it - the function must first be declared in the shell in which it is run in order to work.

When you call xargs, however, you call a program which calls another program, which is a shell, of course, but the new shell processes which xargs calls for you are not aware of the shell function defined in your current shell.

Of course you do export -f which would work, perhaps, if your shell understood what that meant, but if you're using ksh on a Solaris system - then it won't. And anyway, that would be a terribly inefficient means of reaching your goal if it did.

share|improve this answer
    
I guess this answer was only good enough not to get downvoted. –  mikeserv Jul 14 at 19:41
1  
I guess they changed their mind. It's nice to be noticed. –  mikeserv Jul 14 at 19:54
    
export -f worked for me, with a starting point of bash. Perhaps their initial shell was ksh and so the xargs bash subshell couldnt see the function. –  roaima Jul 14 at 20:07
    
@roima - right, it should work if launched from bash, but ksh is the default Solaris shell, and it would not work if run from there. –  mikeserv Jul 14 at 20:14
    
@roaima - i guess that depends on version, and on which shell runs what. According to this, ksh is the default scripted shell, but bash is the default interactive shell for Solaris 11+. So who knows. –  mikeserv Jul 14 at 20:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.