LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Hardware (https://www.linuxquestions.org/questions/linux-hardware-18/)
-   -   Find device name for "ata5" (https://www.linuxquestions.org/questions/linux-hardware-18/find-device-name-for-ata5-882229/)

PTrenholme 02-11-2014 12:33 PM

Sorry for the long delay (I'm 74 yr.s old so a "minor" cold, isn't). I'll need to look at your script a bit longer, but, to start with, a minor quibble:

The idiom you used to have awk print (i.e.: grep <re> | awk '{print ...}') involves an extra "pipe" step. A simple awk '/<re>/{print ...}' does the same thing and is, generally, much more flexible.

More later. (I'm still coughing...)

<edit>
If you were to use gawk instead of mixing your languages, you might find life simplier.

Here's simple a rewrite of the non-distro-specific part of your script to get you started, if you wish. (I don't have a Debian distro installed, so I don't have a /var/log/kern file with which to play.)

Note the the uptime on my Fedora system accepts arguments:
Code:

$ uptime -h

Usage:
 uptime [options]

Options:
 -p, --pretty  show uptime in pretty format
 -h, --help    display this help and exit
 -s, --since    system up since
 -V, --version  output version information and exit

For more details see uptime(1).

Code:

#!/bin/gawk -f
# Get disk model/serial numbers, etc
#
BEGIN {
  cmd = "ls -l /dev/disk/by-id"
  while (cmd | getline idinfo) {
    nf=split(idinfo, Info)
    if (nf != 11) continue
    device="/dev/" substr(Info[11],7)
    if (length(device) > mdev) mdev=length(device)
    if ((device ~ /[[:digit:]]$/) && (Info[9] ~ /-part/)) {continue}
    description = substr(Info[9],1+match(Info[9],/-/))
    if (length(description) > max) max=length(description)+2
    Data[device][1+length(Data[device])]=description
  }
  close(cmd)
  ndev=asorti(Data,SData)
  for (i=1;i<=ndev;++i) {
    printf("%-" mdev "s  ",SData[i])
    for (j in Data[SData[i]]) {printf("%-" max "s",Data[SData[i]][j])}
    printf("\n")
  }
}

# Get the date of the last boot and the time the system has been up
BEGIN {
  cmd="uptime -s"
  if (cmd | getline boot_time) {
    if (match(boot_time,/[[:space:]]+/)) {
      printf("\nLast boot: %s at %s.\n", substr(boot_time,1,RSTART-1), substr(boot_time,RSTART+RLENGTH))
      # Get the time the system has been running
    }
    else {
      printf("/nUnexpected return (\"%s\") from \"%s\".\n", uptime, cmd)
    }
    close(cmd)
  }
  cmd="uptime -p"
  if (cmd | getline run_time) {
    if (match(run_time,/[[:space:]]+/)) {
      printf("Running for %s.\n", substr(run_time,RSTART+RLENGTH))
    }
    else {
      printf("/nUnexpected return (\"%s\") from \"%s\".\n", run_time, cmd)
    }
    close(cmd)
  }
}

Which, on my Fedora system shows (after saving the script and making it executable):
Code:

$ ./affinityvision.gawk
/dev/md126  name-HP-p6710f:127                        uuid-31ae217a:e7564465:9e527f40:40dcd2b8 
/dev/sda    ST31000528AS_6VPBQVS7                    0x5000c50032d81e7f                       
/dev/sdb    ST2000DL003-9VT166_5YD3C2FZ              0x5000c500385662bf                       
/dev/sdc    WDC_WD20EARX-00PASB0_WD-WMAZA8382941      0x50014ee25c328225                       
/dev/sdd    Generic-_SD_MMC_18E391066476-0:0         
/dev/sde    Generic-_Compact_Flash_18E391066476-0:1 
/dev/sdf    Generic-_SM_xD-Picture_18E391066476-0:2 
/dev/sdg    Generic-_MS_MS-Pro_18E391066476-0:3     
/dev/sr0    hp_DVD_A_DH16ABLH_3L8108904593           

Last boot: 2014-02-15 at 10:44:02.
Running for 2 hours, 23 minutes.

</edit>


All times are GMT -5. The time now is 12:35 PM.