Need help with C shell scripting

Discussion in 'OT Technology' started by D1G1T4L, Apr 21, 2004.

  1. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    Basically i am trying to write a script that monitors number of users + processes on the linux box......

    basically i want something like this
    Code:
    Cycle 1 : number of users = 25, processes = 30 
    Cycle 2 : number of users = 24, processes = 29 
    Cycle 3 : number of users = 22, processes = 24 
    Cycle 4 : number of users = 21, processes = 24 
    Cycle 5 : number of users = 24, processes = 31 
    Cycle 6 : number of users = 23, processes = 29 
    Cycle 7 : number of users = 24, processes = 28 
    Cycle 8 : number of users = 29, processes = 30 
    ...
     
    Cycle 15 : number of users = 23, processes = 28 
    
    After monitoring 15  min:
    --------------------------
     Smallest number of users = 21 
     Largest number of users = 29, at cycle no: 8
    
     Smallest number of processes = 24 
     Largest number of processes = 31 , at cycle no: 5
    ---------------- 
    
    i started messing with C shell scripting today and this is what i got

    Code:
    #!/bin/csh
    
    @ loopVariable = 1
    set lNumberOfUsers = 0
    set lNumberOfProcesses = 0
    set sNumberOfUsers = `who | cut -d " " -f1 | sort | uniq | wc -l`
    set sNumberOfProcesses = `ps -a | wc -l`
    while( $loopVariable <= 15 )
    {
    set numberOfU = `who | cut -d " " -f1 | sort | uniq | wc -l`
    set numberOfP = `ps -a | wc -l`
    if($numberOfU < $sNumberOfUsers) then
      set sNumberOfUsers = $numberOfU
    endif
    if($numberOfU > $lNumberOfUsers) then
      set lNumberOfUsers = $numberOfU
      set lNumberCycleOfU = $loopVariable
    endif
    if($numberOfP < $sNumberOfProcesses) then
      set sNumberOfProcesses = $numberOfP
    endif
    if($numberOfP > $lNumberOfProcesses) then
      set lNumberOfProcesses = $numberOfP
      set lNumberCycleOfP = $loopVariable
    endif
    echo -n "Cycle $loopVariable : number of users = "
    echo "${numberOfU}, processes = $numberOfP"
    if($loopVariable != 15) then
     sleep 60
    endif
    @ loopVariable++
    }
    end
    echo "After monitoring 15 mins: "
    echo "--------------------------"
    echo "Smallest number of users = $sNumberOfUsers"
    echo -n "Largest number of users = ${lNumberOfUsers}, at cycle no: "
    echo "$lNumberCycleOfU"
    echo ""
    echo "Smallest number of processes = $sNumberOfProcesses"
    echo -n "Larget number of processes = ${lNumberOfProcesses},"
    echo " at cycle no: $lNumberCycleOfP"
    echo "---------------------------"
    
    
    My result is

    Code:
    {: Command not found
    Cycle 1 : number of users = 19, processes = 20
    }: Command not found
    {: Command not found
    Cycle 2 : number of users = 18, processes = 27
    ........
    
    i dont understand why i get {: Command not found
    any help would be appreciated
     
    Last edited: Apr 21, 2004
  2. wamding

    wamding OT Supporter

    Joined:
    Oct 23, 2003
    Messages:
    210
    Likes Received:
    0
    Code:
    #!/bin/csh
    
    @ loopVariable = 1
    set lNumberOfUsers = 0
    set lNumberOfProcesses = 0
    set sNumberOfUsers = `who | cut -d " " -f1 | sort | uniq | wc -l`
    set sNumberOfProcesses = `ps -a | wc -l`
    while( $loopVariable <= 15 )
    
    set numberOfU = `who | cut -d " " -f1 | sort | uniq | wc -l`
    set numberOfP = `ps -a | wc -l`
    if($numberOfU < $sNumberOfUsers) then
      set sNumberOfUsers = $numberOfU
    endif
    if($numberOfU > $lNumberOfUsers) then
      set lNumberOfUsers = $numberOfU
      set lNumberCycleOfU = $loopVariable
    endif
    if($numberOfP < $sNumberOfProcesses) then
      set sNumberOfProcesses = $numberOfP
    endif
    if($numberOfP > $lNumberOfProcesses) then
      set lNumberOfProcesses = $numberOfP
      set lNumberCycleOfP = $loopVariable
    endif
    echo -n "Cycle $loopVariable : number of users = "
    echo "${numberOfU}, processes = $numberOfP"
    if($loopVariable != 15) then
     sleep 60
    endif
    @ loopVariable++
    
    end
    echo "After monitoring 15 mins: "
    echo "--------------------------"
    echo "Smallest number of users = $sNumberOfUsers"
    echo -n "Largest number of users = ${lNumberOfUsers}, at cycle no: "
    echo "$lNumberCycleOfU"
    echo ""
    echo "Smallest number of processes = $sNumberOfProcesses"
    echo -n "Larget number of processes = ${lNumberOfProcesses},"
    echo " at cycle no: $lNumberCycleOfP"
    echo "---------------------------"
    
    
    The problem was the curly braces at the beginning and end of the while loop. I took them out and ran your code and it works fine.
     
  3. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    but i thought while code should have braces
     
  4. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    never mind, i guess while loop in C shell doesnt have curly braces
     
  5. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    yea, it's not really the "C" programming language, even though it is the C-shell
     

Share This Page