Results 1 to 9 of 9

Thread: bash shell - problem assigning expression to variable

Threaded View

  1. #1
    Join Date
    May 2013
    Beans
    3

    bash shell - problem assigning expression to variable

    Hi guys

    I am having some troubles to get my bash script working. Process assigned to that script stop as soon as I start it.
    After some tests, I identify that something wrong is happening into following lines :
    Code:
     USR=$(top -d1 -n2 | grep 'Cpu(s)' | tail -1 | sed -e 's/\s\+/ /g' | cut -d' ' -f 2 | cut -d'%' -f 1 | cut -d'.' -f 1)
     SYS=$(top -d1 -n2 | grep 'Cpu(s)' | tail -1 | sed -e 's/\s\+/ /g' | cut -d' ' -f 3 | cut -d'%' -f 1 | cut -d'.' -f 1)
    
    
     ALL=$(($USR+$SYS))
     echo $ALL | tr '[ \n]' '-' >> stat.log
    I try to assign to variables USR and SYS some expressions. I think that my syntax is correct but at the end it is not working.

    It tried that way too :
    Code:
     USR=`top -d1 -n2 | grep 'Cpu(s)' | tail -1 | sed -e 's/\s\+/ /g' | cut -d' ' -f 2 | cut -d'%' -f 1 | cut -d'.' -f 1`
     SYS=`top -d1 -n2 | grep 'Cpu(s)' | tail -1 | sed -e 's/\s\+/ /g' | cut -d' ' -f 3 | cut -d'%' -f 1 | cut -d'.' -f 1`
    But result is the same.

    Does somebody have an idea what I am doing wrong here?
    I suspect an issue with some wrong quotes used but after some search I don't find any solution...



    UPDATE 19/05 :
    The script is basically running when I start it on the current thread :
    Code:
     
    stou@laptop:~/$ sh read_stats.sh
    But my problem is when I start it on another thread to let it work in background. It stops as soon as I tip another command (or just press ENTER) in shell :
    Code:
     
    stou@laptop:~/$ sh read_stats.sh &
    [1] 4473
    stou@laptop:~/$
    [1]+  Stopped          sh read_stats.sh
    stou@laptop:~/$
    Last edited by stou; May 19th, 2013 at 10:04 AM. Reason: additional info

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •