Thread: Linux Needs EOF

    #1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Posts
    512
    Rep Power
    50

    Linux Needs EOF


    I'm trying to run a snippet of code that I've messed with over and over and I can't seem to make it work.

    I haven't tried turning one of the devisors into a variable and $((var1/var2)) but maybe I am just missing something simple.
    Code:
    #!/bin/sh
    
    
    watch "echo -n '0: CPU Frequency: ' && echo -n $((sudo cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq /1000)) && echo 'Mhz'
     && echo -n '1: CPU Frequency: ' && echo -n $((sudo cat /sys/devices/system/cpu/cpu1/cpufreq/cpuinfo_cur_freq /1000)) && echo 'Mhz' 
     && echo -n '2: CPU Frequency: ' && echo -n $((sudo cat /sys/devices/system/cpu/cpu2/cpufreq/cpuinfo_cur_freq /1000)) && echo 'Mhz' 
     && echo -n '3: CPU Frequency: ' && echo -n $((sudo cat /sys/devices/system/cpu/cpu3/cpufreq/cpuinfo_cur_freq /1000)) && echo 'Mhz' 
     && echo -n '4: CPU Frequency: ' && echo -n $((sudo cat /sys/devices/system/cpu/cpu4/cpufreq/cpuinfo_cur_freq /1000)) && echo 'Mhz' 
     && echo -n '5: CPU Frequency: ' && echo -n $((sudo cat /sys/devices/system/cpu/cpu5/cpufreq/cpuinfo_cur_freq /1000)) && echo 'Mhz' 
     && echo -n '6: CPU Frequency: ' && echo -n $((sudo cat /sys/devices/system/cpu/cpu6/cpufreq/cpuinfo_cur_freq /1000)) && echo 'Mhz' 
     && echo -n '7: CPU Frequency: ' && echo -n $((sudo cat /sys/devices/system/cpu/cpu7/cpufreq/cpuinfo_cur_freq /1000)) && echo 'Mhz' 
     && echo -n 'TEMP: ' && sudo cat /sys/devices/virtual/thermal/thermal_zone0/temp"
    Can someone give me some insight?
    Ubantu keeps telling me that it needs an EOF and "sudo cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq /1000"
    Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Location
    Minnesota, USA
    Posts
    30
    Rep Power
    66
    Why the double parens - $((...)) - around the commands? My /sys isn't set up like yours, so I can't completely duplicate the problem and solution, but changing the first command to single parens - $(...) - moves the error on to the second command.
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Posts
    512
    Rep Power
    50
    Hi,
    Thanks for the reply.
    I think from what I could find, the double parens signify an arithmetic equation...
    When I make it a single parens then it errors to Enexpected "&&" ....
    I'm guessing it's syntax, but I'm not sure...
    Thanks.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Location
    Minnesota, USA
    Posts
    30
    Rep Power
    66
    Next blush: You want this to be a single line, but you actually have several lines. End all but the last line with " \" (no quotes) to tell the parser that you're planning to continue the line. This still doesn't account for the division though.

    I broke your script up into individual lines, without the watch, and started breaking things down. I ended up with a sequence like:

    c0=$(sudo cat /sys/dev...freq/cpuinfo_cur_freq)
    let c0a=$c0/1000
    echo -n $c0a

    This gives me an error of "let: not found" when I run it, but actually works when I run it via a bash command, it works. The solution here is to change the shebang line to "#! /bin/bash" instead of /bin/sh.:

    pi@rppi:~ $ bash ./testscript.sh
    0: CPU Frequency: 600Mhz
    1: CPU Frequency: 600Mhz
    2: CPU Frequency: 600Mhz
    3: CPU Frequency: 600Mhz
    TEMP: 48312
    pi@rppi:~ $

    Now, we could either watch this script, or try to warp it back into your single line command, but I'm having problems doing that because watch specifically runs /bin/sh to execute the commands it is given, and sh doesn't support the richer mathematics that bash does. It's your call if you want to continue to pursue this to the bitter end, but it appears that I've gotten to a workable solution. And, it does work to put 'watch "./testscript.sh" ' (without the single quotes) in a second script file and execute that.

    Every 2.0s: ./testscript.sh Mon Dec 18 13:50:40 2017

    0: CPU Frequency: 600Mhz
    1: CPU Frequency: 1200Mhz
    2: CPU Frequency: 1200Mhz
    3: CPU Frequency: 1200Mhz
    TEMP: 49925

    Comments on this post

    • additude agrees : Thanks!!
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Posts
    512
    Rep Power
    50
    Hi !
    Thanks for all your help. I am just returning to this and I'm going to take your advise and see if I can make it work.

    Without testing it, can "let" be dropped and just use c0a=c0/1000 ?

    I'll try and see what happens..

    Thanks again.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Location
    Minnesota, USA
    Posts
    30
    Rep Power
    66
    The let command (and its syntactical equivalent: "(( expression ))" ) execute arithmetic expressions. So, for the let line, you could do any of the following:

    let c0a=$c0/1000
    c0a=(($c0/1000))
    let c0a="c0 / 100"
    c0a=((c0 / 1000))

    Within the let command, if you include white space, you need to quote the string. Within (()), the (()) themselves act as quotes. Also, the leading $ in variable names is optional within these constructs.

    Comments on this post

    • additude agrees : Once Again... Thanks.
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Posts
    512
    Rep Power
    50
    Thanks.

    Great Help.

IMN logo majestic logo threadwatch logo seochat tools logo