I can not get this to work in Bourne shell

[b][red]This message was edited by the CS_Smooth at 2002-4-20 8:28:56[/red][/b][hr]
Challenge Problem for Bourne Shell
The following script is an attempt to count the number of accounts in the lab that have the C shell as the default shell.

It does not work because the value of COUNTER inside the while loop is only available inside a child shell process and not available outside that loop.

If someone knows a way to get this to work, I'll be very thankfull.

#!/bin/sh

SAVEDIFS=$IFS
IFS=:

COUNTER=0

ypcat passwd |
while read user epass usid grid com hdir dsh
do
if [ "$dsh" = "/bin/csh" ]
then
COUNTER=`expr $COUNTER + 1`
fi
done

echo "Number of users with default C shell: $COUNTER"

IFS=$SAVEDIFS
exit 0



Comments

  • [b][red]This message was edited by the gautam at 2002-4-20 10:19:11[/red][/b][hr]
    I don't remember much of shell scripting but I think you can do it by the following

    [code]
    export COUNTER
    [/code]


    : Challenge Problem for Bourne Shell
    : The following script is an attempt to count the number of accounts in the lab that have the C shell as the default shell.
    :
    : It does not work because the value of COUNTER inside the while loop is only available inside a child shell process and not available outside that loop.
    :
    : If someone knows a way to get this to work, I'll be very thankfull.
    :
    : #!/bin/sh
    :
    : SAVEDIFS=$IFS
    : IFS=:
    :
    : COUNTER=0
    :
    : ypcat passwd |
    : while read user epass usid grid com hdir dsh
    : do
    : if [ "$dsh" = "/bin/csh" ]
    : then
    : COUNTER=`expr $COUNTER + 1`
    : fi
    : done
    :
    : echo "Number of users with default C shell: $COUNTER"
    :
    : IFS=$SAVEDIFS
    : exit 0
    :
    :
    :
    :



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion