Linux/dev

From zeroincombenze
Jump to: navigation, search
Tb arrow up.jpg File:Tb dev.png File:Tb dev.png File:Tb dev.png File:Tb dev.png Lang english.png


Linux general purpose library

Benvenuto! Qui trovi le informazioni su una semplice libreria bash che ti permette di sviluppare in modo prefessionale.

Puoi copiare il file z0librc nella tua home directory oppure in /etc directory (se preferisci che sia sempre disponibile); poi potrai usare tutte le funzioni di z0librc con una semplice dichiarazione nel header del tuo script come in questo esempio:

if [ -f z0librc ]; then
# Import z0librc from current directory
. ./z0librc
elif [ -f ~/z0librc ]; then
# Import z0librc from home directory
. ~/z0librc
else
# Import z0librc from /etc directory
. /etc/z0librc
fi


Le funzioni disponibili sono:

Function name Purpose
xuname Detect and print more OS informations than uname command
parseoptargs Parse command line arguments in a professional way
print_help Print help for parse command line arguments


xuname

Detect and print more OS informations than uname command

Print Linux OS system information in same way of uname command but returns more information.

Based on Novell article http://www.novell.com/coolsolutions/feature/11251.html

Credits:

  1. Arun Singh (Novell Senior Software Engineer)
  2. antoniomaria.vigliotti@gmail.com (zeroincombenze® Senior Software Engineer)
  3. giuliano69 (https://github.com/Giuliano69/odoo_install/blob/master/odoo8_install.sh)

Check history:

Date Author xname -a xname -f
2015-08-27 antoniov Linux CentOS 6.7(Final 2.6.32-504.16.2.el6.x86_64 x86_64) RHEL
2015-08-27 antoniov Linux CentOS 7.1.1503(Core 3.10.0-229.1.2.el7.x86_64 x86_64) RHEL
2015-08-27 antoniov Linux Ubuntu 12.04( 3.13.0-32-generic i686) Debian
Parameter:
-a             print all
-c             print Linux distribution codename (empty on Unix)
-d             print Linux distribution name (empty on Unix)
-f             print Linux family (RHEL or Debian, empty on Unix)
-i             print architecture
-k             print Linux kernel release (on Unix same as -r)
-m             print machine hardware name
-p             print processor architecture
-r             print kernel release
-s             print kernel name
-v             print distribution version (only on Linux)
-V             show xuname version
-x             print Linux distribution description (empty on Unix)


Sample:

#!/bin/bash
. ~/z0librc
 
f=$(xuname "-f")
if [ "$f" == "RHEL" ]; then
    echo "Found RedHat family OS"
elif [ "$f" == "Debian" ]; then
    echo "Found Debian family OS"
else
    echo "No Linux, maybe Unix or Posix"
fi


Note per Ubuntu:

Ubuntu dispone del comandp lsb_release per visualizzare le stesse informazioni. Qui trovate l'equivalenza delle opzioni di xuname e lsb_release:

Parametro xuname su tutte le distribuzioni Linux Ubuntu
Distribution identification -d -i --id
Distribution version -v -r --release
Distribution codename -c -c --codename
Distribution description -x -d --description


parseoptargs

Parse command line arguments in a professional way

You can define syntax rules declaring some array variables, before calling this function. On exit, variable are set on appropriate way.

Global variables to declare
  OPTOPTS  array with option switch characters (do not forget h for help)
     Common option (without -, description and default variable):
     h help       Show help               opt_help
     n no         Do nothing (only test)  opt_dry_run
     V version    Show app version        opt_version
  OPTDEST  array of destination variables (matches one to one OPTOPTS)
     Recommend variables:
     opt_help     show help (int)
     opt_version  show app version (string)
     opt_dry_run  do nothing (int)
  OPTACTI  array of action (matches one to one OPTOPTS)
     1.st character:
     0    Set variable to zero (false)
     1    Set variable to 1 (true)
     *    Set variable with option switch name itself (i.e. -h)
     =    Set variable to following option value
     2.nd character:
     >    executive command (disable help and version)
  OPTDEFL  array with defalt values (matches one to one OPTOPTS)
  OPTMETA  array with meta help to build help (matches one to one OPTOPTS)
  OPTHELP  array with parameters description to build help (matches one to one OPTOPTS)
  OPTARGS  array of destination variables of positional arguments (set OPTARGS=() if no args)


Sample:

#!/bin/bash
. ~/z0librc
 
__version__=0.1.2
OPTOPTS=(h        n            V           v           q           p        t)
OPTDEST=(opt_help opt_dry_run  opt_version opt_verbose opt_verbose opt_path opt_test)
OPTACTI=(1        "1>"         "*>"        1           0           "=>"     0)
OPTDEFL=(1        0            ""          0           0           ~/       0)
OPTMETA=("help"   "do nothing" "version"   "verbose"   "silent"    "path"   "n/a")
OPTHELP=("this help"\
 "do nothing (dry-run)"\
 "show version"\
 "verbose mode"\
 "silent mode"\
 "destination path"\
 "N/A")
OPTARGS=(src tgt)
parseoptargs $@
 
echo "help    = <$opt_help>"
echo "dry_run = <$opt_dry_run>"
echo "version = <$opt_version>"
echo "verbose = <$opt_verbose>"
echo "path    = <$opt_path>"
echo ""
echo "#1      = <$src>"
echo "#2      = <$tgt>"
 
if [ "$opt_version" ]
then
  echo "$__version__"
  exit 0
fi
if [ $opt_help -gt 0 ]
then
  print_help "Sample for optargs"\
  "(C) 2015 by zeroincombenze(R)\nhttp://wiki.zeroincombenze.org/en/Linux/dev\nAuthor: antoniomaria.vigliotti@gmail.com"
  exit 0
fi

print_help

Print help for parse command line arguments

See above parseoptargs.

If you run above sample, result of -h option switch is:

Usage: optargs [-hnVvq][-p path][-t] src tgt
Sample for optargs
 -h             this help
 -n             do nothing (dry-run)
 -V             show version
 -v             verbose mode
 -q             silent mode
 -p path        destination path
 -t             N/A

(C) 2015 by zeroincombenze(R)
http://wiki.zeroincombenze.org/en/Linux/dev
Author: antoniomaria.vigliotti@gmail.com

Sample for xuname and optargs

#!/bin/bash
. ~/z0librc
 
# Detect and print more OS informations than uname command
# Simple how to use z0librc library 
# Copyright (C) SHS-AV s.r.l. (<http://ww.zeroincombenze.it>)
# This software is free software under GNU Affero GPL3
 
 
# Main program
OPTOPTS=(h        a       d       f        i       k       m       p       r       s       v       V)
OPTDEST=(opt_help opt_prm opt_prm opt_prm  opt_prm opt_prm opt_prm opt_prm opt_prm opt_prm opt_prm opt_version)
OPTACTI=(1        "*>"    "*>"    "*>"     "*>"    "*>"    "*>"    "*>"    "*>"    "*>"    "*>"    "*>")
OPTDEFL=(1        ""      ""      ""       ""      ""      ""      ""      ""      ""      ""      "")
OPTMETA=("help"   "all"   "dist"  "family" "arch"  "kver"  "mach"  "arch"  "ver"   "kname" "ver"   "version")
OPTHELP=("this help"\
 "print all"\
 "print Linux distribution name (empty on Unix)"\
 "print Linux family (RHEL or Debian, empty on Unix)"\
 "print architecture"\
 "print Linux kernel release (on Unix same as -r)"\
 "print machine hardware name"\
 "print processor architecture"\
 "print kernel release"\
 "print kernel name"\
 "print distribution version (only on Linux)"\
 "show xuname version")
OPTARGS=(src tgt)
parseoptargs $@
 
__version__="0.1.5"
if [ "$opt_version" ]
then
  echo "$__version__"
  exit 0
fi
if [ $opt_help -gt 0 ]
then
  print_help "Print system information"\
  "(C) 2015 by zeroincombenze(R)\nhttp://wiki.zeroincombenze.org/en/Linux/dev\nAuthor: antoniomaria.vigliotti@gmail.com"
  exit 0
fi
xuname $opt_prm
exit 0