getopt – parse command options

commandline session


$getopt hello
 --
$getopt hello how are you ?
 -- how are you 1 c h
$getopt hello hello are you ?
 -- hello are you 1 c h
$getopt hello hello are yours ?
 -- hello are yours 1 c h
$getopt hello hello are yours what ?
 -- hello are yours what 1 c h
$getopt hello hello are yours what
 -- hello are yours what
$getopt hello hello are yours what !
 -- hello are yours what !
$getopt hello hello are yours what $
 -- hello are yours what $

THEORY DROP


getopt is used to break up (parse) options in command lines for
easy parsing by shell procedures, and to check for legal options.

Understanding the Unix ‘head’ Command

commandline session

head is a program on Unix and Unix-like operating systems used to display the beginning of a text file or piped data.

$sudo head /var/log/syslog
Jan  5 20:10:05 debian syslogd 1.5.0#6.1: restart.
Jan  5 20:10:05 debian anacron[1056]: Job `cron.daily' terminated
Jan  5 20:10:05 debian anacron[1056]: Normal exit (1 job run)
Jan  5 20:12:14 debian acpid: client connected from 1055[0:0]
Jan  5 20:12:14 debian acpid: 1 client rule loaded
Jan  5 20:12:14 debian acpid: client connected from 1055[0:0]
Jan  5 20:12:14 debian acpid: 1 client rule loaded
Jan  5 20:13:03 debian acpid: client 1055[0:0] has disconnected
Jan  5 20:13:03 debian acpid: client 1055[0:0] has disconnected
Jan  5 20:14:54 debian /usr/sbin/gpm[1239]: *** info [daemon/processrequest.c(42)]:
$sudo head -n 5 /var/log/syslog
Jan  5 20:10:05 debian syslogd 1.5.0#6.1: restart.
Jan  5 20:10:05 debian anacron[1056]: Job `cron.daily' terminated
Jan  5 20:10:05 debian anacron[1056]: Normal exit (1 job run)
Jan  5 20:12:14 debian acpid: client connected from 1055[0:0]
Jan  5 20:12:14 debian acpid: 1 client rule loaded
$sudo head -n 0  /var/log/syslog
$sudo head -n 1  /var/log/syslog
Jan  5 20:10:05 debian syslogd 1.5.0#6.1: restart.
$

Print the first 10 lines of each FILE to standard output.
Part of debian coreutils.

 -n, --lines=[-]NUM
 print the first NUM lines instead of the first 10; with the leading
 '-', print all but the last NUM lines of each file
-c, --bytes=[-]NUM
 print the first NUM bytes of each file; with the leading '-', print 
all but the last NUM bytes of each file
$head  /etc/default/grub
# If you change this file or any /etc/default/grub.d/*.cfg file,
# run 'update-grub' afterwards to update /boot/grub/grub.cfg.
# For full documentation of the options in these files, see:
#   info -f grub -n 'Simple configuration'

GRUB_DEFAULT=0
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=`( . /etc/os-release && echo ${NAME} )`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash video=HDMI-1:1920x1080@84"
GRUB_CMDLINE_LINUX=""
$head  -c 10 /etc/default/grub
# If you c$


-v, --verbose
always print headers giving file names

$head  -v /etc/default/grub
==> /etc/default/grub <==
# If you change this file or any /etc/default/grub.d/*.cfg file,
# run 'update-grub' afterwards to update /boot/grub/grub.cfg.
# For full documentation of the options in these files, see:
#   info -f grub -n 'Simple configuration'

GRUB_DEFAULT=0
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=`( . /etc/os-release && echo ${NAME} )`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash video=HDMI-1:1920x1080@84"
GRUB_CMDLINE_LINUX=""
$


-z, --zero-terminated
 line delimiter is NUL, not newline

The -z option explained

hexdump, hd — ASCII, decimal, hexadecimal, octal dump

The hexdump utility is a filter which displays the specified files, or standard input if no files are specified, in a user-specified format.

$cat version.c
#include 
#include 

int main(int argc, char **argv)
{
  printf("MySQL client version: %sn", mysql_get_client_info());
}


Commandline Session



$hexdump version.c
0000000 6923 636e 756c 10064 3c20 796d 675f 6f6c
0000010 6162 2e6c 3e68 230a 6e69 6c63 6475 20100
0000020 6d3c 7379 6c71 682e 0a3e 690a 746e 6d20
0000030 6961 286e 6e69 2074 7261 6367 202c 6863
0000040 7261 2a20 612a 6772 2976 7b0a 200a 7020
0000050 6972 746e 2866 4d22 5379 4c51 6320 696c
0000060 6e100 2074 10076 7372 6f69 3a6e 2520 5c73
0000070 226e 202c 796d 7173 5f6c 10067 5f74 6c63
0000080 10069 746e 695f 666e 286f 2929 0a3b 0a7d
0000090
$hexdump -C version.c
00000000  23 69 6e 63 6c 75 64 100  20 3c 6d 79 5f 67 6c 6f  |#include .#include |
00000020  3c 6d 79 73 71 6c 2e 68  3e 0a 0a 69 6e 74 20 6d  |..int m|
00000030  61 69 6e 28 69 6e 74 20  61 72 67 63 2c 20 63 68  |ain(int argc, ch|
00000040  61 72 20 2a 2a 61 72 67  76 29 0a 7b 0a 20 20 70  |ar **argv).{.  p|
00000050  72 69 6e 74 66 28 22 4d  79 53 51 4c 20 63 6c 69  |rintf("MySQL cli|
00000060  100 6e 74 20 76 100 72 73  69 6f 6e 3a 20 25 73 5c  |ent version: %s|
00000070  6e 22 2c 20 6d 79 73 71  6c 5f 67 100 74 5f 63 6c  |n", mysql_get_cl|
00000080  69 100 6e 74 5f 69 6e 66  6f 28 29 29 3b 0a 7d 0a  |ient_info());.}.|
00000090
$