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

if [ "$1" != "" ]; then
echo "True" >> Solaris.txt
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:


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

  • If you're downvoting our answers it would be helpful for us to understand why. Jul 14, 2015 at 20:11

4 Answers 4


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.

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

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.

  • Why the downvote without comment? Jul 14, 2015 at 20: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 $@"
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.

  • I guess this answer was only good enough not to get downvoted.
    – mikeserv
    Jul 14, 2015 at 19:41
  • 1
    I guess they changed their mind. It's nice to be noticed.
    – mikeserv
    Jul 14, 2015 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. Jul 14, 2015 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, 2015 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, 2015 at 20:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .