Mandalika's scratchpad [ Work blog @Oracle | My Music Compositions ]

Old Posts: 09.04  10.04  11.04  12.04  01.05  02.05  03.05  04.05  05.05  06.05  07.05  08.05  09.05  10.05  11.05  12.05  01.06  02.06  03.06  04.06  05.06  06.06  07.06  08.06  09.06  10.06  11.06  12.06  01.07  02.07  03.07  04.07  05.07  06.07  08.07  09.07  10.07  11.07  12.07  01.08  02.08  03.08  04.08  05.08  06.08  07.08  08.08  09.08  10.08  11.08  12.08  01.09  02.09  03.09  04.09  05.09  06.09  07.09  08.09  09.09  10.09  11.09  12.09  01.10  02.10  03.10  04.10  05.10  06.10  07.10  08.10  09.10  10.10  11.10  12.10  01.11  02.11  03.11  04.11  05.11  07.11  08.11  09.11  10.11  11.11  12.11  01.12  02.12  03.12  04.12  05.12  06.12  07.12  08.12  09.12  10.12  11.12  12.12  01.13  02.13  03.13  04.13  05.13  06.13  07.13  08.13  09.13  10.13  11.13  12.13  01.14  02.14  03.14  04.14  05.14  06.14  07.14  09.14  10.14  11.14  12.14  01.15  02.15  03.15  04.15  06.15  09.15  12.15  01.16  03.16  04.16  05.16  06.16  07.16  08.16  09.16  12.16  01.17  02.17  03.17  04.17  06.17  07.17  08.17  09.17  10.17  12.17  01.18  02.18  03.18  04.18  05.18  06.18  07.18  08.18  09.18  11.18  12.18  01.19  02.19  05.19  06.19  08.19  10.19  11.19  05.20  10.20  11.20  12.20  09.21  11.21  12.22 


Friday, August 31, 2018
 
Random Solaris & Shell Command Tips — kstat, tput, sed, digest

The examples shown in this blog post were created and executed on a Solaris system. Some of these tips and examples are applicable to all *nix systems.

Digest of a File

One of the typical uses of computed digest is to check if a file has been compromised or tampered. The digest utility can be used to calculate the digest of files.

On Solaris, -l option lists out all cryptographic hash algorithms available on the system.

eg.,
% digest -l
sha1
md5
sha224
sha256
..
sha3_224
sha3_256
..

-a option can be used to specify the hash algorithm while computing the digest.

eg.,
% digest -v -a sha1 /usr/lib/libc.so.1
sha1 (/usr/lib/libc.so.1) = 89a588f447ade9b1be55ba8b0cd2edad25513619

Multiline Shell Script Comments

Shell treats any line that start with '#' symbol as a comment and ignores such lines completely. (The line on top that start with #! is an exception).

From what I understand there is no multiline comment mechanism in shell. While # symbol is useful to mark single line comments, it becomes laborious and not well suited to comment quite a number of contiguous lines.

One possible way to achieve multiline comments in a shell script is to rely on a combination of shell built-in ':' and Here-Document code block. It may not be the most attractive solution but gets the work done.

Shell ignores the lines that start with a ":" (colon) and returns true.

eg.,
% cat -n multiblock_comment.sh
     1  #!/bin/bash
     2
     3  echo 'not a commented line'
     4  #echo 'a commented line'
     5  echo 'not a commented line either'
     6  : <<'MULTILINE-COMMENT'
     7  echo 'beginning of a multiline comment'
     8  echo 'second line of a multiline comment'
     9  echo 'last line of a multiline comment'
    10  MULTILINE-COMMENT
    11  echo 'yet another "not a commented line"'

% ./multiblock_comment.sh
not a commented line
not a commented line either
yet another "not a commented line"

tput Utility to Jazz Up Command Line User Experience

The tput command can help make the command line terminals look interesting. tput can be used to change the color of text, apply effects (bold, underline, blink, ..), move cursor around the screen, get information about the status of the terminal and so on.

In addition to improving the command line experience, tput can also be used to improve the interactive experience of scripts by showing different colors and/or text effects to users.

eg.,
% tput bold <= bold text
% date
Thu Aug 30 17:02:57 PDT 2018
% tput smul <= underline text
% date
Thu Aug 30 17:03:51 PDT 2018
% tput sgr0 <= turn-off all attributes (back to normal)
% date
Thu Aug 30 17:04:47 PDT 2018

Check the man page of terminfo for a complete list of capabilities to be used with tput.

Processor Marketing Name

On systems running Solaris, processor's marketing or brand name can be extracted with the help of kstat utility. cpu_info module provides information related to the processor(s) on the system.

eg.,
On SPARC:

% kstat -p cpu_info:1:cpu_info1:brand
cpu_info:1:cpu_info1:brand      SPARC-M8

On x86/x64:

% kstat -p cpu_info:1:cpu_info1:brand
cpu_info:1:cpu_info1:brand      Intel(r) Xeon(r) CPU           L5640  @ 2.27GHz

In the above example, cpu_info is the module. 1 is the instance number. cpu_info1 is the name of the section and brand is the statistic in focus. Note that cpu_info module has only one section cpu_info1. Therefore it is fine to skip the section name portion (eg., cpu_info:1::brand).

To see the complete list of statistics offered by cpu_info module, simply run kstat cpu_info:1.

Consolidating Multiple sed Commands

sed utility allows specifying multiple editing commands on the same command line. (in other words, it is not necessary to pipe multiple sed commands). The editing commands need to be separated with a semicolon (;)

eg.,

The following two commands are equivalent and yield the same output.

% prtconf | grep Memory | sed 's/Megabytes/MB/g' | sed 's/ size//g'
Memory: 65312 MB

% prtconf | grep Memory | sed 's/Megabytes/MB/g;s/ size//g'
Memory: 65312 MB

Labels:





2004-2019 

This page is powered by Blogger. Isn't yours?