Shell Programming: The Ultimate Beginner's Guide to Learn Shell Step by Step by Kathleen Peterson
Author:Kathleen Peterson [Peterson, Kathleen]
Language: eng
Format: azw3, epub, pdf
Published: 2020-10-07T00:00:00+00:00
Page 165
3 parameters and arguments
164
To take care of such options, you can use the arguments of the command
dozeile either check and evaluate it yourself or use the command
topts. Here is the syntax to getopts:
getopts options variable [arguments]
To use the getopts function correctly, you must observe the following rules:
Even if topts is used, this does not require that optimal
must be specified.
Options must precede the file name or parameter:
command -o file.txt
# Right
command file.txt -o
# Not correct
The order in which the options are specified is not important:
command -o -x file.txt # Correct
command -x -o file.txt # Also correct
Options can also be summarized:
command -ox file.txt # Correct
command -xo file.txt # Correct
If getopts is called, this command checks whether an option (initiated
with a »-«) in the position parameters (from left to right). Of the
The parameter to be edited next is stored in the auto-
matic variable OPTIND managed. The value of this variable is when called
initially 1, but is increased by 1 with every further getopts call. When a
The command line is to be read in several times, the index must be returned manually.
be set.
If an option is found, this character is assigned to the variable »Variable«
grasslands. In the string »Options« all switches are marked with an identification book
letters indicated. At switches that receive additional arguments, a
Colon appended. The options that expect another argument (i.e.
those marked with a colon) are stored in the shell variable OPTARG
saved.
Unless an "argument" is explicitly passed when calling getopts,
the shell command uses the positional parameters, i.e. the arguments of
the command line of the script.
If getopts returns 0, this indicates that an option has been found. At a
Value not equal to 0, the end of the arguments has been reached or an error has occurred.
to step. If you want to avoid outputting an error message, you can use a double
Use pelpunkt as the first character or set the shell variable OPTERR to 0.
3
Usually getopts is evaluated in a loop with a case construction.
Both have not yet been dealt with (we will only come to the next chapter
tel on this topic), but we don't want to talk about the
leave dry theory. Here is a possible example:
# Demonstrated topped
# Name: agetopter
while getopts abc: D: opt
do
case $ opt in
a) echo "Option a" ;;
b) echo "Option b" ;;
c) echo "Option c: ($ OPTARG)" ;;
D) echo "Option D: ($ OPTARG)" ;;
esac
done
The script when running:
you @ host> ./agetopter -a
Option a
you @ host> ./agetopter -b
Option b
you @ host> ./agetopter -c
./agetopter: option requires an argument - c
you @ host> ./agetopter -c Hello
Option c: (hello)
you @ host> ./agetopter -D
./agetopter: option requires an argument - D
you @ host> ./agetopter -D Again
Option D: (Again)
you @ host> ./agetopter -ab
Option a
Option b
you @ host> ./agetopter -abD
Option a
Option b
./agetopter: option requires an argument - D
you @ host> ./agetopter -abD Hello
Option a
Option b
Option D: (Hello)
you @ host> ./agetopter -x
./agetopter: illegal option - x
In the example you could also see the effects of the colon
recognize behind an option symbol. Give when using such
If the option does not take any further arguments, an error message is returned. Self
a wrong argument evaluates getopts here and reports the error.
Download
Shell Programming: The Ultimate Beginner's Guide to Learn Shell Step by Step by Kathleen Peterson.epub
Shell Programming: The Ultimate Beginner's Guide to Learn Shell Step by Step by Kathleen Peterson.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8304)
Test-Driven Development with Java by Alan Mellor(6752)
Data Augmentation with Python by Duc Haba(6666)
Principles of Data Fabric by Sonia Mezzetta(6419)
Learn Blender Simulations the Right Way by Stephen Pearson(6313)
Microservices with Spring Boot 3 and Spring Cloud by Magnus Larsson(6188)
Hadoop in Practice by Alex Holmes(5962)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5809)
RPA Solution Architect's Handbook by Sachin Sahgal(5588)
Big Data Analysis with Python by Ivan Marin(5375)
The Infinite Retina by Robert Scoble Irena Cronin(5276)
Life 3.0: Being Human in the Age of Artificial Intelligence by Tegmark Max(5152)
Pretrain Vision and Large Language Models in Python by Emily Webber(4345)
Infrastructure as Code for Beginners by Russ McKendrick(4104)
Functional Programming in JavaScript by Mantyla Dan(4040)
The Age of Surveillance Capitalism by Shoshana Zuboff(3959)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3819)
Embracing Microservices Design by Ovais Mehboob Ahmed Khan Nabil Siddiqui and Timothy Oleson(3621)
Applied Machine Learning for Healthcare and Life Sciences Using AWS by Ujjwal Ratan(3597)
