Script Gone Wrong

Hello all,

so this is a script i did for an assignement,

  • first option greets the user according to the time after fetching his name
  • second options isn't implemented
  • third check the performance according to how many users are using the system
  • creates a log of names, time and ip of the user currently using the system.

problem is the script was working at some point, or so id like to believe.
after a few days i tried to run it, and i got syntax error on every option.
i tried looking over at it, but it seems right.

so im hoping someone would be able to figure out happened to it!!

echo Choose Number to select:
echo 1.Greet User
echo 2.Replace Patter
echo 3.Check Performance
echo 4.Log info
echo 5.Exit

read choice

user=$(whoami | cut -d" " -f1 )
name=$(grep $user /etc/passwd | cut -d: -f5)

while [ $choice -ne 5 ]
do
    if [ $choice -eq 1 ]
    then
        time=$(date | cut -d" " -f5 | cut -d: -f1)
        if [ $time -gt 00 -a $time -le 12 ]
        then
            echo Good Morning $name
        elif [ $time -gt 12 -a $time -le 17 ]
        then
            echo Good Afternoon $name
        elif [ $time -gt 17 -a $time -le 24 ]
        then
            echo Good Evening $name
        fi
    elif [ $choice -eq 2 ]
    then
        echo $choice
    elif [ $choice -eq 3 ]
    then
        users=$(uptime | cut -d" " -f8)
        if [ $users -ge 0 -a $users -lt 7 ]
        then
            echo System has high performance
        elif [ $users -ge 7 -a $users -lt 12 ]
        then
            echo System has medium preformance
        elif [ $users -gt 12 ]
        then
            echo System has low performance
        fi
    elif [ $choice -eq 4 ]
    then
        who > temp
        lines=$(wc -l ./temp | cut -d" " -f1)
        cnt=1
        
        touch tmplog    
        touch logfile

        while [ $cnt -le $lines ]
        do
            usertemp=$(head -$cnt ./temp | tail -1 | cut -d" " -f1)
            nametemp=$(grep $usertemp /etc/passwd | cut -d: -f5)
            datetemp=$(head -$cnt ./temp | tail -1 | cut -d" " -f13)
            adrstemp=$(head -$cnt ./temp | tail -1 | cut -d" " -f14)

            if [ "$usertemp" != "$user" ]
            then 
                echo -n $nametemp >> tmplog
                echo -n " " >> tmplog
                echo -n $datetemp >> tmplog
                echo -n " " >> tmplog
                echo -n $adrstemp >> tmplog
                echo "" >> tmplog
            fi
            timetemp=$(head -$cnt ./temp | tail -1 | cut -d" " -f1)
            let cnt=$cnt+1             
        done        
        
        sort -db tmplog -o logfile
        rm temp
        rm tmplog
    else
        echo no such option

    fi

echo Choose Number to select:
echo 1.Greet User
echo 2.Replace Patter
echo 3.Check Performance
echo 4.Log info
echo 5.Exit

read choice

done

can you post the exact error..

Hi
Let change first this values

# time=$(date | cut -d" " -f5 | cut -d: -f1)
time=$(date +%H)
# users=$(uptime | cut -d" " -f8)
users=$(uptime | cut -d"," -f4 | sed -e 's/[^0-9]*//' -e 's/\..*//')
1 Like

I agree with this correction.

time=${date +%H)

Prefer this to extract the number of users from uptime. If this is Solaris, substitute nawk for awk.

users=$(uptime | awk '{print $6}')

or
users=$(who -u|wc -l)

1 Like

$6 isn't reliable, uptime output has a variable format, at least on Solaris.
I would use something like this:

users=$(uptime | sed -e 's/ user.*$//' -e 's/.* //')
1 Like

Thanks jlliagre. I had completely forgotton that. The format does indeed change. Lets stick with "who" ?
Both "uptime" and "who" read the same file.

They indeed read the same file but reports different values:

$ who -q                                                             
jlliagre jlliagre jlliagre guest    
# users=4
$ uptime                                                             
 12:56am  up  5:15,  2 users,  load average: 0,10, 0,16, 0,16

"How many users" is undoubtly ambiguous.

Though we don't seem to have the discrepancy on our systems we don't have any scripts which rely on the "uptime" command.

There is a discrepancy but both values are correct.

As the accountant said to the auditor.

If you are logged twice to a system, are you a single user or two ?

Must depend on context.
Some software is licenced by seat and some by connection.
Some software allows multiple users under the same login, some doesn't.
We'd look at the most relevant count.

Where the system is hosting a database the count of active database connections is much more useful than unix login sessions (which might be nil).
Within that there may be rules about multiple connections.

Wonder if the O/P wants number of current sessions or number of different users who have current sessions?

@ygemici, methyl, jlliagre - Thanks alot for the help, i corrected the code, and now its working fine, i should have posted it before handing it in.

i have a question though,

  • as for the (date +%H) what does it do?!

as for "users=$(who -u|wc -l)" instead of using "uptime" that was pretty slick, i liked it.

@vidyadhar85's - sorry i forgot to post the errors, here they are, but i guess its a little bit, the guys figured out the erros

Thanks again
let's hope the teacher accepts a modified late assignment :stuck_out_tongue:

Why you din't make change

time=$(date | cut -d" " -f5 | cut -d: -f1)
#=>
time=$(date '+%H')   # run command date '+%H' to test and read man date

If need arguments, so use "$time", better than $time.
After those you don't get those errors.

Here is maybe more easier to read version, menu has only once, used more pipes, not temp files.

while
      echo "1. Greet User"
      echo "2. Replace Papper"
      #...
      echo -n "Choice:"
      read choice
do
      if [ "$choice" = 5 ] ; then
         break
      fi

      ###################################
      if [ "$choice" = 1 ] ; then
         #do_some
         continue
      fi

      ###################################
      if [ "$choice" = 2 ] ; then
         #do_some2
         continue
      fi

      ###################################
      if [ "$choice" = 3 ] ; then
         users=$(who | wc -l)
         #...
         continue
      fi

      ###################################
      if [ "$choice" = 4 ] ; then
         who | while read line
         do
            values=($line)
            usertemp=${values[0]}
            #....
         done
         #....

         continue
      fi


      #...
      echo "no such option"
done
1 Like

i already changed the wrong parts of the code, those errors are before modifying the code, i just submitted the errors for the heck of it :slight_smile: