Command Line Processing

By High School Technology Services myhsts.org Recap From Previous Session

In previous session we learned . We covered –

▪ Process Ownership ▪ suid and sgid ▪ Restricted Shells ▪ Other Security Features ▪ The newgrp ▪ Statement blocks ▪ case Statement

Outline

▪ Getting Data Into Scripts ▪ Manipulating Positional Parameters (set, , shift) ▪ Analyzing Switches with getopts ▪ Options ▪ Reserved Variables (OPTARG, OPTIND, IFS) ▪ Setting Default Values Getting Data Into Scripts

Hello, am I talking to? read varname echo It\'s to meet you $varname Manipulating Positional Parameters (set, sort, shift) set is a built-in function of the (sh), C shell (csh), and Korn shell (ksh), which is used to define and determine the values of the system environment. set [+-aefhkmnopstuvx] [+-o option]... [+-A name] [arg...] https://www.computerhope.com/unix/uset.htm Manipulating Positional Parameters (set, sort, shift) sort is a simple and very useful command which will rearrange the lines in a text so that they are sorted, numerically and alphabetically. By default, the rules for sorting are:

Lines starting with a number will appear before lines starting with a letter. Lines starting with a letter that appears earlier in the alphabet will appear before lines starting with a letter that appears later in the alphabet. Lines starting with a lowercase letter will appear before lines starting with the same letter in uppercase. https://www.computerhope.com/unix/usort.htm Analyzing Switches with getopts

When you want to parse command line arguments in a professional way, getopts is the tool of choice. Unlike its older brother (note the missing s!), it's a command. The advantages are:

No need to pass the positional parameters through to an external program. Being a builtin, getopts can set shell variables to use for parsing (impossible for an external process!) There's no need to argue with several getopt implementations which had buggy concepts in the past (whitespace, …) getopts is defined in POSIX®. Reserved Variables (OPTARG, OPTIND, IFS)

The base-syntax for getopts is: getopts OPTSTRING VARNAME [ARGS...] where:

OPTSTRING tells getopts which options to expect and where to expect arguments (see below) VARNAME tells getopts which shell-variable to use for option reporting ARGS tells getopts to parse these optional words instead of the positional parameters Looking Forward

In the next session we will . We’ll cover - ▪ Improving the Performance of Scripts ▪ Timing Commands and Scripts ▪ System Resources ▪ Setting Limits ▪ Portability Issues Thank You.

By High School Technology Services myhsts.org