Old script on systemV Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Old script on systemV without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I want to add a group for permit user belong to group to shutdown the machine system is old sysv unix,with old bourne sh. I did

{
groufind=`groups|grep system|awk '{print $2}'`
}
        groupscheck
        eval `id  |  /usr/bin/sed 's/[^a-z0-9=].*//'`
        if [ "$LOGNAME != "root"  -o  $groufind != "system" ];
        then
                echo "$0:  Only root or system group can run /sbin/shutdown."
                exit 2
        fi
else
        echo "$0:  can't check user id."
        exit 2

But doesn’t work,exit with error

/sbin/shutdown: test: argument expected

Where is wrong?


Sorry for incomplete script,i want a work like this

    groupscheck()
{
groufind=`groups|grep system|awk '{print $2}'`
who=`id|cut -d "(" -f 2|cut -d ")" -f 1`
root=root
system=system
}
        groupscheck
        if [ $who !=  $root  -o  $groufind !=  $system ];
then
        echo "OK"
        else
        echo    "no"
fi

If root or user in system exec the script give OK otherwise no,but why don’t work?

Answer

There’s a missing quote… You should really check your code, at least with a syntax highlighter. This line…

if [ "$LOGNAME != "root"  -o  $groufind != "system" ];

Should be:

if [ "$LOGNAME" != "root"  -o  $groufind != "system" ];

Now for the rest, I’m gonna have to guess because your code sample is nothing close to helpful:

  • This weird scope at the beginning is probably groupchecks‘s definition.
  • I’m not really sure why you’re calling eval.
  • I don’t why you added an else branch in the end, since it has no matching if.

Anyway, I’d say your script could be simplified as:

#!/bin/sh

groups $LOGNAME | grep system > /dev/null 2>&1
if [ $? -ne 0 -a "$LOGNAME" != "root" ]; then
    echo "$0:  Only root or system group can run /sbin/shutdown."
    exit 1
else
    echo "$0: Shutdown possible."
    exit 0
fi

… even though I’m not quite sure if all of this runs on an old SYSv4 system.

We are here to answer your question about Old script on systemV - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji