Book Linux
Book Linux
Bash
Release a0866cc, 2019-08-29
Matt Harasymczuk
2019-08-29
INTRODUCTION
1 Introduction 3
1.1 About this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Agenda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 VIM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2 Linux 7
2.1 Directory Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 Basic Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 Environmental Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.4 Users and groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.5 Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.6 SSH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.7 Crontab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.8 Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.9 Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.10 Booting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.11 Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.12 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.13 Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.14 X Window System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3 Bash 19
3.1 Interpreter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.4 Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.5 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.6 Pipe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.7 Stdout and Stderr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.8 Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.9 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.10 Parameter expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.11 Multiprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4 Appendix 27
4.1 License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.2 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.3 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Bibliography 29
i
ii
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
Tip: If you’re interested in training course on topics from this book, please email me at [email protected]
INTRODUCTION 1
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
2 INTRODUCTION
CHAPTER
ONE
INTRODUCTION
Obszar procesowy
3
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
1.2 Agenda
12:00-13:00 Lunch
13:00-17:00 Bash programming workshop
1. Variables
2. Scopes
3. Files
4 Chapter 1. Introduction
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
12:00-13:00 Lunch
13:00-17:00 Bash programming workshop
1. Variables
2. Scopes
3. Files
1.3 VIM
1.3. VIM 5
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
6 Chapter 1. Introduction
CHAPTER
TWO
LINUX
7
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
2.2.1 cd
• cd ~
• cd -
• cd
• cd ..
8 Chapter 2. Linux
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
2.2.2 ls
• ls -lh
• alias l='ls -lAh --color=auto'
PS1
## Prompt
red='\[\033[00;31m\]'
green='\[\033[00;32m\]'
blue='\[\033[00;36m\]'
white='\[\033[00;39m\]'
2.4.1 Files
• /etc/passwd
• /etc/shadow
• /etc/group
2.4.2 whoami
2.4.3 UID
2.4.4 HOME
2.5 Permissions
chmod
chown
10 Chapter 2. Linux
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
chgrp
2.5.3 UMASK
2.5.5 ACL
2.6 SSH
2.6.1 Connecting
• ~/.id_rsa
• ~/.id_rsa.pub
-L
-R
2.6.8 SSHd
2.7 Crontab
$ crontab -e
$ crontab -l
$ sudo crontab -e
2.6. SSH 11
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
45 04 * * * /usr/bin/updatedb
45 04 * * * /usr/sbin/chkrootkit && /usr/bin/updatedb
00 06 * * * env DISPLAY=:0.0 gui_appname
00 01 * * * ubuntu /home/ubuntu/script.sh
export EDITOR=/usr/bin/vim
Variables
PATH=/usr/sbin:/usr/bin:/sbin:/bin
Special characters
• * any value
• , value list separator
• - range of values
• / step values
Crontab formatting
• minute: 0-60
• hour: 0-23
• day of month: 0-31
• month: JAN-DEC / 0-12
• day of week: SUN-SAT / 0-7 (Sunday = 0 or 7)
Short notation
12 Chapter 2. Linux
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
• /etc/cron.allow
• /etc/cron.deny
• /etc/crontab
• /var/spool/crontab/
• /etc/cron.d/
• /etc/cron.daily/
• /etc/cron.hourly/
• /etc/cron.weekly/
• /etc/cron.monthly/
2.7.5 Other
2.8 Logs
2.8.1 dmesg
2.8.2 /var/log
2.8.3 /var/log/syslog
2.8.4 /var/log/messages
2.9 Filesystem
2.9.1 Symlinks
• no extension
• .filenames (starting with dot)
• file
2.9.3 Size
du -h df -h
2.8. Logs 13
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
parted
gparted
druid
fdisk
mount
Devices
• /dev/
Mount points
• /etc/fstab
• /etc/mtab
Filesystems
2.10 Booting
2.10.1 LiveCD
RamFS
2.10.2 GRUB
Kernel
Initramfs
Splashscreen
Multiple OSes
/etc/rc.d
/etc/init.d/
Systemd
14 Chapter 2. Linux
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
System-V
Init-d
2.11 Devices
2.12 Networking
• /etc/hosts localhost
• 127.0.0.1
• ::1
• /etc/hosts
• /etc/resolv.conf
• /etc/network/interfaces
• /etc/if-up-down/
2.12.1 Built-in
ifconfig
ip
route
netstat
iptables
2.12.2 Additional
nc
wireshark
nmap
2.11. Devices 15
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
tcpdump
2.13 Processes
2.13.2 Listing
ps aux
lsof
top
htop
2.13.3 PID
PID files
pidof
/var/spool/pid
2.13.4 Locks
2.13.5 Killing
kill
kill -9
killall
Ctrl-c
Ctrl-d
2.13.6 Priorities
nice
2.14.2 startx
• wdm
• xdm
16 Chapter 2. Linux
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
• kdm
• gdm
• gnome
• kde
• fluxbox
• fvwm
• xfce
18 Chapter 2. Linux
CHAPTER
THREE
BASH
3.1 Interpreter
• ~/.profile
• ~/.bashrc
• ~/.bash_logout
• /etc/bashrc
3.1.2 Locale
• $LANG
• /etc/locale
3.1.3 Autocompletion
• “\n”
3.1.5 #!/bin/bash
3.1.6 bash -x
3.1.7 Comments
19
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
3.2 Variables
$ name='José Jiménez'
$ echo 'My name is $name'
My name is $name
$ name="José Jiménez"
$ echo "$name"
My name is José Jiménez
3.3 Arrays
3.3.1 Declaration
ARRAY=() Declares an indexed array ARRAY and initializes it to be empty. This can also be used to empty an
existing array.
ARRAY[0]= Generally sets the first element of an indexed array. If no array ARRAY existed before, it is created.
declare -a ARRAY Declares an indexed array ARRAY. An existing array is not initialized. declare -A
ARRAY Declares an associative array ARRAY. This is the one and only way to create associative arrays.
ARRAY[N]=VALUE Sets the element N of the indexed array ARRAY to VALUE. N can be any valid arithmetic
expression.
ARRAY[STRING]=VALUE Sets the element indexed by STRING of the associative array ARRAY.
ARRAY=VALUE As above. If no index is given, as a default the zeroth element is set to VALUE. Careful, this is
even true of associative arrays - there is no error if no key is specified, and the value is assigned to string index
“0”.
ARRAY=(E1 E2 ...) Compound array assignment - sets the whole array ARRAY to the given list of elements
indexed sequentially starting at zero. The array is unset before assignment unless the += operator is used. When
the list is empty (ARRAY=()), the array will be set to an empty array. This method obviously does not use explicit
indexes. An associative array can not be set like that! Clearing an associative array using ARRAY=() works.
ARRAY=([X]=E1 [Y]=E2 ...) Compound assignment for indexed arrays with index-value pairs declared
individually (here for example X and Y). X and Y are arithmetic expressions. This syntax can be combined with
the above - elements declared without an explicitly specified index are assigned sequetially starting at either the
last element with an explicit index, or zero.
ARRAY=([S1]=E1 [S2]=E2 ...) Individual mass-setting for associative arrays. The named indexes (here:
S1 and S2) are strings.
ARRAY+=(E1 E2 ...) Append to ARRAY. ARRAY=("${ANOTHER_ARRAY[@]}") Copy AN-
OTHER_ARRAY to ARRAY, copying each element.
20 Chapter 3. Bash
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
${ARRAY[N]} Expands to the value of the index N in the indexed array ARRAY. If N is a negative number, it’s
treated as the offset from the maximum assigned index (can’t be used for assignment) - 1
${ARRAY[S]} Expands to the value of the index S in the associative array ARRAY.
"${ARRAY[@]}" ${ARRAY[@]}` "${ARRAY[*]}" ${ARRAY[*]} Similar to mass-expanding positional
parameters, this expands to all elements. If unquoted, both subscripts * and @ expand to the same result, if quoted,
@ expands to all elements individually quoted, * expands to all elements quoted as a whole.
"${ARRAY[@]:N:M}" ${ARRAY[@]:N:M} "${ARRAY[*]:N:M}" ${ARRAY[*]:N:M} Similar to what
this syntax does for the characters of a single string when doing substring expansion, this expands to M elements
starting with element N. This way you can mass-expand individual indexes. The rules for quoting and the sub-
scripts * and @ are the same as above for the other mass-expansions.
3.3.4 Metadata
3.3.5 Destruction
unset -v ARRAY unset -v ARRAY[@] unset -v ARRAY[*] Destroys a complete array unset -v
ARRAY[N] Destroys the array element at index N unset -v ARRAY[STRING] Destroys the array element
of the associative array at index STRING
3.4 Conditionals
3.4.1 if
name="José Jiménez"
name="José Jiménez"
3.4. Conditionals 21
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
$ name="José Jiménez"
$ [ $imie == "José Jiménez" ] && echo "My name José Jiménez" || echo "I am someone
˓→else"
exit 0
case "$Keypress" in
[[:lower:]] ) echo "Lowercase letter";;
[[:upper:]] ) echo "Uppercase letter";;
[0-9] ) echo "Digit";;
* ) echo "Punctuation, whitespace, or other";;
esac # Allows ranges of characters in [square brackets],
#+ or POSIX ranges in [[double square brackets.
3.5 Loops
3.5.1 For
for i in $( ls ); do
echo item: $i
done
Warning: IFS=’\n’
Inline for
22 Chapter 3. Bash
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
3.5.2 While
COUNTER=0
3.5.3 Until
COUNTER=20
3.5. Loops 23
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
3.6 Pipe
3.6.1 Pipe - |
3.6.2 awk
3.6.3 sed
3.6.4 sort
3.6.5 uniq
3.8 Network
3.8.1 wget
3.8.2 curl
3.9.1 Grep
3.9.2 Egrep
• $PARAMETER
• ${PARAMETER}
3.10.2 Indirection
• ${!PARAMETER}
24 Chapter 3. Bash
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
• ${PARAMETER^}
• ${PARAMETER^^}
• ${PARAMETER,}
• ${PARAMETER,,}
• ${PARAMETER~}
• ${PARAMETER~~}
• ${!PREFIX*}
• ${!PREFIX@}
• ${PARAMETER#PATTERN}
• ${PARAMETER##PATTERN}
• ${PARAMETER%PATTERN}
• ${PARAMETER%%PATTERN}
• ${PARAMETER/PATTERN/STRING}
• ${PARAMETER//PATTERN/STRING}
• ${PARAMETER/PATTERN}
• ${PARAMETER//PATTERN}
• ${#PARAMETER}
• ${PARAMETER:OFFSET}
• ${PARAMETER:OFFSET:LENGTH}
• ${PARAMETER:-WORD}
• ${PARAMETER-WORD}
• ${PARAMETER:=WORD}
• ${PARAMETER=WORD}
• ${PARAMETER:+WORD}
• ${PARAMETER+WORD}
• ${PARAMETER:?WORD}
• ${PARAMETER?WORD}
3.11 Multiprocessing
26 Chapter 3. Bash
CHAPTER
FOUR
APPENDIX
4.1 License
MIT License
Copyright (c) 2019 Matt Harasymczuk
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM-
PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4.2 Glossary
4.3 Bibliography
27
Linux and Shell Programming with Bash, Release a0866cc, 2019-08-29
28 Chapter 4. Appendix
BIBLIOGRAPHY
[1] Inc. Linux Kernel Organization. The linux kernel documentation. 2019. URL: https://www.kernel.org/doc/
html/latest/.
29