# 13-C.8: Positional Parameters / exec Command / source Command

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$

( \newcommand{\kernel}{\mathrm{null}\,}\) $$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\id}{\mathrm{id}}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\kernel}{\mathrm{null}\,}$$

$$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$

$$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$

$$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

$$\newcommand{\vectorA}[1]{\vec{#1}} % arrow$$

$$\newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow$$

$$\newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vectorC}[1]{\textbf{#1}}$$

$$\newcommand{\vectorD}[1]{\overrightarrow{#1}}$$

$$\newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}$$

$$\newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}}$$

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\avec}{\mathbf a}$$ $$\newcommand{\bvec}{\mathbf b}$$ $$\newcommand{\cvec}{\mathbf c}$$ $$\newcommand{\dvec}{\mathbf d}$$ $$\newcommand{\dtil}{\widetilde{\mathbf d}}$$ $$\newcommand{\evec}{\mathbf e}$$ $$\newcommand{\fvec}{\mathbf f}$$ $$\newcommand{\nvec}{\mathbf n}$$ $$\newcommand{\pvec}{\mathbf p}$$ $$\newcommand{\qvec}{\mathbf q}$$ $$\newcommand{\svec}{\mathbf s}$$ $$\newcommand{\tvec}{\mathbf t}$$ $$\newcommand{\uvec}{\mathbf u}$$ $$\newcommand{\vvec}{\mathbf v}$$ $$\newcommand{\wvec}{\mathbf w}$$ $$\newcommand{\xvec}{\mathbf x}$$ $$\newcommand{\yvec}{\mathbf y}$$ $$\newcommand{\zvec}{\mathbf z}$$ $$\newcommand{\rvec}{\mathbf r}$$ $$\newcommand{\mvec}{\mathbf m}$$ $$\newcommand{\zerovec}{\mathbf 0}$$ $$\newcommand{\onevec}{\mathbf 1}$$ $$\newcommand{\real}{\mathbb R}$$ $$\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}$$ $$\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}$$ $$\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}$$ $$\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}$$ $$\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}$$ $$\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}$$ $$\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}$$ $$\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}$$ $$\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}$$ $$\newcommand{\laspan}[1]{\text{Span}\{#1\}}$$ $$\newcommand{\bcal}{\cal B}$$ $$\newcommand{\ccal}{\cal C}$$ $$\newcommand{\scal}{\cal S}$$ $$\newcommand{\wcal}{\cal W}$$ $$\newcommand{\ecal}{\cal E}$$ $$\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}$$ $$\newcommand{\gray}[1]{\color{gray}{#1}}$$ $$\newcommand{\lgray}[1]{\color{lightgray}{#1}}$$ $$\newcommand{\rank}{\operatorname{rank}}$$ $$\newcommand{\row}{\text{Row}}$$ $$\newcommand{\col}{\text{Col}}$$ $$\renewcommand{\row}{\text{Row}}$$ $$\newcommand{\nul}{\text{Nul}}$$ $$\newcommand{\var}{\text{Var}}$$ $$\newcommand{\corr}{\text{corr}}$$ $$\newcommand{\len}[1]{\left|#1\right|}$$ $$\newcommand{\bbar}{\overline{\bvec}}$$ $$\newcommand{\bhat}{\widehat{\bvec}}$$ $$\newcommand{\bperp}{\bvec^\perp}$$ $$\newcommand{\xhat}{\widehat{\xvec}}$$ $$\newcommand{\vhat}{\widehat{\vvec}}$$ $$\newcommand{\uhat}{\widehat{\uvec}}$$ $$\newcommand{\what}{\widehat{\wvec}}$$ $$\newcommand{\Sighat}{\widehat{\Sigma}}$$ $$\newcommand{\lt}{<}$$ $$\newcommand{\gt}{>}$$ $$\newcommand{\amp}{&}$$ $$\definecolor{fillinmathshade}{gray}{0.9}$$

## Command Line Arguments/Positional Parameters

A command line argument is nothing but an argument sent to a program being called. A program can take any number of command line arguments. For example, type the following command:

pbmac@pbmac-server $ls grate_stories_of grate_stories_of: No such file or directory pbmac@pbmac-server$ ls great_stories_of
great_stories_of


There is no file or directory named grate_stories_of so the system issues an error message.

ls is the name of an actual command and shell executed this command when you type command at shell prompt. The first word on the command line is:

• ls - name of the command to be executed.
• Everything else on command line is taken as arguments to this command. (ALTHOUGH many texts, this course included, make a distinction between options and arguments - with options specifying behavior of the command, whereas the actual arguments specify what to apply the options to.)

Consider the following example:

pbmac@pbmac-server $tail +10 /path/to/file.txt  • tail: Command name. • +10 /path/to/file.txt: The arguments. ### Examples Try the following command and note down its command line arguments: Command Command name Total number of arguments Argument name(s) ls ls 0 N/A ls /etc/resolv.conf ls 1 /etc/resolv.conf cp /etc/resolv.conf /tmp/test.txt cp 2 /etc/resolv.conf, and /tmp/test.txt sort -r -n /path/to/file sort 3 -r, -n, and /path/to/file date +"%d-%m-%Y" date 1 +"%d-%m-%Y" ### Command Line Parameters Different Names A command line parameter is also known as: • Command line options • Options • Positional parameters • Flag • Switches or switch • Command-line arguments ### Why Use Command Line Arguments • Telling the command/utility which option to use. • Informing the utility/command which file or group of files to process (reading/writing of files). ## Accessing Command-Line Arguments All command line parameters (positional parameters) are available via special shell variable$1, $2,$3,...,$9. The simple shell script called cmdargs.sh is shown in this example: #!/bin/bash echo "The script name :$0"
echo "The value of the first argument to the script : $1" echo "The value of the second argument to the script :$2"
echo "The value of the third argument to the script : $3" echo "The number of arguments passed to the script :$#"
echo "The value of all command-line arguments (\$* version) :$*"
echo "The value of all command-line arguments (\$@ version) :$@"


When the script is run, the output looks like:

pbmac@pbmac-server $cmdargs.sh bmw ford toyota The script name : ./cmdargs.sh The value of the first argument to the script : bmw The value of the second argument to the script : ford The value of the third argument to the script : toyota The number of arguments passed to the script : 3 The value of all command-line arguments ($* version) : bmw ford toyota
The value of all command-line arguments ($@ version) : bmw ford toyota The following are additional examples; the results are in the table that follows. pbmac@pbmac-server$ ls /tmp
pbmac@pbmac-server $./math 10 + 3 pbmac@pbmac-server$ ~/scripts/addzone cyberciti.com
pbmac@pbmac-server $~/scripts/adddomain cyberciti.biz '74.86.48.99' '2607:f0d0:1002:11::4' pbmac@pbmac-server$ /etc/init.d/named reload
pbmac@pbmac-server $/usr/local/etc/rc.d/jail restart cyberciti.biz  Shell script name ($0) Total number of arguments ($#) Actual Command line argument ($1,..,$9) ls 1 /tmp ./math 3 10, +, and 3 ~/scripts/addzone 1 cyberciti.com ~/scripts/adddomain 3 cyberciti.biz, 74.86.48.99, and 2607:f0d0:1002:11::4 /etc/init.d/named reload 1 reload /usr/local/etc/rc.d/jail 2 restart, and cyberciti.biz ### The Difference Between$@ and $* •$@ expanded as "$1" "$2" "$3" ... "$n"
• $* expanded as "$1y$2y$3y...$n", where y is the value of$IFS (Input Field Seperator) variable i.e. "$*" is one long string and$IFS act as a separator or token delimiters.

An example showing difference between these two variables:

#!/bin/bash

# Set Input FIeld Separator
IFS=", "
echo "* Displaying all pizza names using \$@" echo "$@"
echo

# Output includes the field separator - the comma
echo "* Displaying all pizza names using \$*" echo "$*"


When the script runs the output is:

pbmac@pbmac-server $./pizza.sh * Displaying all pizza names using$@
Margherita Tomato Panner Gourmet

*Displaying all pizza names using $* Margherita,Tomato,Panner,Gourmet ## The exec Command The exec command in Linux is used to execute a command from the bash itself. This command does not create a new process, it just replaces the bash with the command to be executed. If the exec command is successful, it does not return to the calling process. Syntax: exec [-cl] [-a name] [command [arguments]] [redirection ...]  Options: • -c: Used to execute the command with empty environment. • -a name: Used to pass a name as the zeroth argument of the command. • - l: Used to pass dash as the zeroth argument of the command. Note: exec command does not create a new process. When we run the exec command from the terminal, the ongoing terminal process is replaced by the command that is provided as the argument for the exec command. The exec command can be used in two modes: 1. Exec with a command as an argument: In the first mode, the exec tries to execute it as a command passing the remaining arguments, if any, to that command and managing the redirections, if any. # The given command is executed in a different process...and returns the results, then the original process is back in control pbmac@pbmac-server$ exec wc -w < tmp
9
pbmac@pbmac-server $ 1. Exec without a command: If no command is supplied, the redirections can be used to modify the current shell environment. This is useful as it allows us to change the file descriptors of the shell as per our desire. The process continues even after the exec command unlike the previous case but now the standard input, output, and error are modified according to the redirections. pbmac@pbmac-server$ bash

This exec creates a new process that overlays the bash shell started by the previous command
pbmac@pbmac-server $exec > tmp We are now in a different process....and all of this is being directed to the file names tmp by the previous command pbmac@pbmac-server$ ls
pbmac@pbmac-server $echo "This message will not be displayed" pbmac@pbmac-server$ exit
exit

The exit command exits the other process...and returns to the original context
We see all of the output that had been sent to the file named tmp
pbmac@pbmac-server $cat tmp test2.sh test.sh tmp This message will not be displayed ## The source Command The source command is a shell built-in command which is used to read and execute the content of a file (generally a set of commands), passed as an argument in the current shell script. After taking the content of the specified files, the command passes it to the TCL interpreter as a text script which then gets executed. If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise, the positional parameters remain unchanged. The entries in$PATH are used to find the directory containing FILENAME, however if the file is not present in $PATH it will search the file in the current directory. The source command has no option and the argument is the file only. Syntax: source FILENAME [arguments] Using the source command is very similar to writing a shell script, but there is no requirement for a shebang line, and the file does NOT have to be set to executable. If there is a file that has the following two lines: pbmac@pbmac-server$ cat test-3.txt
ls
cal

Then to source the file:

pbmac@pbmac-server \$ source test-3.txt
CPP      get-pip.py   Linux           t1       test2
'CS 11'   Java         myFile.txt.gz   t1.cpp   test2.cpp
CSP31B   JavaScript   Python          test1    WebStuff
November 2020
Su Mo Tu We Th Fr Sa
1  2  3  4  5  6  7
8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30