# Script Gone Wrong

**URL:** <https://community.unix.com/t/script-gone-wrong/265770>\
**Category:** Shell Programming and Scripting\
**Created:** [May 15, 2010, 2:00am UTC](https://community.unix.com/t/script-gone-wrong/265770 "2010-05-15T02:00:28Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ibzee33](https://community.unix.com/letter_avatar/ibzee33/32/6_449c533f586939b6e5f246632a46421f.png) [@ibzee33](https://community.unix.com/u/ibzee33)\
**Post date:** [May 15, 2010, 2:00am UTC](https://community.unix.com/t/script-gone-wrong/265770/1 "2010-05-15T02:00:28Z")

</div>

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!!

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![vidyadhar85](https://community.unix.com/user_avatar/community.unix.com/vidyadhar85/32/997_2.png) [@vidyadhar85](https://community.unix.com/u/vidyadhar85)\
**Post date:** [May 15, 2010, 2:30am UTC](https://community.unix.com/t/script-gone-wrong/265770/2 "2010-05-15T02:30:40Z")

</div>

can you post the exact error..

---

<div class="post-metadata">

**Author:** ![ygemici](https://community.unix.com/letter_avatar/ygemici/32/6_449c533f586939b6e5f246632a46421f.png) [@ygemici](https://community.unix.com/u/ygemici)\
**Post date:** [May 15, 2010, 10:12am UTC](https://community.unix.com/t/script-gone-wrong/265770/3 "2010-05-15T10:12:06Z")

</div>

Hi  
Let change first this values

```nohighlight
# time=$(date | cut -d" " -f5 | cut -d: -f1)
time=$(date +%H)

```

```nohighlight
# users=$(uptime | cut -d" " -f8)
users=$(uptime | cut -d"," -f4 | sed -e 's/[^0-9]*//' -e 's/\..*//')

```

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/6_449c533f586939b6e5f246632a46421f.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [May 15, 2010, 5:12pm UTC](https://community.unix.com/t/script-gone-wrong/265770/4 "2010-05-15T17:12:40Z")

</div>

I agree with this correction.

```nohighlight
time=${date +%H)

```

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

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

```

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

---

<div class="post-metadata">

**Author:** ![jlliagre](https://community.unix.com/letter_avatar/jlliagre/32/6_449c533f586939b6e5f246632a46421f.png) [@jlliagre](https://community.unix.com/u/jlliagre)\
**Post date:** [May 15, 2010, 5:37pm UTC](https://community.unix.com/t/script-gone-wrong/265770/5 "2010-05-15T17:37:10Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/6_449c533f586939b6e5f246632a46421f.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [May 15, 2010, 5:54pm UTC](https://community.unix.com/t/script-gone-wrong/265770/6 "2010-05-15T17:54:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jlliagre](https://community.unix.com/letter_avatar/jlliagre/32/6_449c533f586939b6e5f246632a46421f.png) [@jlliagre](https://community.unix.com/u/jlliagre)\
**Post date:** [May 15, 2010, 6:58pm UTC](https://community.unix.com/t/script-gone-wrong/265770/7 "2010-05-15T18:58:34Z")

</div>

They indeed read the same file but reports different values:

```nohighlight
$ 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.

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/6_449c533f586939b6e5f246632a46421f.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [May 15, 2010, 7:13pm UTC](https://community.unix.com/t/script-gone-wrong/265770/8 "2010-05-15T19:13:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jlliagre](https://community.unix.com/letter_avatar/jlliagre/32/6_449c533f586939b6e5f246632a46421f.png) [@jlliagre](https://community.unix.com/u/jlliagre)\
**Post date:** [May 15, 2010, 7:19pm UTC](https://community.unix.com/t/script-gone-wrong/265770/9 "2010-05-15T19:19:17Z")

</div>

There is a discrepancy but both values are correct.

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/6_449c533f586939b6e5f246632a46421f.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [May 15, 2010, 7:21pm UTC](https://community.unix.com/t/script-gone-wrong/265770/10 "2010-05-15T19:21:34Z")

</div>

As the accountant said to the auditor.

---

<div class="post-metadata">

**Author:** ![jlliagre](https://community.unix.com/letter_avatar/jlliagre/32/6_449c533f586939b6e5f246632a46421f.png) [@jlliagre](https://community.unix.com/u/jlliagre)\
**Post date:** [May 15, 2010, 7:27pm UTC](https://community.unix.com/t/script-gone-wrong/265770/11 "2010-05-15T19:27:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/6_449c533f586939b6e5f246632a46421f.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [May 15, 2010, 7:53pm UTC](https://community.unix.com/t/script-gone-wrong/265770/12 "2010-05-15T19:53:38Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ibzee33](https://community.unix.com/letter_avatar/ibzee33/32/6_449c533f586939b6e5f246632a46421f.png) [@ibzee33](https://community.unix.com/u/ibzee33)\
**Post date:** [May 16, 2010, 1:57am UTC](https://community.unix.com/t/script-gone-wrong/265770/13 "2010-05-16T01:57:01Z")

</div>

@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

 ![](https://community.unix.com/uploads/default/original/2X/8/86a216a69f5a82d575fe6926cb16d04ced56dfc0.jpeg)

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

---

<div class="post-metadata">

**Author:** ![kshji](https://community.unix.com/user_avatar/community.unix.com/kshji/32/15661_2.png) [@kshji](https://community.unix.com/u/kshji)\
**Post date:** [May 16, 2010, 3:09am UTC](https://community.unix.com/t/script-gone-wrong/265770/14 "2010-05-16T03:09:57Z")

</div>

Why you din't make change

```nohighlight
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.

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![ibzee33](https://community.unix.com/letter_avatar/ibzee33/32/6_449c533f586939b6e5f246632a46421f.png) [@ibzee33](https://community.unix.com/u/ibzee33)\
**Post date:** [May 16, 2010, 3:44am UTC](https://community.unix.com/t/script-gone-wrong/265770/15 "2010-05-16T03:44:51Z")

</div>

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 🙂
