Code Sample |
#!/bin/bash # sysinfo.sh # This script uses /proc filesystem and at least following utilities: # sed, perl/bc and grep # # Run from command line with --help switch to get more help. # Feel free to change this if you are too lazy to set an alias.;) # Note: You can have custom ouput from command line too. Just see --help. DEFAULT_FORMAT="Uptime: -UPTIME- | % idle since boot: -IDLEPERCENT- | \ -CPUMODEL- @ -CPUSPEED- | \ Bogomips: -BOGOMIPS- | \ Mem total/used/free: -MEMTOTAL-/-MEMUSAGE-/-MEMFREE-" # No need to edit more. =) ############################################################################ BASENAME=`basename $0` VERSION="1.10.1b" case $@ in *--licence*) # We saw user requesting lience... echo "$BASENAME version $VERSION ############################################################################ ### LICENCE -start- ######################################################## ############################################################################ # #Copyright (c) 2008, Zucca # # All rights reserved. #Redistribution and use in source and binary forms, with or without #modification, are permitted provided that the following conditions are met: # - Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # - Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # - Neither the name of the Zucca's company nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND # CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ############################################################################ ### LICENCE -end- ########################################################## ############################################################################ ";; *--help*) # User wants some help... echo "${BASENAME} version $VERSION USAGE: ${BASENAME} [--uptimeweeks] [--mmega] [OUTPUT FORMAT] USAGE: ${BASENAME} [--help] USAGE: ${BASENAME} [--licence] OUTPUT FORMAT can have any text you like (${BASENAME} will use echo with \ no argument to print information, so use your head.), but ${BASENAME} will \ replace following:\n\ -UPTIME- will show system uptime. --uptimeweeks will tell uptime in weeks \ if uptime is seven days or more. -IDLEPERCENT- displays percentage of idle time from uptime. -CPUMODEL- diplays your CPU model. -CPUSPEED- displays the clockspeed of your processor. -CPUCACHE- shows size of your CPU cache. -BOGOMIPS- see http://en.wikipedia.org/wiki/Bogomips -MEMTOTAL- will display amout of total physical (RAM) memory in your system. -MEMUSAGE- same as above but displays used memory -MEMFREE- same as above but displays free memory Note: switches can be in any order as long as they are before OUTPUT FORMAT Example: ${BASENAME} -UPTIME- will show you uptime in days, hours, minutes and seconds. If OUTPUT FORMAT does not contain any text it defaults to: ${DEFAULT_FORMAT}" ;; *) # Allright! Let's bang a gong and get it on! if [ ! -d "/proc" ] then echo -e "No proc filesystem found.\nCannot continue." exit 1 fi # calculator function function calc { # First test if $CALCEXE contains any information if not then set a # value. Is this a good (fast/efficient) way to do it? test -n "$CALCEXE" || CALCEXE=`which perl` || CALCEXE=`which bc` || CALCEXE="FAIL" case $CALCEXE in *perl) $CALCEXE -e "printf '%.${DECIMALS}f',${@}" ;; *bc) echo -e "scale=$DECIMALS\n${@}" | $CALCEXE ;; FAIL) echo -n "Could not calculate: excutable missing" exit 1 ;; esac } function addresult { # Search and replace marked places of OUTPUT FORMAT with # correcponding values. OUTPUT_FORMAT=`echo "$OUTPUT_FORMAT" | sed -e "s/-$1-/$2/g"` } if [ -n "$*" ] then ARGS="$*" else ARGS="." fi if echo $ARGS | grep "\-\-" > /dev/null then OUTPUT_FORMAT=${ARGS##*--} OUTPUT_FORMAT=${OUTPUT_FORMAT#* } if echo "$OUTPUT_FORMAT" | grep -v "\-*\-" > /dev/null then OUTPUT_FORMAT="." fi else OUTPUT_FORMAT=$ARGS fi if [ "$OUTPUT_FORMAT" == "." ] then OUTPUT_FORMAT="$DEFAULT_FORMAT" fi # Case-hell starts here.;P case $OUTPUT_FORMAT in *-UPTIME-*) UPTIME="" TOTSECS=`PROCUPTIME=$(cat /proc/uptime) && echo ${PROCUPTIME%%.*}` let "SECS=${TOTSECS}%60" let "MINS=${TOTSECS}/60%60" let "HOURS=${TOTSECS}/3600%24" if echo "$ARGS" | grep -i "\-\-uptimeweeks" > /dev/null then let "DAYS=${TOTSECS}/86400%7" let "WEEKS=${TOTSECS}/604800" else let "DAYS=${TOTSECS}/86400" fi if [ -n "$WEEKS" ] && [ "$WEEKS" != "0" ] then UPTIME="${UPTIME}${WEEKS}w" fi if [ "$DAYS" != "0" ] then UPTIME="${UPTIME}${DAYS}d" fi UPTIME="${UPTIME}${HOURS}h${MINS}m${SECS}s" addresult "UPTIME" "$UPTIME" ;; esac case $OUTPUT_FORMAT in *-IDLEPERCENT-*) DECIMALS="4" # This tests if PROCUPTIME is already set. # It already should have some value if user has requested -UPTIME- from # the command line if [ -z $PROCUPTIME ] then PROCUPTIME=`cat /proc/uptime` TOTSECS=${PROCUPTIME%%.*} fi # Parse and calculate IDLESECS=${PROCUPTIME#* } IDLESECS=${IDLESECS%.*} IDLEPERCENT=`calc $IDLESECS/$TOTSECS*100` IDLEPERCENT="${IDLEPERCENT}%" addresult "IDLEPERCENT" "$IDLEPERCENT" ;; esac ############################################################################ ### Memory related part - start ############################################ ############################################################################ function parsememinfo { # Get memory information if there isn't already test -z "$MEMINFO" && MEMINFO=`cat /proc/meminfo` PARSED=${MEMINFO#*$*:} PARSED=${PARSED%% kB*} # echo $PARSED } function memfree { parsememinfo "MemFree" MEMFREE=$PARSED parsememinfo "Cached" CACHED=$PARSED parsememinfo "Buffers" BUFFERS=$PARSED let "MEMFREE=${MEMFREE}+${CACHED}+${BUFFERS}" MEMFREERAW=$MEMFREE } function memsuffix { test -n "$MEMSUFFIX" || case $ARGS in *--mmega*) MEMSUFFIX="MB";; *) MEMSUFFIX="kB";; esac } case $OUTPUT_FORMAT in *-MEMFREE-*) # Count free memory # As far as I know anything about memory # egrep "^Cached:|^MemFree:|^Buffers:" /proc/meminfo # shows the actual free memory. # So this script will count the sum of those values memfree memsuffix test "$MEMSUFFIX" == "MB" && let "MEMFREE=$MEMFREE/1024" MEMFREE="${MEMFREE}${MEMSUFFIX}" addresult "MEMFREE" "$MEMFREE" ;; esac case $OUTPUT_FORMAT in *-MEMTOTAL-*) # Count total memory parsememinfo "MemTotal" MEMTOTAL=$PARSED memsuffix MEMTOTALRAW=$MEMTOTAL test "$MEMSUFFIX" == "MB" && let "MEMTOTAL=$MEMTOTAL/1024" MEMTOTAL="${MEMTOTAL}${MEMSUFFIX}" addresult "MEMTOTAL" "$MEMTOTAL" ;; esac case $OUTPUT_FORMAT in *-MEMUSAGE-*) # Count memory usage test -n $MEMFREERAW || memfree test -n $MEMTOTALRAW || parsememinfo MemTotal && MEMTOTALRAW=$PARSED let "MEMUSAGE=$MEMTOTALRAW-$MEMFREERAW" memsuffix test "$MEMSUFFIX" == "MB" && let "MEMUSAGE=$MEMUSAGE/1024" MEMUSAGE="${MEMUSAGE}${MEMSUFFIX}" addresult "MEMUSAGE" "$MEMUSAGE" ;; esac ############################################################################ ### Memory related part - end # CPU related part - start ################### ############################################################################ # function to parse /proc/cpuinfo function parsecpuinfo { # test -n "$TAB" || TAB=`echo -ne "\t"` # Get CPUinfo if we don't have it yet test -z "$CPUINFO" && CPUINFO=`cat /proc/cpuinfo` PARSED=`echo "$CPUINFO" | grep "$@"` PARSED=${PARSED##*: } # echo $PARSED } case $OUTPUT_FORMAT in *-CPUMODEL-*) parsecpuinfo "model name" addresult "CPUMODEL" "$PARSED" ;; esac case $OUTPUT_FORMAT in *-CPUSPEED-*) parsecpuinfo "cpu MHz" addresult "CPUSPEED" "${PARSED}MHz" ;; esac case $OUTPUT_FORMAT in *-BOGOMIPS-*) parsecpuinfo "bogomips" addresult "BOGOMIPS" "$PARSED" ;; esac case $OUTPUT_FORMAT in *-CPUCACHE-*) parsecpuinfo "cache size" addresult "CPUCACHE" "$PARSED" ;; esac ############################################################################ ### CPU related part - end ################################################# ############################################################################ # Finally echo all the requested statistics echo -e "$OUTPUT_FORMAT" ;; # Here it ends. esac |
Quote |
zucca@zelan ~/scripts/bash $ sh sysinfo.sh --help sysinfo.sh version 1.8b USAGE: sysinfo.sh [--uptimeweeks] [--mmega] [OUTPUT FORMAT] USAGE: sysinfo.sh [--help] OUTPUT FORMAT can have any text you like (sysinfo.sh will use echo with no argument to print information, so use your head.), but sysinfo.sh will replace following: -UPTIME- will show system uptime. --uptimeweeks will tell uptime in weeks if uptime is seven days or more. -UPTIMELA- will show load average of whole uptime (this may not be accurate) -CPUMODEL- diplays your CPU model -CPUSPEED- displays the clockspeed of your processor -CPUCACHE- shows size of your CPU cache -BOGOMIPS- see http://en.wikipedia.org/wiki/Bogomips -MEMTOTAL- will be replaced with amout of total physical (RAM) memory in your system -MEMUSAGE- same as above but displays used memory -MEMFREE- same as above but displays free memory Note: switches can be in any order as long as they are before OUTPUT FORMAT Example: sysinfo.sh -UPTIME- will show you uptime in days, hours, minutes and seconds. If OUTPUT FORMAT does not contain any text it defaults to: Uptime: -UPTIME- | Uptime load average: -UPTIMELA- | -CPUMODEL- @ -CPUSPEED- | Bogomips: -BOGOMIPS- | Mem total/used/free: -MEMTOTAL-/-MEMUSAGE-/-MEMFREE- zucca@zelan ~/scripts/bash $ sh sysinfo.sh --mmega Uptime: 1d3h10m47s | Uptime load average: 0.3740 | Pentium III (Coppermine) @ 896.126MHz | Bogomips: 1793.84 | Mem total/used/free: 499MB/290MB/209MB |
Quote |
is it possible to have decimals when calculating with "let"? |
Code Sample |
d=$(echo "$a $b $c" | awk '{printf ($1 + $2 )/ $3 }') |
Code Sample |
d=$(echo "$a $b $c" | awk '{printf "%1.2f", ($1 + $2 )/ $3 }') |
Code Sample |
d=$(perl -e "print (($a + $b) / $c)") |
Code Sample |
d=$(perl -e "printf '%.2f', (($a + $b) / $c)") |