Advertisement
Guest User

A script to determine GPU and its RAM under Linux

a guest
Apr 20th, 2011
1,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.39 KB | None | 0 0
  1. # A bash script to determine GPU and its RAM (under Linux)
  2. # Prerequisites: bash, sed, awk, lshw
  3. # (c) 2010 Artem S. Tashkinov. Licensed under GPLv2 :)
  4.  
  5. #! /bin/bash
  6.  
  7. if ! lshw -version &> /dev/null; then
  8.         echo "Please, install lshw utility"
  9.         exit 0
  10. fi
  11.  
  12. ramregions()
  13. {
  14.         ram=`sed 's/(prefetchable)//g' | awk '{for (i=1;i<=NF;i++) if ($i ~ "memory") print $i}' | awk -F : '{print $2}' | tr a-z A-Z`
  15.         echo -e "ibase=16;\n$ram" | bc | awk '{sum+=$1}END{print (0-sum)/1024/1024"MB"}'
  16. }
  17.  
  18. cpu2gpu()
  19. {
  20.         if echo "$@" | grep -q "Core Processor"; then
  21.                 echo `grep "model name" /proc/cpuinfo | head -1 | awk -F : '{print $2}' | sed 's/[ ]\{2,\}/ /g;s/^ *//;s/ *$//'`" Integrated Graphics"
  22.         else
  23.                 echo "$@"
  24.         fi
  25. }
  26.  
  27. outfile=`mktemp` || exit 1
  28. lshw -class display -disable dmi -quiet &> "$outfile" || exit 2
  29.  
  30. cat "$outfile" | while read a; do
  31.         case "$a" in
  32.                 *product:*)
  33.                         gpu=`echo $a | awk -F 'product: ' '/product:/ {print $2}'`
  34.                 ;;
  35.                 *memory:*)
  36.                         ram=`echo $a | ramregions`
  37.                 ;;
  38.         esac
  39.         if [ -n "$gpu" -a -n "$ram" ]; then
  40.                 gpu=`cpu2gpu "$gpu"`
  41.                 echo "GPU: [$gpu] RAM: [$ram]"
  42.                 unset gpu
  43.                 unset ram
  44.         fi
  45. done
  46.  
  47. /bin/rm $outfile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement