Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
elearning:workbooks:redhat:rh124en:l104 [2024/11/11 12:40] – created admin | elearning:workbooks:redhat:rh124en:l104 [2024/11/26 10:56] (Version actuelle) – admin | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | ~~PDF: | ||
+ | |||
Version : **2024.01** | Version : **2024.01** | ||
- | Last updated | + | Last update |
- | ======RH12404 | + | ====== |
=====Contents===== | =====Contents===== | ||
- | * **RH12404 | + | * **RH12405 |
* Contents | * Contents | ||
- | * LAB #1 - Use of Basic Shell Commands | + | |
- | * 1.1 - The stty Command | + | |
- | * 1.2 - The date command | + | * 1.1 - Shell Internal and External Commands |
- | * 1.3 - The who Command | + | * 1.2 - Aliases |
- | * 1.4 - The df Command | + | * 1.3 - Defining a user's shell |
- | * 1.5 - The free Command | + | * 1.4 - The Prompt |
- | * 1.6 - The whoami Command | + | * 1.5 - Recalling commands |
- | * 1.7 - The pwd Command | + | * 1.6 - Generating file name endings |
- | * 1.8 - The cd Command | + | * 1.7 - The interactive shell |
- | | + | * Character |
- | * 1.10 - The lsof Command | + | * Character ? |
- | * 1.11 - The touch Command | + | * Character [ ] |
- | * 1.12 - The echo Command | + | * 1.8 - The extglob option |
- | * 1.13 - The cp Command | + | * ? |
- | * 1.14 - The file Command | + | * *(expression) |
- | | + | * +(expression) |
- | * 1.16 - The mv Command | + | * @(expression) |
- | * 1.17 - The mkdir Command | + | * !(expression) |
- | * 1.18 - The rmdir Command | + | * Protecting Metacharacters |
- | * 1.19 - The rm Command | + | * 1.9 - Exit Codes |
- | * 1.20 - The sort Command | + | * 1.10 - Redirections |
- | * 1.21 - The more Command | + | * 1.11 - Pipes |
- | * 1.22 - The less Command | + | * 1.12 - Command |
- | * 1.23 - The find Command | + | * 1.13 - Command |
- | * 1.24 - The su Command | + | * 1.14 - Displaying shell variables |
- | * 1.25 - The updatedb and locate Commands | + | * Main variables |
- | * 1.26 - The whereis Command | + | * Internationalization and Localization Variables |
- | * 1.27 - The which Command | + | * Special variables |
- | * 1.28 - The uptime Command | + | * 1.15 - The env command |
- | * 1.29 - The w Command | + | * 1.16 - Bash Shell options |
- | * 1.30 - The uname Command | + | * Examples |
- | * 1.31 - The du Command | + | * noclobber |
- | * 1.32 - The clear Command | + | * noglob |
- | * 1.33 - The exit Command | + | * nounset |
- | * 1.34 - The logout Command | + | |
- | * 1.35 - The sleep Command | + | |
- | * 1.36 - The wall Command | + | |
- | * 1.37 - The seq Command | + | |
- | * 1.38 - The screen Command | + | |
- | * LAB #2 - Switches and Arguments | + | |
- | * LAB #3 - Regular Expressions | + | |
- | * BREs | + | |
- | * EREs | + | |
- | * Manipulating Text Files | + | |
- | * Text-search Utilities | + | |
- | * The grep Command | + | |
- | * The egrep Command | + | |
- | * The fgrep Command | + | |
- | * LAB #4 - Using grep, egrep and fgrep | + | |
- | * The Stream EDitor SED | + | |
- | * LAB #5 - Using sed | + | |
- | * The Text Processor AWK | + | |
- | * Presentation | + | |
- | * Field Separation | + | |
- | * Conditions | + | |
- | * A regular expression applied to a record | + | |
- | * A regular expression applied to a field | + | |
- | * Comparisons | + | |
- | * Logical Operators | + | |
- | * Built-in Variables | + | |
- | * Awk Scripts | + | |
- | * The printf function | + | |
- | * Control Statements | + | |
- | * if | + | |
- | * for | + | |
- | * while | + | |
- | * do-while | + | |
- | * LAB #3 - Using awk | + | |
- | * LAB 7 - Other Useful Commands | + | |
- | * 7.1 - The expand Command | + | |
- | * 7.2 - La Commande unexpand | + | |
- | * 7.3 - The cut command | + | |
- | * 7.4 - The uniq Command | + | |
- | * 7.5 - The tr Command | + | |
- | * 7.6 - The paste Command | + | |
- | * 7.7 - The split Command | + | |
- | * 7.8 - The diff Command | + | |
- | * 7.9 - The cmp Command | + | |
- | * 7.10 - The patch Command | + | |
- | * 7.11 - The strings Command | + | |
- | * 7.12 - The comm Command | + | |
- | * 7.13 - The head Command | + | |
- | * 7.14 - The tail Command | + | |
- | * LAB #8 - Use the grep, tr and cut commands to extract your IPv4 address from the output of ifconfig | + | |
- | * LAB #9 - Use the grep, awk and sed commands to extract your IPv4 address from the output of ip | + | |
- | =====LAB #1 - Use of Basic Shell Commands===== | + | =====The Shell===== |
- | <WRAP center round todo 60%> | + | A shell is a **command line interpreter** (C.L.I). It is used as an interface to give instructions or **commands** to the operating system. |
- | **To do** - You are currently the root user in your terminal. Before proceeding further, type **exit** and hit the < | + | |
- | </ | + | |
- | ====1.1 - The stty Command==== | + | The word shell is generic. There are many shells in the Unix world, for example |
- | + | ||
- | Using this command with the **-a** switch allows you to identify which combination of keys should be used to control a foreground process: | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# exit | + | |
- | logout | + | |
- | [trainee@redhat9 ~]$ stty -a | + | |
- | speed 38400 baud; rows 27; columns 105; line = 0; | + | |
- | intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = < | + | |
- | start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0; | + | |
- | -parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts | + | |
- | -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel | + | |
- | -iutf8 | + | |
- | opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 | + | |
- | isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho | + | |
- | -extproc | + | |
- | </ | + | |
- | + | ||
- | <WRAP center round important 60%> | + | |
- | **Important** - The two most important combinations | + | |
- | </ | + | |
- | + | ||
- | ===Command Line Switches=== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **stty** command to view the command line switches. | + | |
- | </ | + | |
- | ====1.2 | + | ^ Shell ^ Name ^ Release Date ^ Inventer ^ Command ^ Comments ^ |
+ | | tsh | Thompson Shell | 1971 | Ken Thompson | sh | The first shell | | ||
+ | | sh | Bourne Shell | 1977 | Stephen Bourne | sh | The shell common to all Unix and Linux OSs: /bin/sh | | ||
+ | | csh | C-Shell | 1978 | Bill Joy | csh | The BSD shell: /bin/csh | | ||
+ | | tcsh | Tenex C-Shell | 1979 | Ken Greer | tcsh | A fork of the csh shell: /bin/tcsh | | ||
+ | | ksh | Korn Shell | 1980 | David Korn | ksh | Open Source since 2005: /bin/ksh | | ||
+ | | bash| Bourne Again Shell | 1987 | Brian Fox | bash | The default shell for Linux, MacOS X, Solaris 11: /bin/bash | | ||
+ | | zsh | Z Shell | 1990 | Paul Falstad | zsh | Zsh is an extended Bourne shell with a large number of improvements, | ||
- | This command' | + | Under RHEL 9 the **/bin/sh** shell is a symbolic link to **/ |
< | < | ||
- | [trainee@redhat9 ~]$ date | + | [trainee@redhat9 ~]$ ls -l /bin/sh |
- | Wed Sep 25 02:45:00 PM CEST 2024 | + | lrwxrwxrwx. 1 root root 4 Feb 15 2024 /bin/sh -> bash |
</ | </ | ||
- | ===Command Line Switches=== | + | =====LAB #1 - The /bin/bash Shell===== |
- | <WRAP center round todo 60%> | + | This module is about using the **bash** shell under Linux. The **bash** shell allows you to: |
- | **To do** : Use the **--help** option of the **date** command | + | |
- | </ | + | |
- | ====1.3 | + | * Recall previously typed commands |
+ | * Auto-generate the end of a file name | ||
+ | * Use Aliases | ||
+ | * Use tables | ||
+ | * Use C language numerical and math variables | ||
+ | * Manage strings | ||
+ | * Use Functions | ||
- | This command's output shows who is currently connected | + | A command |
- | < | + | * An Alias, |
- | [trainee@redhat9 ~]$ who | + | * A Function, |
- | trainee | + | |
- | </ | + | * An External Command. |
- | ===Command Line Switches=== | + | ====1.1 - Built-in and External Shell Commands==== |
- | <WRAP center round todo 60%> | + | Built-in shell commands are commands such as **cd**. To check the type of command, use the **type** command: |
- | **To do** : Use the **--help** option | + | |
- | </ | + | |
- | + | ||
- | ====1.4 - The df Command==== | + | |
- | + | ||
- | This command' | + | |
< | < | ||
- | [trainee@redhat9 ~]$ df | + | [trainee@redhat9 ~]$ type cd |
- | Filesystem | + | cd is a shell builtin |
- | devtmpfs | + | |
- | tmpfs | + | |
- | tmpfs | + | |
- | / | + | |
- | / | + | |
- | tmpfs 786956 | + | |
- | tmpfs 786956 | + | |
</ | </ | ||
- | The units are shown as **blocks**. In order to **humanize** | + | Commands external |
< | < | ||
- | [trainee@redhat9 ~]$ df -h | + | [trainee@redhat9 ~]$ type ifconfig |
- | Filesystem | + | ifconfig is /usr/sbin/ifconfig |
- | devtmpfs | + | |
- | tmpfs 3.8G | + | |
- | tmpfs 1.6G 9.1M 1.5G 1% /run | + | |
- | / | + | |
- | / | + | |
- | tmpfs 769M | + | |
- | tmpfs 769M | + | |
</ | </ | ||
- | ===Command Line Switches=== | ||
- | <WRAP center round todo 60%> | + | ====1.2 - Aliases==== |
- | **To do** : Use the **--help** option of the **df** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ====1.5 - The free Command==== | + | |
- | + | ||
- | This command' | + | |
- | + | ||
- | < | + | |
- | [trainee@redhat9 ~]$ free | + | |
- | | + | |
- | Mem: | + | |
- | Swap: 5242876 | + | |
- | </ | + | |
- | + | ||
- | The units are shown as **blocks**. In order to **humanize** the output, it is possible to use the **-h** switch: | + | |
- | + | ||
- | < | + | |
- | [trainee@redhat9 ~]$ free -h | + | |
- | | + | |
- | Mem: | + | |
- | Swap: 5.0Gi 0B | + | |
- | </ | + | |
- | + | ||
- | ===Command Line Switches=== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **free** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ====1.6 - The whoami Command==== | + | |
- | + | ||
- | This command' | + | |
- | + | ||
- | < | + | |
- | [trainee@redhat9 ~]$ whoami | + | |
- | trainee | + | |
- | </ | + | |
- | Now become | + | Aliases are names used to designate a command or a sequence of commands and are specific only to the shell that created them and to the user's environment |
< | < | ||
- | [trainee@redhat9 ~]$ su - | + | [trainee@redhat9 ~]$ type ls |
- | Password: fenestros | + | ls is aliased to `ls --color=auto' |
</ | </ | ||
<WRAP center round important 60%> | <WRAP center round important 60%> | ||
- | **Important** : Note that the password will not be visible. | + | **Important**: |
</ | </ | ||
- | Now use the **whoami** command | + | An alias is defined using the **alias** command: |
< | < | ||
- | [root@redhat9 ~]# whoami | + | [trainee@redhat9 ~]$ alias dir=‘ls -l’ |
- | root | + | [trainee@redhat9 ~]$ dir |
+ | total 4 | ||
+ | -rw-r--r--. 1 trainee trainee 0 Sep 25 15:11 aac | ||
+ | -rw-r--r--. 1 trainee trainee 0 Sep 25 15:11 abc | ||
+ | -rw-r--r--. 1 trainee trainee 0 Sep 25 15:11 bca | ||
+ | drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Desktop | ||
+ | drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Documents | ||
+ | drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Downloads | ||
+ | drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Music | ||
+ | drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Pictures | ||
+ | drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Public | ||
+ | drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Templates | ||
+ | drwxr-xr-x. 2 trainee trainee 6 Oct 19 2023 Videos | ||
+ | -rw-r--r--. 1 trainee trainee 442 Sep 25 14:24 vitext | ||
+ | -rw-r--r--. 1 trainee trainee 0 Sep 25 15:11 xyz | ||
</ | </ | ||
- | |||
<WRAP center round important 60%> | <WRAP center round important 60%> | ||
- | **Important** : Note the current effective user ID is root. | + | **Important**: |
</ | </ | ||
- | Finally execute | + | The list of defined aliases can be viewed using the **alias** command: |
< | < | ||
- | [root@redhat9 ~]# exit | + | [trainee@redhat9 ~]$ alias |
- | logout | + | alias dir=‘ls -l’ |
- | [trainee@redhat9 ~]$ | + | alias egrep=‘egrep --color=auto’ |
+ | alias fgrep=‘fgrep --color=auto’ | ||
+ | alias grep=‘grep --color=auto’ | ||
+ | alias l.=‘ls -d .* --color=auto’ | ||
+ | alias ll=‘ls -l --color=auto’ | ||
+ | alias ls=‘ls --color=auto’ | ||
+ | alias xzegrep=‘xzegrep --color=auto’ | ||
+ | alias xzfgrep=‘xzfgrep --color=auto’ | ||
+ | alias xzgrep=‘xzgrep --color=auto’ | ||
+ | alias zegrep=‘zegrep --color=auto’ | ||
+ | alias zfgrep=‘zfgrep --color=auto’ | ||
+ | alias zgrep=‘zgrep --color=auto’ | ||
</ | </ | ||
- | ===Command Line Switches=== | + | < |
- | + | **Important**: Note that this list contains, indiscriminately, | |
- | < | + | |
- | **To do** : Use the **--help** option of the **whoami** command | + | |
</ | </ | ||
- | ====1.7 - The pwd Command==== | + | To force execution of a command |
- | + | ||
- | This command's output shows the current working directory: | + | |
< | < | ||
- | [trainee@redhat9 ~]$ pwd | + | [trainee@redhat9 ~]$ \dir |
- | / | + | aac abc bca Desktop Documents Downloads Music Pictures Public Templates Videos vitext xyz |
</ | </ | ||
- | ===Command Line Switches=== | + | To delete an alias, use the **unalias** command: |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **help** command | + | |
- | </ | + | |
- | + | ||
- | ====1.8 - The cd Command=== | + | |
- | + | ||
- | This command' | + | |
< | < | ||
- | [trainee@redhat9 ~]$ cd /tmp | + | [trainee@redhat9 ~]$ unalias dir |
- | + | [trainee@redhat9 | |
- | [trainee@redhat9 | + | aac abc bca Desktop Documents Downloads Music Pictures Public Templates Videos vitext xyz |
- | /tmp | + | |
- | [trainee@redhat9 tmp]$ | + | |
</ | </ | ||
- | ===Command Line Switches=== | + | ====1.3 - Defining a user's shell==== |
- | <WRAP center round todo 60%> | + | The user shell is defined by **root** in the last field of the **/etc/passwd** file: |
- | **To do** : Use the **help** command with **cd** option to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ====1.9 - The ls Command=== | + | |
- | + | ||
- | This commands output lists information about the files in directory specified as an argument. If no argument is specified, the output lists the files in the current working directory: | + | |
< | < | ||
- | [trainee@redhat9 | + | [trainee@redhat9 |
- | dbus-G7skg3Wlpv | + | root: |
- | dbus-s2vBGtxTHi | + | bin: |
- | inode | + | daemon: |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-colord.service-FfNuds | + | adm: |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-dbus-broker.service-VIpKgR | + | lp: |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-kdump.service-0SbYbm | + | sync: |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-ModemManager.service-k4DpLF | + | shutdown: |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-power-profiles-daemon.service-mIx9S5 | + | halt: |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-rtkit-daemon.service-2gD28Z | + | mail: |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-switcheroo-control.service-rLbOK4 | + | operator: |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-systemd-logind.service-uLNyfd | + | games: |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-upower.service-bIpAUN | + | ftp: |
+ | nobody: | ||
+ | systemd-coredump: | ||
+ | dbus: | ||
+ | polkitd: | ||
+ | avahi: | ||
+ | tss: | ||
+ | colord: | ||
+ | clevis: | ||
+ | rtkit: | ||
+ | sssd: | ||
+ | geoclue: | ||
+ | libstoragemgmt: | ||
+ | systemd-oom: | ||
+ | setroubleshoot: | ||
+ | pipewire: | ||
+ | flatpak: | ||
+ | gdm: | ||
+ | cockpit-ws: | ||
+ | cockpit-wsinstance: | ||
+ | gnome-initial-setup: | ||
+ | sshd: | ||
+ | chrony: | ||
+ | dnsmasq: | ||
+ | tcpdump: | ||
+ | trainee: | ||
</ | </ | ||
- | ===Command Line Switches=== | + | However the user can change their shell using the **chsh** command. The shells available to system users are listed in the **/etc/shells** file. Enter the **cat /etc/shells** command: |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **ls** command | + | |
- | </ | + | |
- | + | ||
- | ====1.10 - The lsof Command==== | + | |
- | + | ||
- | This command' | + | |
< | < | ||
- | [trainee@redhat9 | + | [trainee@redhat9 |
- | Password: fenestros | + | /bin/sh |
- | + | /bin/bash | |
- | [root@redhat9 ~]# lsof | more | + | /usr/bin/sh |
- | COMMAND | + | /usr/bin/bash |
- | systemd | + | |
- | systemd | + | |
- | systemd | + | |
- | lib/ | + | |
- | systemd | + | |
- | selinux/targeted/ | + | |
- | systemd | + | |
- | lib64/libm.so.6 | + | |
- | systemd | + | |
- | lib64/libzstd.so.1.5.1 | + | |
- | systemd | + | |
- | lib64/libcrypto.so.3.0.7 | + | |
- | systemd | + | |
- | lib64/libp11-kit.so.0.3.1 | + | |
- | systemd | + | |
- | lib64/ | + | |
- | systemd | + | |
- | lib64/ | + | |
- | systemd | + | |
- | lib64/ | + | |
- | systemd | + | |
- | lib64/ | + | |
- | systemd | + | |
- | lib64/ | + | |
- | systemd | + | |
- | --More-- | + | |
- | [q] | + | |
</ | </ | ||
- | ===Command Line Switches=== | + | Then use the **echo** command to display |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **lsof** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ====1.11 - The touch Command==== | + | |
- | + | ||
- | This command updates the access and modification times of one or several file(s) to the current time. If the file does not exist, the system creates an empty file: | + | |
< | < | ||
- | [root@redhat9 ~]# exit | + | [trainee@redhat9 ~]$ echo $SHELL |
- | logout | + | /bin/bash |
- | + | ||
- | [trainee@redhat9 tmp]$ touch test | + | |
- | + | ||
- | [trainee@redhat9 tmp]$ ls | + | |
- | dbus-G7skg3Wlpv | + | |
- | dbus-s2vBGtxTHi | + | |
- | inode | + | |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-colord.service-FfNuds | + | |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-dbus-broker.service-VIpKgR | + | |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-kdump.service-0SbYbm | + | |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-ModemManager.service-k4DpLF | + | |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-power-profiles-daemon.service-mIx9S5 | + | |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-rtkit-daemon.service-2gD28Z | + | |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-switcheroo-control.service-rLbOK4 | + | |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-systemd-logind.service-uLNyfd | + | |
- | systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-upower.service-bIpAUN | + | |
- | test | + | |
- | </ | + | |
- | + | ||
- | ===Command Line Switches=== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **touch** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ====1.12 - The echo Command ==== | + | |
- | + | ||
- | This command writes the arguments to the standard output (i.e. the screen): | + | |
- | + | ||
- | < | + | |
- | [trainee@redhat9 tmp]$ echo fenestros | + | |
- | fenestros | + | |
- | </code> | + | |
- | + | ||
- | ===Command Line Switches=== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **help** command with **echo** option to view the command line switches. | + | |
- | </WRAP> | + | |
- | + | ||
- | ====1.13 - The cp Command=== | + | |
- | + | ||
- | This command is used to copy a source to a destination or multiple sources to a directory: | + | |
- | + | ||
- | < | + | |
- | [trainee@redhat9 tmp]$ cp test ~ | + | |
- | + | ||
- | [trainee@redhat9 tmp]$ ls -l ~ | + | |
- | total 4 | + | |
- | drwxr-xr-x. 2 trainee trainee | + | |
- | drwxr-xr-x. 2 trainee trainee | + | |
- | drwxr-xr-x. 2 trainee trainee | + | |
- | drwxr-xr-x. 2 trainee trainee | + | |
- | drwxr-xr-x. 2 trainee trainee | + | |
- | drwxr-xr-x. 2 trainee trainee | + | |
- | drwxr-xr-x. 2 trainee trainee | + | |
- | -rw-r--r--. 1 trainee trainee | + | |
- | drwxr-xr-x. 2 trainee trainee | + | |
- | -rw-r--r--. 1 trainee trainee 442 Sep 25 14:24 vitext | + | |
</ | </ | ||
<WRAP center round important 60%> | <WRAP center round important 60%> | ||
- | Note the use of the **~** (tilde) character which is a shortcut to the current | + | **Important**: |
</ | </ | ||
- | ===Command Line Switches=== | + | Then change the **trainee** shell using the **chsh** command, specifying |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **cp** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ====1.14 - The file Command==== | + | |
- | + | ||
- | This command determines a file type: | + | |
< | < | ||
- | [trainee@redhat9 | + | [trainee@redhat9 |
- | /home/trainee/test: empty | + | Changing shell for trainee. |
+ | New shell [/bin/bash]: | ||
+ | Password: trainee | ||
+ | Shell changed. | ||
</ | </ | ||
<WRAP center round important 60%> | <WRAP center round important 60%> | ||
- | **Important** | + | **Important**: Note that the password entered will **not** be visible. |
</ | </ | ||
- | Using the < | + | Next, check the active shell for **trainee** : |
< | < | ||
- | [trainee@redhat9 | + | [trainee@redhat9 |
+ | /bin/bash | ||
</ | </ | ||
- | Now use the **file** command once again to determine the file type: | + | Lastly check the shell stipulated in the **/etc/passwd** file for **trainee**: |
< | < | ||
- | [trainee@redhat9 | + | [trainee@redhat9 |
- | / | + | trainee: |
</ | </ | ||
- | ===Command Line Switches=== | + | <WRAP center round important |
- | + | **Important**: You will notice that the active shell is always | |
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **file** command | + | |
</ | </ | ||
- | ====1.15 - The cat Command==== | + | Change your shell to **/ |
- | + | ||
- | This commands concatenate files, or standard input, | + | |
< | < | ||
- | [trainee@redhat9 | + | [trainee@redhat9 ~]$ chsh |
- | fenestros | + | Changing shell for trainee. |
- | </ | + | New shell [/bin/sh]: /bin/bash |
- | + | Password: trainee | |
- | ===Command Line Switches=== | + | Shell changed. |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **cat** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ====1.16 - The mv Command==== | + | |
- | + | ||
- | This command renames a source to a destination or moves sources to a directory: | + | |
- | + | ||
- | < | + | |
- | [trainee@redhat9 tmp]$ mv ~/test . | + | |
- | + | ||
- | [trainee@redhat9 tmp]$ ls -l ~ | + | |
- | total 4 | + | |
- | drwxr-xr-x. 2 trainee trainee | + | |
- | drwxr-xr-x. 2 trainee trainee | + | |
- | drwxr-xr-x. 2 trainee trainee | + | |
- | drwxr-xr-x. 2 trainee trainee | + | |
- | drwxr-xr-x. 2 trainee trainee | + | |
- | drwxr-xr-x. 2 trainee trainee | + | |
- | drwxr-xr-x. 2 trainee trainee | + | |
- | drwxr-xr-x. 2 trainee trainee | + | |
- | -rw-r--r--. 1 trainee trainee 442 Sep 25 14:24 vitext | + | |
- | + | ||
- | [trainee@redhat9 tmp]$ mv test TeSt | + | |
- | + | ||
- | [trainee@redhat9 tmp]$ ls -l | + | |
- | total 4 | + | |
- | srwxrwxrwx. 1 gdm | + | |
- | srwxrwxrwx. 1 gdm | + | |
- | drwxr-xr-x. 2 root root 54 Sep 25 13:10 inode | + | |
- | drwx------. 3 root root 17 Sep 25 12:45 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-colord.service-FfNuds | + | |
- | drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-dbus-broker.service-VIpKgR | + | |
- | drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-kdump.service-0SbYbm | + | |
- | drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-ModemManager.service-k4DpLF | + | |
- | drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-power-profiles-daemon.service-mIx9S5 | + | |
- | drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-rtkit-daemon.service-2gD28Z | + | |
- | drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-switcheroo-control.service-rLbOK4 | + | |
- | drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-systemd-logind.service-uLNyfd | + | |
- | drwx------. 3 root root 17 Sep 25 12:44 systemd-private-7e6b4544d8d34a9bb30a13aeb8e4e02a-upower.service-bIpAUN | + | |
- | -rw-r--r--. 1 trainee trainee 10 Sep 25 15:01 TeSt | + | |
</ | </ | ||
<WRAP center round important 60%> | <WRAP center round important 60%> | ||
- | **Important** | + | **Important**: Note that the password entered will **not** be visible. |
</ | </ | ||
- | ===Command Line Switches=== | + | ====1.4 - The Prompt==== |
- | <WRAP center round todo 60%> | + | A user's prompt depends on their status: |
- | **To do** : Use the **--help** option of the **mv** command to view the command line switches. | + | |
- | </ | + | |
- | ====1.17 - The mkdir Command==== | + | * **$** for a normal user, |
+ | * **#** for root. | ||
- | This command creates the directory(ies) if it (they) does (do) not exist: | + | ====1.5 - Recalling commands ==== |
- | < | + | The **/bin/bash** shell can be used to recall |
- | [trainee@redhat9 tmp]$ cd ~ | + | |
- | + | ||
- | [trainee@redhat9 ~]$ mkdir testdir | + | |
- | + | ||
- | [trainee@redhat9 ~]$ ls | + | |
- | Desktop | + | |
- | </ | + | |
- | + | ||
- | ===Command Line Switches=== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option | + | |
- | </ | + | |
- | + | ||
- | ====1.18 - The rmdir Command==== | + | |
- | + | ||
- | This command removes the directory(ies) if it (they) is (are) **empty**: | + | |
< | < | ||
- | [trainee@redhat9 ~]$ rmdir testdir | + | [trainee@redhat9 ~]$ history | more |
- | + | 1 su - | |
- | [trainee@redhat9 ~]$ ls | + | 2 exit |
- | Desktop | + | 3 su - |
+ | 4 clear | ||
+ | 5 cd / | ||
+ | 6 ls -l | ||
+ | 7 cd afs | ||
+ | 8 ls | ||
+ | 9 cd / | ||
+ | 10 su - | ||
+ | 11 cd ~ | ||
+ | 12 vi vitext | ||
+ | 13 view vitext | ||
+ | 14 vi vitext | ||
+ | 15 vi .exrc | ||
+ | 16 vi vitext | ||
+ | 17 su - | ||
+ | 18 stty -a | ||
+ | 19 date | ||
+ | 20 who | ||
+ | 21 df | ||
+ | 22 df -h | ||
+ | 23 free | ||
+ | 24 free -h | ||
+ | 25 whoami | ||
+ | 26 su - | ||
+ | 27 pwd | ||
+ | 28 cd /tmp | ||
+ | 29 pwd | ||
+ | 30 ls | ||
+ | 31 su - | ||
+ | 32 touch test | ||
+ | 33 ls | ||
+ | 34 echo fenestros | ||
+ | 35 cp test ~ | ||
+ | 36 ls -l ~ | ||
+ | 37 file ~/test | ||
+ | --More-- | ||
+ | [q] | ||
</ | </ | ||
- | ===Command Line Switches=== | + | <WRAP center round important |
- | + | **Important**: The history is specific | |
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **rmdir** command | + | |
</ | </ | ||
- | ====1.19 - The rm Command==== | + | The command history is in **emacs** mode by default. As a result, the last command is recalled using the **[Up Arrow]** key or the **[CTRL]-[P]** keys and the next command is recalled using the **[Down Arrow]** key or the **[CTRL]-[N]** keys : |
- | This command removes a directory, empty of not, as well as files: | + | ^ Control Character |
+ | | [CTRL]-[P] (= Up Arrow) | ||
+ | | [CTRL]-[N] (= Down Arrow) | ||
- | < | + | To move through the history line : |
- | [trainee@redhat9 ~]$ mkdir testdir1 | + | |
- | [trainee@redhat9 ~]$ cd /tmp | + | ^ Control Character |
+ | | [CTRL]-[A] | Move to the beginning of the line | | ||
+ | | [CTRL]-[E] | Move to the end of the line | | ||
+ | | [CTRL]-[B] | Move one character to the left | | ||
+ | | [CTRL]-[F] | Move one character to the right | | ||
+ | | [CTRL]-[D] | Delete the character under the cursor | | ||
- | [trainee@redhat9 tmp]$ echo " | + | To search the history, use the keys : |
- | [trainee@redhat9 tmp]$ cd ~ | + | ^ Control Character |
+ | | [CTRL]-[R] // | ||
+ | | [CTRL]-[S] // | ||
+ | | [CTRL]-[G] | Quit the search mode | | ||
- | [trainee@redhat9 ~]$ mv /tmp/TeSt ~/ | + | It is also possible |
- | + | ||
- | [trainee@redhat9 ~]$ ls -lR testdir1/ | + | |
- | testdir1/: | + | |
- | total 4 | + | |
- | -rw-r--r--. 1 trainee trainee 10 Sep 25 15:08 TeSt | + | |
- | + | ||
- | [trainee@redhat9 ~]$ rmdir testdir1/ | + | |
- | rmdir: failed | + | |
- | + | ||
- | [trainee@redhat9 ~]$ rm -rf testdir1 | + | |
- | + | ||
- | [trainee@redhat9 ~]$ ls | + | |
- | Desktop | + | |
- | </ | + | |
- | + | ||
- | ===Command Line Switches=== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **rm** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ====1.20 - The sort Command ==== | + | |
- | + | ||
- | This command writes a sorted concatenation of all files to standard output: | + | |
< | < | ||
- | [trainee@redhat9 ~]$ touch aac abc bca xyz | ||
- | |||
[trainee@redhat9 ~]$ ls | [trainee@redhat9 ~]$ ls | ||
+ | aac abc bca Desktop Documents Downloads Music Pictures Public Templates Videos vitext xyz | ||
- | aac abc bca Desktop | + | [trainee@redhat9 ~]$ !! |
- | + | ls | |
- | [trainee@redhat9 ~]$ ls | sort | + | aac abc bca Desktop Documents Downloads Music Pictures Public Templates Videos vitext xyz |
- | aac | + | |
- | abc | + | |
- | bca | + | |
- | Desktop | + | |
- | Documents | + | |
- | Downloads | + | |
- | Music | + | |
- | Pictures | + | |
- | Public | + | |
- | Templates | + | |
- | Videos | + | |
- | vitext | + | |
- | xyz | + | |
- | + | ||
- | [trainee@redhat9 ~]$ ls | sort -r | + | |
- | xyz | + | |
- | vitext | + | |
- | Videos | + | |
- | Templates | + | |
- | Public | + | |
- | Pictures | + | |
- | Music | + | |
- | Downloads | + | |
- | Documents | + | |
- | Desktop | + | |
- | bca | + | |
- | abc | + | |
- | aac | + | |
</ | </ | ||
- | <WRAP center round important 60%> | + | You can recall |
- | **Important** - Note the use of the **|** character, called | + | |
- | </ | + | |
- | + | ||
- | ===Command Line Switches=== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option | + | |
- | </ | + | |
- | + | ||
- | ====1.21 - The more Command ==== | + | |
- | + | ||
- | This command is used to display a long file page by page: | + | |
< | < | ||
- | [trainee@redhat9 ~]$ more / | + | [trainee@redhat9 ~]$ history |
- | # / | + | 1 su - |
- | # $Id: services, | + | 2 exit |
- | # | + | 3 su - |
- | # Network services, Internet style | + | 4 clear |
- | # IANA services version: last updated 2016-07-08 | + | 5 cd / |
- | # | + | 6 ls -l |
- | # Note that it is presently the policy of IANA to assign a single well-known | + | 7 cd afs |
- | # port number for both TCP and UDP; hence, most entries here have two entries | + | 8 ls |
- | # even if the protocol doesn' | + | 9 cd / |
- | # Updated from RFC 1700, ``Assigned Numbers'' | + | 10 su - |
- | # are included, only the more common ones. | + | ... |
- | # | + | 85 echo $SHELL |
- | # The latest IANA port assignments can be gotten from | + | 86 cat /etc/passwd | grep trainee |
- | # | + | 87 chsh |
- | # The Well Known Ports are those from 0 through 1023. | + | 88 history | more |
- | # The Registered Ports are those from 1024 through 49151 | + | 89 clear |
- | # The Dynamic and/or Private Ports are those from 49152 through 65535 | + | 90 ls |
- | # | + | 91 history |
- | # Each line describes one service, and is of the form: | + | |
- | # | + | |
- | # service-name | + | |
- | tcpmux | + | [trainee@redhat9 ~]$ !90 |
- | tcpmux | + | ls |
- | rje | + | aac abc bca Desktop Documents Downloads Music Pictures Public Templates Videos vitext xyz |
- | rje | + | |
- | --More--(0%) | + | |
- | [q] | + | |
</ | </ | ||
- | <WRAP center round important 60%> | + | The command |
- | **Important** - The < | + | |
- | </ | + | |
- | + | ||
- | ===Command Line Switches=== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **more** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ====1.22 - The less Command==== | + | |
- | + | ||
- | The **less** command produces a similar result to that of the **more** command. Practice using the less command and refer to the help using **less --help**. Which command seems the most powerful and versatile ? | + | |
< | < | ||
- | [trainee@redhat9 ~]$ less /etc/services | + | [trainee@redhat9 ~]$ cat /etc/profile | grep HISTSIZE |
- | # / | + | HISTSIZE=1000 |
- | # $Id: services,v 1.49 2017/08/18 12:43:23 ovasik Exp $ | + | export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL |
- | # | + | |
- | # Network services, Internet style | + | |
- | # IANA services version: last updated 2016-07-08 | + | |
- | # | + | |
- | # Note that it is presently the policy of IANA to assign a single well-known | + | |
- | # port number for both TCP and UDP; hence, most entries here have two entries | + | |
- | # even if the protocol doesn' | + | |
- | # Updated from RFC 1700, ``Assigned Numbers'' | + | |
- | # are included, only the more common ones. | + | |
- | # | + | |
- | # The latest IANA port assignments can be gotten from | + | |
- | # | + | |
- | # The Well Known Ports are those from 0 through 1023. | + | |
- | # The Registered Ports are those from 1024 through 49151 | + | |
- | # The Dynamic and/or Private Ports are those from 49152 through 65535 | + | |
- | # | + | |
- | # Each line describes one service, and is of the form: | + | |
- | # | + | |
- | # service-name | + | |
- | + | ||
- | tcpmux | + | |
- | tcpmux | + | |
- | rje | + | |
- | rje | + | |
- | / | + | |
- | [q] | + | |
</ | </ | ||
- | ===Command Line Switches=== | + | You will notice that in the previous case, the value of **HISTSIZE** is **1000**. This implies that the last thousand commands are memorised. |
- | <WRAP center round todo 60%> | + | The memorised commands are stored in the **~/ |
- | **To do** : Use the **--help** option of the **less** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ====1.23 - The find Command=== | + | |
- | + | ||
- | This command is used to search | + | |
< | < | ||
- | [trainee@redhat9 ~]$ find acc | + | [trainee@redhat9 ~]$ nl .bash_history | tail |
- | find: ‘acc’: No such file or directory | + | 59 ls |
- | + | 60 ls | sort | |
- | [trainee@redhat9 ~]$ find aac | + | 61 ls | sort -r |
- | aac | + | 62 more / |
+ | 63 less / | ||
+ | 64 find acc | ||
+ | | ||
+ | 66 su - | ||
+ | 67 sleep 10 | ||
+ | 68 su - | ||
</ | </ | ||
<WRAP center round important 60%> | <WRAP center round important 60%> | ||
- | **Important** : Note that when the file cannot be found, | + | **Important**: |
</ | </ | ||
- | ===Command Line Switches=== | + | ====1.6 - Generating file name endings==== |
- | <WRAP center round todo 60%> | + | The /bin/bash shell is used to generate file name endings. This is accomplished by using the **[Tab]** key. In the following example, |
- | **To do** : Use the **--help** option of the **find** command to view the command | + | |
- | </ | + | |
- | + | ||
- | ====1.24 - The su Command=== | + | |
- | This command is used to change the effective user id and group id to that of the user passed as an argument. When executed with no argument, the system assumes the destination user is **root**: | + | $ ls .b [Tab][Tab][Tab] |
< | < | ||
- | [trainee@redhat9 ~]$ su - | + | [trainee@redhat9 ~]$ ls .bash |
- | Password: fenestros | + | .bash_history .bash_logout .bash_profile .bashrc |
- | + | ||
- | [root@redhat9 ~]# | + | |
</ | </ | ||
<WRAP center round important 60%> | <WRAP center round important 60%> | ||
- | **Important** : Note that the password will not be visible. | + | **Important**: |
</ | </ | ||
- | ===Command Line Switches=== | + | The same possibility exists for generating command name endings. In this case enter the following command: |
- | <WRAP center round todo 60%> | + | $ mo [Tab][Tab] |
- | **To do** : Use the **--help** option of the **su** command to view the command line switches. | + | |
- | </ | + | |
- | ====1.25 - The updatedb and locate Commands==== | + | Press the < |
- | + | ||
- | The **locate** command is used to list files in databases that match a pattern supplied as an argument to the command. The **locate** command uses a database. This database needs to be created using the **updatedb** command before using the **locate** command. | + | |
- | + | ||
- | The default database is **/ | + | |
< | < | ||
- | [root@redhat9 ~]# ls -l / | + | [trainee@redhat9 ~]$ mo |
- | -rw-r-----. 1 root slocate 3436535 Sep 25 15:21 / | + | modinfo |
+ | modprobe | ||
</ | </ | ||
+ | ====1.7 - The interactive shell ==== | ||
- | <WRAP center round important 60%> | + | When using the shell, we often need to execute a command on several files instead of processing them individually. For this purpose we can use metacharacters. |
- | **Important** : For information concerning | + | |
- | </ | + | |
- | The **updatedb** command is configured by editing | + | ^ Metacharacter ^ Description ^ |
+ | | ***** | Matches one or more characters | | ||
+ | | **?** | Matches a single character | | ||
+ | | **[abc]** | Matches any one of the characters between square brackets | | ||
+ | | **[!abc]** | Matches any character except those between square brackets | | ||
+ | | [m-t] | Matches any character from m through to t | | ||
+ | | [!m-t] | Matches any character other than m through to t | | ||
+ | | **? | ||
+ | | ***(expression1|expression2| ...)** | Matches 0 to x occurences of expression1 OR 0 to x occurences of expression2 OR ... | | ||
+ | | **+(expression1|expression2| ...)** | Matches 1 to x occurences of expression1 OR 1 to x occurences of expression2 OR ... | | ||
+ | | **@(expression1|expression2| ...)** | Matches 1 occurrence of expression1 OR 1 occurence of expression2 OR ... | | ||
+ | | **!(expression1|expression2| ...)** | Matches 0 occurrences of expression1 OR 0 occurrences of expression2 OR ... | | ||
- | < | + | ===The * Metacharacter=== |
- | [root@redhat9 ~]# cat / | + | |
- | PRUNE_BIND_MOUNTS | + | |
- | PRUNEFS | + | |
- | PRUNENAMES | + | |
- | PRUNEPATHS | + | |
- | </ | + | |
- | Use of these two commands is very simple: | + | In your home directory, create a **training** directory. Then create 5 files in this directory named f1, f2, f3, f4 and f5 respectively: |
< | < | ||
- | [root@redhat9 ~]# updatedb | + | [trainee@redhat9 ~]$ mkdir training |
- | + | [trainee@redhat9 ~]$ cd training | |
- | [root@redhat9 ~]# locate aac | + | [trainee@redhat9 training]$ touch f1 f2 f3 f4 f5 |
- | /home/trainee/aac | + | [trainee@redhat9 training]$ ls |
- | / | + | f1 f2 f3 f4 f5 |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
- | / | + | |
</ | </ | ||
- | The default database is **/ | + | To demonstrate the use of the metacharacter |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | -rw-r-----. 1 root slocate 3436535 Sep 25 15:21 / | + | f1 f2 f3 f4 f5 |
</ | </ | ||
<WRAP center round important 60%> | <WRAP center round important 60%> | ||
- | **Important**: | + | **Important**: |
</ | </ | ||
- | ===Command Line Switches=== | + | ===The ? Metacharacter=== |
- | <WRAP center round todo 60%> | + | Now create |
- | **To do** : Use the **--help** option of the **updatedb** | + | |
- | </ | + | |
- | + | ||
- | ==== 1.26 - The whereis Command=== | + | |
- | + | ||
- | This command is used to show the full paths of the executable, the configuration | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | passwd: / | + | |
</ | </ | ||
- | ===Command Line Switches=== | + | Then enter the following |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **whereis** | + | |
- | </ | + | |
- | + | ||
- | ==== 1.27 - The which Command==== | + | |
- | + | ||
- | This command searches the PATH variable and returns to standard output the first full path associated with the argument: | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | / | + | f52 f62 |
</ | </ | ||
- | ===Command Line Switches=== | + | <WRAP center round important |
- | + | **Important**: Note that the **?** character replaces | |
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **which** command to view the command line switches. | + | |
</ | </ | ||
- | ====1.28 - The uptime Command==== | + | ===The |
- | This command prints to standard output the current time, the length of time the system has been up, the number of users on the system and the average number of jobs in the run queue over the last 1, 5 and 15 minutes: | + | Usage can take several different forms: |
- | < | + | ^ Metacharacter ^ Description ^ |
- | [root@redhat9 ~]# uptime | + | | [xyz] | Represents either x or y or z | |
- | 15:27:33 up 2:42, 1 user, load average: 0.00, 0.00, 0.00 | + | | [m-t] | Represents a character in the range m to t | |
- | </ | + | | [!xyz] | Represents any character other than x or y or z | |
- | + | | [!m-t] | Represents any character outside | |
- | ===Command Line Switches=== | + | |
- | + | ||
- | The switches associated with this command are: | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option | + | |
- | </ | + | |
- | + | ||
- | ====1.29 - The w Command==== | + | |
- | This command outputs | + | To demonstrate |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | | + | |
- | USER | + | |
- | trainee | + | |
</ | </ | ||
- | The JCPU time is the time used by all processes attached to the tty. It does not include past background jobs, but does include currently running background jobs. | + | Then enter the following commands and note the result: |
- | + | ||
- | The PCPU time is the time used by the current process, named in the **what** field. | + | |
- | + | ||
- | ===Command Line Switches=== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **w** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ====1.30 - The uname Command==== | + | |
- | + | ||
- | This command prints system information to the standard output: | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | Linux redhat9.ittraining.loc 5.14.0-427.37.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Sep 13 12:41:50 EDT 2024 x86_64 x86_64 x86_64 GNU/Linux | + | a100 f1 f2 f3 f4 f5 f52 f62 |
- | + | ||
- | [root@redhat9 ~]# uname -s | + | |
- | Linux | + | |
- | + | ||
- | [root@redhat9 ~]# uname -n | + | |
- | redhat9.ittraining.loc | + | |
- | + | ||
- | [root@redhat9 ~]# uname -r | + | |
- | 5.14.0-427.37.1.el9_4.x86_64 | + | |
- | + | ||
- | [root@redhat9 ~]# uname -v | + | |
- | #1 SMP PREEMPT_DYNAMIC Fri Sep 13 12:41:50 EDT 2024 | + | |
- | + | ||
- | [root@redhat9 ~]# uname -m | + | |
- | x86_64 | + | |
- | + | ||
- | [root@redhat9 ~]# uname -p | + | |
- | x86_64 | + | |
- | + | ||
- | [root@redhat9 ~]# uname -i | + | |
- | x86_64 | + | |
- | + | ||
- | [root@redhat9 ~]# uname -o | + | |
- | GNU/Linux | + | |
</ | </ | ||
- | ===Command Line Switches=== | + | <WRAP center round important |
- | + | **Important**: Note here that all files starting with the letters | |
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **uname** command to view the command line switches. | + | |
</ | </ | ||
- | |||
- | ====1.31 - The du Command==== | ||
- | |||
- | This command summarizes disk usage of each file, recursively for directories: | ||
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | 0 / | + | a100 f1 f2 f3 f4 f5 f52 f62 |
- | 0 / | + | |
- | 358M /boot | + | |
- | 0 / | + | |
- | 29M / | + | |
- | 4.4M /home | + | |
- | 0 / | + | |
- | 0 / | + | |
- | 0 / | + | |
- | 0 / | + | |
- | 0 / | + | |
- | 0 / | + | |
- | 2.0G /root | + | |
- | 9.2M /run | + | |
- | 0 / | + | |
- | 0 / | + | |
- | 0 / | + | |
- | 12K / | + | |
- | 4.0G /usr | + | |
- | 368M /var | + | |
</ | </ | ||
<WRAP center round important 60%> | <WRAP center round important 60%> | ||
- | **Important** : Note the use of the **2>/ | + | **Important**: |
</ | </ | ||
- | |||
- | ===Command Line Switches=== | ||
- | |||
- | <WRAP center round todo 60%> | ||
- | **To do** : Use the **--help** option of the **du** command to view the command line switches. | ||
- | </ | ||
- | |||
- | ==== 1.32 - The clear Command ==== | ||
- | |||
- | This command is used to clear the current screen of the terminal: | ||
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | + | f1 f2 f3 f4 f5 f52 f62 | |
- | [root@redhat9 ~]# | + | |
</ | </ | ||
- | ==== 1.33 - The exit Command ==== | + | <WRAP center round important |
- | + | **Important**: Note here that all files are displayed on the screen, with the exception of one file beginning | |
- | This command exits the current shell: | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# exit | + | |
- | logout | + | |
- | [trainee@redhat9 ~]$ | + | |
- | </ | + | |
- | + | ||
- | ===Command Line Switches=== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **help** command | + | |
</ | </ | ||
- | |||
- | ====1.34 - The logout Command==== | ||
- | |||
- | This command logs out a user from a login shell writing the utmp and wtmp entries in the log files. | ||
- | |||
- | ===Command Line Switches=== | ||
- | |||
- | <WRAP center round todo 60%> | ||
- | **To do** : Use the **help** command with **logout** option to view the command line switches. | ||
- | </ | ||
- | |||
- | ====1.35 - The sleep Command ==== | ||
- | |||
- | This command pauses for a number seconds. The number is specified as the first argument. | ||
< | < | ||
- | [trainee@redhat9 | + | [trainee@redhat9 |
+ | a100 | ||
</ | </ | ||
- | ===Command Line Switches=== | + | <WRAP center round important |
- | + | **Important**: Note here that only the file starting with the letter | |
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **sleep** command to view the command line switches. | + | |
</ | </ | ||
- | |||
- | ====1.36 - The wall Command==== | ||
- | |||
- | Wall sends a message to everybody logged in with their mesg(1) permission set to yes. The message can be given as an argument to wall, or it can be sent to wall's standard input. | ||
< | < | ||
- | [trainee@redhat9 | + | [trainee@redhat9 |
- | Password: fenestros | + | [a-f] |
- | + | ||
- | [root@redhat9 ~]# wall this is a message from root | + | |
- | + | ||
- | Broadcast message from root@redhat9.ittraining.loc (pts/0) (Wed Sep 25 15:31:46 | + | |
- | + | ||
- | this is a message from root | + | |
</ | </ | ||
- | The wall command ignores the TZ environment variable. The time displayed on the first page is based on the system' | + | <WRAP centre round important 60%> |
- | + | **Important**: Note that in this case, there are no files named **a**, **b**, **c**, **d**, **e** or **f**. For this reason, having found no correspondence between the filter used and the objects in the current directory, | |
- | <code> | + | |
- | [root@redhat9 ~]# date | + | |
- | Wed Sep 25 03:33:11 PM CEST 2024 | + | |
- | </ | + | |
- | + | ||
- | ====1.37 - The seq Command==== | + | |
- | + | ||
- | The **seq** command prints numbers from FIRST to LAST, in steps of INCREMENT: | + | |
- | + | ||
- | | + | |
- | | + | |
- | | + | |
- | + | ||
- | For example : | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# seq 10 | + | |
- | 1 | + | |
- | 2 | + | |
- | 3 | + | |
- | 4 | + | |
- | 5 | + | |
- | 6 | + | |
- | 7 | + | |
- | 8 | + | |
- | 9 | + | |
- | 10 | + | |
- | + | ||
- | [root@redhat9 ~]# seq 10 20 | + | |
- | 10 | + | |
- | 11 | + | |
- | 12 | + | |
- | 13 | + | |
- | 14 | + | |
- | 15 | + | |
- | 16 | + | |
- | 17 | + | |
- | 18 | + | |
- | 19 | + | |
- | 20 | + | |
- | + | ||
- | [root@redhat9 ~]# seq 20 10 90 | + | |
- | 20 | + | |
- | 30 | + | |
- | 40 | + | |
- | 50 | + | |
- | 60 | + | |
- | 70 | + | |
- | 80 | + | |
- | 90 | + | |
- | </ | + | |
- | + | ||
- | ===Command Line Switches=== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **seq** command | + | |
</ | </ | ||
- | ====1.38 - The screen Command==== | + | ====1.8 - The extglob option==== |
- | Screen is a full-screen window manager | + | Enable the **extglob** option in the bash shell so that you can use **?(expression), *(expression), +(expression), @(expression) and !(expression)**: |
- | + | ||
- | The screen command is not installed by default under RHEL 9: | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | / | + | |
- | + | ||
- | [root@redhat9 ~]# dnf makecache | + | |
- | Updating Subscription Management repositories. | + | |
- | Red Hat Enterprise Linux 9 for x86_64 - AppStream (RPMs) | + | |
- | Red Hat Enterprise Linux 9 for x86_64 - BaseOS (RPMs) | + | |
- | Metadata cache created. | + | |
- | + | ||
- | [root@redhat9 ~]# dnf install screen -y | + | |
- | Updating Subscription Management repositories. | + | |
- | Last metadata expiration check: 0:00:49 ago on Wed 25 Sep 2024 03:35:01 PM CEST. | + | |
- | No match for argument: screen | + | |
- | Error: Unable to find a match: screen | + | |
- | + | ||
- | [root@redhat9 ~]# subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms | + | |
- | Repository ' | + | |
- | + | ||
- | [root@redhat9 ~]# dnf install https:// | + | |
- | Updating Subscription Management repositories. | + | |
- | Red Hat CodeReady Linux Builder for RHEL 9 x86_64 (RPMs) | + | |
- | Last metadata expiration check: 0:00:01 ago on Wed 25 Sep 2024 03:39:17 PM CEST. | + | |
- | epel-release-latest-9.noarch.rpm | + | |
- | Dependencies resolved. | + | |
- | ========================================================================================================= | + | |
- | | + | |
- | ========================================================================================================= | + | |
- | Installing: | + | |
- | | + | |
- | + | ||
- | Transaction Summary | + | |
- | ========================================================================================================= | + | |
- | Install | + | |
- | + | ||
- | Total size: 18 k | + | |
- | Installed size: 26 k | + | |
- | Is this ok [y/N]: y | + | |
- | Downloading Packages: | + | |
- | Running transaction check | + | |
- | Transaction check succeeded. | + | |
- | Running transaction test | + | |
- | Transaction test succeeded. | + | |
- | Running transaction | + | |
- | Preparing | + | |
- | Installing | + | |
- | Running scriptlet: epel-release-9-8.el9.noarch | + | |
- | Many EPEL packages require the CodeReady Builder (CRB) repository. | + | |
- | It is recommended that you run / | + | |
- | + | ||
- | Verifying | + | |
- | Installed products updated. | + | |
- | + | ||
- | Installed: | + | |
- | epel-release-9-8.el9.noarch | + | |
- | + | ||
- | Complete! | + | |
- | + | ||
- | [root@redhat9 ~]# dnf install screen | + | |
- | Updating Subscription Management repositories. | + | |
- | Extra Packages for Enterprise Linux 9 - Next - x86_64 | + | |
- | Last metadata expiration check: 0:00:01 ago on Wed 25 Sep 2024 03:43:49 PM CEST. | + | |
- | Dependencies resolved. | + | |
- | ========================================================================================================= | + | |
- | | + | |
- | ========================================================================================================= | + | |
- | Installing: | + | |
- | | + | |
- | + | ||
- | Transaction Summary | + | |
- | ========================================================================================================= | + | |
- | Install | + | |
- | + | ||
- | Total download size: 649 k | + | |
- | Installed size: 957 k | + | |
- | Is this ok [y/N]: y | + | |
- | Downloading Packages: | + | |
- | screen-4.8.0-6.el9.x86_64.rpm | + | |
- | --------------------------------------------------------------------------------------------------------- | + | |
- | Total 875 kB/s | 649 kB | + | |
- | Running transaction check | + | |
- | Transaction check succeeded. | + | |
- | Running transaction test | + | |
- | Transaction test succeeded. | + | |
- | Running transaction | + | |
- | Preparing | + | |
- | Running scriptlet: screen-4.8.0-6.el9.x86_64 | + | |
- | Installing | + | |
- | Running scriptlet: screen-4.8.0-6.el9.x86_64 | + | |
- | Verifying | + | |
- | Installed products updated. | + | |
- | + | ||
- | Installed: | + | |
- | screen-4.8.0-6.el9.x86_64 | + | |
- | + | ||
- | Complete! | + | |
</ | </ | ||
- | Create a session with screen: | + | The **shopt** command is used to enable or disable options for optional shell behaviour. The list of options can be viewed by executing the **shopt** command without options: |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
+ | autocd off | ||
+ | assoc_expand_once off | ||
+ | cdable_vars off | ||
+ | cdspell off | ||
+ | checkhash off | ||
+ | checkjobs off | ||
+ | checkwinsize on | ||
+ | cmdhist on | ||
+ | compat31 off | ||
+ | compat32 off | ||
+ | compat40 off | ||
+ | compat41 off | ||
+ | compat42 off | ||
+ | compat43 off | ||
+ | compat44 off | ||
+ | complete_fullquote on | ||
+ | direxpand off | ||
+ | dirspell off | ||
+ | dotglob off | ||
+ | execfail off | ||
+ | expand_aliases on | ||
+ | extdebug off | ||
+ | extglob on | ||
+ | extquote on | ||
+ | failglob off | ||
+ | force_fignore on | ||
+ | globasciiranges on | ||
+ | globstar off | ||
+ | gnu_errfmt off | ||
+ | histappend on | ||
+ | histreedit off | ||
+ | histverify off | ||
+ | hostcomplete off | ||
+ | huponexit off | ||
+ | inherit_errexit off | ||
+ | interactive_comments on | ||
+ | lastpipe off | ||
+ | lithist off | ||
+ | localvar_inherit off | ||
+ | localvar_unset off | ||
+ | login_shell on | ||
+ | mailwarn off | ||
+ | no_empty_cmd_completion off | ||
+ | nocaseglob off | ||
+ | nocasematch off | ||
+ | nullglob off | ||
+ | progcomp on | ||
+ | progcomp_alias off | ||
+ | promptvars on | ||
+ | restricted_shell off | ||
+ | shift_verbose off | ||
+ | sourcepath on | ||
+ | syslog_history off | ||
+ | xpg_echo off | ||
</ | </ | ||
- | Now press the < | + | ===? |
- | To return to the first screen, use the < | + | Create |
- | + | ||
- | To see the status of all active screens, use the **screen -ls** command: | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | There is a screen on: | + | |
- | 3103.mysession | + | |
- | 1 Socket in / | + | |
</ | </ | ||
- | Now enter the following | + | Enter the following |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | [1] 3153 | + | f123.txt f.txt |
- | + | ||
- | [root@redhat9 ~]# jobs -l | + | |
- | [1]+ 3153 Running | + | |
- | + | ||
- | [root@redhat9 ~]# | + | |
</ | </ | ||
- | In order to detach the current screen press the <key>CTRL</ | + | <WRAP centre round important 60%> |
+ | **Important**: | ||
+ | </WRAP> | ||
- | < | + | ===*(expression)=== |
- | [root@redhat9 ~]# screen -S mysession | + | |
- | [detached from 3103.mysession] | + | |
- | [root@redhat9 ~]# | + | Enter the following command: |
- | </ | + | |
- | + | ||
- | To re-attach the screen, execute | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
+ | f123123123.txt f123123.txt f123123.txt f.txt | ||
</ | </ | ||
- | Using the jobs command, check if the process created by the sleep command is still running: | + | <WRAP centre round important 60%> |
- | + | **Important**: Note here that the command | |
- | <code> | + | |
- | [root@redhat9 ~]# jobs -l | + | |
- | [1]+ 3153 Running | + | |
- | + | ||
- | [root@redhat9 ~]# | + | |
- | </ | + | |
- | + | ||
- | To move forward or backwards between screens press the < | + | |
- | + | ||
- | Once again detach the current screen by pressing the < | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# screen -S mysession | + | |
- | [detached from 3103.mysession] | + | |
- | + | ||
- | [root@redhat9 ~]# | + | |
- | </ | + | |
- | + | ||
- | Now create a new, non-nested screen: | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# screen -S mysession1 | + | |
- | </ | + | |
- | + | ||
- | Use the **screen -ls** command to see what has happened: | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# screen -ls | + | |
- | There are screens on: | + | |
- | 3181.mysession1 (Attached) | + | |
- | 3103.mysession | + | |
- | 2 Sockets in / | + | |
- | </ | + | |
- | + | ||
- | Note, however, | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# jobs -l | + | |
- | [root@redhat9 ~]# | + | |
- | </ | + | |
- | + | ||
- | To re-attach a specific screen, reference it by it's number: | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# screen -r 3103 | + | |
- | + | ||
- | [root@redhat9 ~]# screen -ls | + | |
- | There are screens on: | + | |
- | 3181.mysession1 (Attached) | + | |
- | 3103.mysession | + | |
- | 2 Sockets in / | + | |
- | </ | + | |
- | + | ||
- | Once again detach the current screen by pressing the < | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# screen -S mysession1 | + | |
- | [detached | + | |
- | + | ||
- | [root@redhat9 ~]# | + | |
- | </ | + | |
- | + | ||
- | Now check which screen you are connected | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# screen -ls | + | |
- | There are screens on: | + | |
- | 3181.mysession1 (Detached) | + | |
- | 3103.mysession | + | |
- | 2 Sockets in / | + | |
- | </ | + | |
- | + | ||
- | Finally, kill the two sessions: | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# screen -ls | + | |
- | There are screens on: | + | |
- | 3181.mysession1 (Detached) | + | |
- | 3103.mysession | + | |
- | 2 Sockets in / | + | |
- | </ | + | |
- | + | ||
- | Tuez maintenant les deux sessions : | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# screen -XS 3181 quit | + | |
- | + | ||
- | [root@redhat9 ~]# screen -ls | + | |
- | There is a screen on: | + | |
- | 3103.mysession | + | |
- | 1 Socket in / | + | |
- | + | ||
- | [root@redhat9 ~]# screen -XS 3103 quit | + | |
- | + | ||
- | [root@redhat9 ~]# screen -ls | + | |
- | No Sockets found in / | + | |
- | + | ||
- | </ | + | |
- | + | ||
- | ===Command Line Switches=== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option | + | |
</ | </ | ||
- | =====LAB #2 - Switches and Arguments===== | + | ===+(expression)=== |
- | Switches under Linux can either be short or long. Several differences are important to note. | + | Enter the following command: |
- | + | ||
- | Firstly short options are generally preceded by a single dash **-**, whilst long options are preceded by a double dash **- -**. | + | |
- | + | ||
- | An example is the help option used with most commands: | + | |
- | + | ||
- | * -h | + | |
- | * --help | + | |
- | + | ||
- | Secondly, Linux short switches can be combined whereas long switches cannot be combined. For example **ls -l -a -i** can also be written as **ls -lai**, **ls -lia** or **ls -ali**: | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | total 12 | + | f123123123.txt f123123.txt f123.txt |
- | | + | |
- | 128 dr-xr-xr-x. 18 root root 235 Oct 19 2023 .. | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | 100664689 drwx------. | + | |
- | 10601 drwx------. | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | 2074740 drwxrwxrwt. | + | |
- | 10599 drwxrwxrwt. | + | |
- | + | ||
- | [root@redhat9 ~]# ls -ali /tmp | + | |
- | total 12 | + | |
- | | + | |
- | 128 dr-xr-xr-x. 18 root root 235 Oct 19 2023 .. | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | 100664689 drwx------. | + | |
- | 10601 drwx------. | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | 2074740 drwxrwxrwt. | + | |
- | 10599 drwxrwxrwt. | + | |
- | + | ||
- | [root@redhat9 ~]# ls -ial /tmp | + | |
- | total 12 | + | |
- | | + | |
- | 128 dr-xr-xr-x. 18 root root 235 Oct 19 2023 .. | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | 100664689 drwx------. | + | |
- | 10601 drwx------. | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | 2074740 drwxrwxrwt. | + | |
- | 10599 drwxrwxrwt. 2 root root 6 Sep 25 12:30 .XIM-unix | + | |
</ | </ | ||
- | However **ls -l --all --inode** cannot be written **ls -l --allinode**: | + | < |
- | + | **Important** | |
- | < | + | |
- | [root@redhat9 ~]# ls -l --all --inode /tmp | + | |
- | total 12 | + | |
- | | + | |
- | 128 dr-xr-xr-x. 18 root root 235 Oct 19 2023 .. | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | 100664689 drwx------. | + | |
- | 10601 drwx------. | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | 2074740 drwxrwxrwt. | + | |
- | 10599 drwxrwxrwt. | + | |
- | + | ||
- | [root@redhat9 ~]# ls -l --allinode /tmp | + | |
- | ls: unrecognized option ' | + | |
- | Try 'ls --help' | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | **Important** | + | |
</ | </ | ||
- | =====LAB #3 - Regular Expressions===== | + | ===@(expression)=== |
- | Text files play a very important role under Linux. For example, almost all configuration files are simple text files and being able to manipulate them is of great importance. Manipulating text files is essentially achieved by using **Regular Expressions**. There are two types of Regular Expressions: | + | Enter the following command: |
- | + | ||
- | * The IEEE POSIX Basic Regular Expressions (**BRE**) understood by the commands **vi**, **grep**, **expr** and **sed**, | + | |
- | * The IEEE POSIX Extended Regular Expressions (**ERE**) understood by the commands **egrep** ( grep -E ) and **awk**. | + | |
- | + | ||
- | Regular Expressions use **Metacharacters**. Certain are common to both BREs and EREs: | + | |
- | + | ||
- | ^Metacharacter ^ Description ^ | + | |
- | | **%%^%%**// | + | |
- | | // | + | |
- | | **\**// | + | |
- | | **[**// | + | |
- | | **[%%^%%**// | + | |
- | | **.** | Match any character except when at the end of a line | | + | |
- | | // | + | |
- | | \< | Match //string// at the beginning of a word | | + | |
- | | \> | Match //string// at the end of a word | | + | |
- | + | ||
- | ===BREs=== | + | |
- | + | ||
- | Certain Metacharacters are specific to BREs: | + | |
- | + | ||
- | ^Metacharacter ^ Description ^ | + | |
- | | **\{x,y\}** | Match from **x** to **y** occurrences of the preceeding element | | + | |
- | | **\{x\}** | Match exactly **x** occurrences of the preceeding element | | + | |
- | | **\{x,\}** | Match **x** or more occurrences of the preceeding element | | + | |
- | | \(BRE) | Commit to memory the BRE | | + | |
- | | \1 | Recall the first BRE committed to memory | | + | |
- | | \2, \3 ... | Recall the second BRE committed to memory, recall the third BRE committed to memory ... | | + | |
- | + | ||
- | ===EREs=== | + | |
- | + | ||
- | Certain Metacharacters are specific to EREs: | + | |
- | + | ||
- | ^Metacharacter ^ Description ^ | + | |
- | | {x,y} | Match from **x** to **y** occurrences of the preceeding element | | + | |
- | | {x} | Match exactly **x** occurrences of the preceeding element | | + | |
- | | {x,} | Match **x** or more occurrences of the preceeding element | + | |
- | | ? | Matches 0 or 1 occurrence of the preceeding element | | + | |
- | | + | Matches 1 or more occurrence(s) of the preceeding element | | + | |
- | | %%|%% | Matches either the expression before or the expression after the operator | | + | |
- | | () | Combines the Reguler Expressions between the parentheses | | + | |
- | + | ||
- | =====Manipulating Text Files===== | + | |
- | + | ||
- | ====Text-search Utilities==== | + | |
- | + | ||
- | ===The grep Command=== | + | |
- | + | ||
- | The **grep** command can be used to find lines containing a string of characters in a group of files. The **-v** or **--invert-case** option can be stipulated to find lines that do **not** contain the specified string. | + | |
- | + | ||
- | The grep command is case sensitive. The **-i** or **--ignore-case** option can be specified in order to use grep in a non case sensitive search. | + | |
- | + | ||
- | The grep command can also use **BREs**. | + | |
- | + | ||
- | ==Command Line Switches== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **grep** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ===The egrep Command=== | + | |
- | + | ||
- | The **egrep** command is identical to the **grep** command when used with the **-E** switch. Both can use EREs. | + | |
- | + | ||
- | ==Command Line Switches== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **egrep** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ===The fgrep Command=== | + | |
- | + | ||
- | The **fgrep** command is identical to the **grep** command when used with the **-F** switch. Both have no knowledge of Regular Expressions. | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **fgrep** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ===LAB #4 - Using grep, egrep and fgrep=== | + | |
- | + | ||
- | Create the following file: | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# cd /tmp | + | |
- | + | ||
- | [root@redhat9 tmp]# vi greptest | + | |
- | + | ||
- | [root@redhat9 tmp]# cat greptest | + | |
- | fenestrOS | + | |
- | fenestros | + | |
- | 555-5555 | + | |
- | f | + | |
- | .fenestros | + | |
- | .fe | + | |
- | £ | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | Now use grep to search for lines containing at least one uppercase or lowercase letter: | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# grep ' | + | |
- | fenestrOS | + | |
- | fenestros | + | |
- | f | + | |
- | .fenestros | + | |
- | .fe | + | |
- | </ | + | |
- | + | ||
- | Next use grep to search for lines containing at least one uppercase or lowercase letter or a number: | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# grep ' | + | |
- | fenestrOS | + | |
- | fenestros | + | |
- | 555-5555 | + | |
- | f | + | |
- | .fenestros | + | |
- | .fe | + | |
- | </ | + | |
- | + | ||
- | To search for the NNN-NNNN pattern where N is a number, use the following command: | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# grep ' | + | |
- | 555-5555 | + | |
- | </ | + | |
- | + | ||
- | Lines containing just one character have that character both at the beginning (**^**) and at the end (**$**) of the line: | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# grep ' | + | |
- | f | + | |
- | £ | + | |
- | </ | + | |
- | + | ||
- | To search for a line containing a special character such as **.**, that character needs to be preceded by **\**: | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | .fenestros | + | f123.txt |
- | .fe | + | |
</ | </ | ||
<WRAP center round important 60%> | <WRAP center round important 60%> | ||
- | **Important** | + | **Important**: Note here that the command |
</ | </ | ||
- | Make the following changes to the greptest file: | + | ===!(expression)=== |
- | < | + | Enter the following |
- | [root@redhat9 tmp]# vi greptest | + | |
- | [root@redhat9 tmp]# cat greptest | + | |
- | # Starting comment | + | |
- | fenestrOS | + | |
- | fenestros | + | |
- | # Another comment | + | |
- | 555-5555 | + | |
- | f | + | |
- | + | ||
- | .fenestros | + | |
- | + | ||
- | .fe | + | |
- | + | ||
- | £ | + | |
- | # End comment | + | |
- | </ | + | |
- | + | ||
- | Now use the **grep** | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | fenestrOS | + | f123123123.txt f123123.txt |
- | fenestros | + | |
- | 555-5555 | + | |
- | f | + | |
- | .fenestros | + | |
- | .fe | + | |
- | £ | + | |
</ | </ | ||
<WRAP center round important 60%> | <WRAP center round important 60%> | ||
- | **Important** | + | **Important**: Note here that the command only displays files with a name that does **not** contain |
</ | </ | ||
- | Now use the **egrep** command to do the same thing, this time redirecting the output to the file **/ | + | ====Protecting Metacharacters==== |
- | < | + | In order to use a metacharacter in a literal context, an escape character must be used. There are three escape characters: |
- | [root@redhat9 ~]# egrep -v ' | + | |
- | [root@redhat9 ~]# cat / | + | |
- | fenestrOS | + | |
- | fenestros | + | |
- | 555-5555 | + | |
- | f | + | |
- | .fenestros | + | |
- | .fe | + | |
- | £ | + | |
- | </ | + | |
- | <WRAP center round important 60%> | + | ^ Character ^ Description ^ |
- | **Important**: The above command is very useful when you want to quickly ascertain which directives are active in a very long configuration file. | + | | \ | Escapes the character which immediately follows | |
- | </ | + | | ' ' | Protects any character between the two **'** | |
+ | | " " | Protects any character between the two **"** except the following: **$**, **\** and **'** | | ||
- | Make the following changes to the greptest file: | + | To illustrate |
- | < | + | echo * is a metacharacter |
- | [root@redhat9 tmp]# vi greptest | + | |
- | + | ||
- | [root@redhat9 tmp]# cat greptest | + | |
- | # Starting comment | + | |
- | ^ This line will be used to demonstrate the use of fgrep | + | |
- | fenestrOS | + | |
- | fenestros | + | |
- | # Another comment | + | |
- | 555-5555 | + | |
- | f | + | |
- | + | ||
- | .fenestros | + | |
- | + | ||
- | .fe | + | |
- | + | ||
- | £ | + | |
- | # End comment | + | |
- | </ | + | |
- | + | ||
- | Now use fgrep to match the line starting with the **^** character: | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# fgrep ' | + | |
- | ^ This line will be used to demonstrate the use of fgrep | + | |
- | </ | + | |
- | Compare the above output to that when using the grep command: | + | When you enter this command |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | # Starting comment | + | a100 f f1 f123123123.txt f123123.txt f123123.txt f2 f3 f4 f5 f52 f62 f.txt is a metacharacter |
- | ^ This line will be used to demonstrate the use of fgrep | + | |
- | fenestrOS | + | |
- | fenestros | + | |
- | # Another comment | + | |
- | 555-5555 | + | |
- | f | + | |
- | .fenestros | + | [trainee@redhat9 training]$ echo \* is a metacharacter |
+ | * is a metacharacter | ||
- | .fe | + | [trainee@redhat9 training]$ echo "* is a metacharacter" |
+ | * is a metacharacter | ||
- | £ | + | [trainee@redhat9 training]$ echo ‘* is a metacharacter’ |
- | # End comment | + | * is a metacharacter |
</ | </ | ||
- | As you can see, grep matched **every** line that had a // | + | ====1.9 - Exit codes==== |
- | < | + | Each command returns an **exit status** when it is executed. |
- | [root@redhat9 ~]# grep ' | + | |
- | ^ This line will be used to demonstrate the use of fgrep | + | |
- | </ | + | |
- | ====The Stream EDitor SED==== | + | For example: |
- | + | ||
- | **sed** is an abbreviation of //Stream EDitor// and is a non-interactive text editor. sed's basic syntax is as follows: | + | |
- | + | ||
- | < | + | |
- | sed [address] command [arguments] file | + | |
- | </ | + | |
- | + | ||
- | The specified commands are applied to each line in the file unless an //address// is specified. Sed prints all results to standard output and does not modify the source file. The address therefore specifies which lines are concerned by the command. | + | |
- | + | ||
- | sed's addresses are as follows: | + | |
- | + | ||
- | ^address ^ Matching lines ^ | + | |
- | | x | Line number **x** | | + | |
- | | $ | The last line of the file | | + | |
- | | /BRE/ | Lines matching the specified BRE | | + | |
- | | x,y | From line **x** to line **y** | | + | |
- | | /ERb1/, /ERb2/ | All lines from the first line that matches the first BRE to the first line that matches the second BRE | | + | |
- | + | ||
- | sed's commands are as follows: | + | |
- | + | ||
- | ^command ^ Description ^ | + | |
- | | d | Do not show the matching line(s) on standard output | | + | |
- | | p | Show the matching line(s) on standard output | + | |
- | | s | Do a substitution upon match | | + | |
- | | w | Write the matching line(s) to a file | | + | |
- | | = | Print the matching line's number | | + | |
- | | ! | Exclude the line(s) matching the address | | + | |
- | + | ||
- | ===Command Line Switches=== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **fgrep** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ===LAB #5 - Using sed=== | + | |
- | + | ||
- | Start by displaying the contents of the file **/ | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | # are included, only the more common ones. | + | [trainee@redhat9 ~]$ mkdir codes |
- | # | + | [trainee@redhat9 ~]$ echo $? |
- | # The latest IANA port assignments can be gotten from | + | 0 |
- | # | + | [trainee@redhat9 ~]$ touch codes/exit.txt |
- | # The Well Known Ports are those from 0 through 1023. | + | [trainee@redhat9 ~]$ rmdir codes |
- | # The Registered Ports are those from 1024 through 49151 | + | rmdir: failed to remove ‘codes’: |
- | # The Dynamic and/or Private Ports are those from 49152 through 65535 | + | [trainee@redhat9 ~]$ echo $? |
- | # | + | 1 |
- | # Each line describes one service, and is of the form: | + | |
- | # | + | |
- | # service-name | + | |
- | + | ||
- | tcpmux | + | |
- | tcpmux | + | |
- | rje | + | |
- | rje | + | |
- | echo 7/tcp | + | |
- | echo 7/udp | + | |
- | discard | + | |
- | discard | + | |
- | systat | + | |
- | systat | + | |
- | daytime | + | |
- | --More-- | + | |
</ | </ | ||
- | Now display | + | In this example |
- | <code> | + | Deleting the directory encountered an error because **codes** contained the file **return**. The exit code stored in the $? variable is **one**. |
- | [root@redhat9 ~]# sed '/ | + | |
- | tcpmux | + | If the exit code is **zero**, the last command was executed without error. |
- | tcpmux | + | |
- | rje | + | |
- | rje | + | |
- | echo 7/tcp | + | |
- | echo 7/udp | + | |
- | discard | + | |
- | discard | + | |
- | systat | + | |
- | systat | + | |
- | daytime | + | |
- | daytime | + | |
- | qotd 17/ | + | |
- | qotd 17/ | + | |
- | msp | + | |
- | msp | + | |
- | chargen | + | |
- | chargen | + | |
- | ftp-data | + | |
- | ftp-data | + | |
- | ftp | + | |
- | ftp | + | |
- | --More-- | + | |
- | </code> | + | |
- | <WRAP center round important 60%> | + | If the exit code is **other than zero**, the last command was completed with an error. |
- | **Important**: | + | |
- | </ | + | |
- | Continue by trying to just display the first two lines of **/ | + | ====1.10 - Redirections==== |
- | < | + | Your dialogue with the Linux system uses input and output channels. The keyboard is called the **standard input channel** and the screen is called the **standard output channel**: |
- | [root@redhat9 ~]# sed ' | + | |
- | root:x: | + | |
- | root: | + | |
- | bin: | + | |
- | bin: | + | |
- | daemon: | + | |
- | adm: | + | |
- | lp: | + | |
- | ... | + | |
- | </ | + | |
- | <WRAP center round important 60%> | + | {{:free:stdin.png|}} |
- | **Important**: As you can see in the above output, the command used displays not only the first two lines but **also** the entire file. As a result the first two lines are displayed twice. | + | |
- | </ | + | |
- | To force sed to **only** display the lines you specify, use the **-n** switch: | + | In other words, when you type a command on the keyboard, you see the result of that command on the screen. |
- | < | + | Sometimes, however, it is useful to redirect the standard output channel to a file. In this way, the result of a command such as **free** can be stored in a file for future reference: |
- | [root@redhat9 ~]# sed -n '1,2p' / | + | |
- | root: | + | |
- | bin: | + | |
- | </ | + | |
- | Now you want to use sed to strip out the comments from **/ | + | {{:free: |
- | < | + | This effect is achieved |
- | [root@redhat9 ~]# sed -n '/ | + | |
- | + | ||
- | [root@redhat9 ~]# more / | + | |
- | + | ||
- | tcpmux | + | |
- | tcpmux | + | |
- | rje | + | |
- | rje | + | |
- | echo 7/tcp | + | |
- | echo 7/udp | + | |
- | discard | + | |
- | discard | + | |
- | systat | + | |
- | systat | + | |
- | daytime | + | |
- | daytime | + | |
- | qotd 17/ | + | |
- | qotd 17/ | + | |
- | msp | + | |
- | msp | + | |
- | chargen | + | |
- | chargen | + | |
- | ftp-data | + | |
- | ftp-data | + | |
- | ftp | + | |
- | ftp | + | |
- | --More--(0%) | + | |
- | </ | + | |
- | + | ||
- | <WRAP center round important 60%> | + | |
- | **Important**: | + | |
- | </ | + | |
- | + | ||
- | Finally, create a file containing | + | |
< | < | ||
- | [root@redhat9 ~]# echo " | + | [trainee@redhat9 ~]$ pwd |
- | [root@redhat9 ~]# cat /tmp/ | + | /home/trainee |
- | user1 user2 user3 | + | [trainee@redhat9 ~]$ cd training |
+ | [trainee@redhat9 training]$ free > file | ||
+ | [trainee@redhat9 training]$ | ||
+ | | ||
+ | Mem: | ||
+ | Swap: 5242876 | ||
</ | </ | ||
- | <WRAP center round important 60%> | + | If the target file does not exist, it is created and its contents will be the result |
- | **Important**: | + | |
- | </ | + | |
- | ====The Text Processor AWK==== | + | On the other hand if the file already exists, it will be overwritten |
- | + | ||
- | ===Presentation=== | + | |
- | + | ||
- | The **awk** command acts as a **filter** and uses the following syntax: | + | |
- | + | ||
- | awk [-F seperator] ' | + | |
- | + | ||
- | ===Field Separation=== | + | |
- | + | ||
- | A file or a text stream is treated by awk as a sequence of records. By default each line is a record. Awk analyzes each record, separating that record into fields and then storing the record and fields in variables: | + | |
- | + | ||
- | * $0 contains the record, | + | |
- | * $1 contains the first field, | + | |
- | * $2 contains the second field, | + | |
- | * e.t.c. | + | |
- | + | ||
- | Awk interprets a space as a separator between fields unless a different separator is specified with the **-F** option. | + | |
- | + | ||
- | Awk then checks | + | |
- | + | ||
- | For example, the following command takes the standard output of **ls -l** and prints fields 8, 3 and 4 to standard output: | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | + | [trainee@redhat9 training]$ cat file | |
- | 05:23rootroot | + | Thu Sep 26 12:49:11 PM CEST 2024 |
- | 05: | + | |
- | 05: | + | |
- | 05: | + | |
- | 12:05rootroot | + | |
</ | </ | ||
- | Since there is no condition, the action is applied | + | To add additional data to the same target file, a **double redirect** must be used: |
- | + | ||
- | To make the output easier to read, you can include spaces between each field: | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | + | [trainee@redhat9 training]$ free >> file | |
- | 05:23 root root | + | [trainee@redhat9 training]$ cat file |
- | 05:21 root root | + | Thu Sep 26 12:49:11 PM CEST 2024 |
- | 05:28 root root | + | total used free shared |
- | 05:29 root root | + | Mem: 7869560 |
- | 12:05 root root | + | Swap: |
</ | </ | ||
- | === Conditions === | + | This way, the output of the free command will be added to the end of your file after the information in the free command. |
- | ==A regular expression applied to a record== | + | < |
- | + | **Important**: | |
- | * Format: | + | |
- | * /regular expression/ {action} | + | |
- | * Exemple: | + | |
- | * /hello/ {print $0} | + | |
- | + | ||
- | ==A regular expression applied to a field== | + | |
- | + | ||
- | * Format: | + | |
- | * $n ~/regular expression/ {action} | + | |
- | * $n!~/ | + | |
- | * Examples: | + | |
- | * $1 ~/hello/ {print $0} | + | |
- | * $1!~/hello/ {print $0} | + | |
- | + | ||
- | ==Comparisons== | + | |
- | + | ||
- | * Format: | + | |
- | * $n operator criteria {action} | + | |
- | * Example: | + | |
- | * $1 > 20 {print $0} | + | |
- | + | ||
- | **Operators** | + | |
- | + | ||
- | ^ Operator ^ Condition ^ | + | |
- | | < | Less than | | + | |
- | | <= | Less than or equal to | | + | |
- | | == | Equal to | | + | |
- | | != | Different | | + | |
- | | > | Greater than | | + | |
- | | >= | Greater than or equal to | | + | |
- | + | ||
- | + | ||
- | ==Logical Operators== | + | |
- | + | ||
- | * Format: | + | |
- | * test1 logical operator test2 {action} | + | |
- | * Example: | + | |
- | * $1 ~/hello/ && $2 > 20 {print $0} | + | |
- | + | ||
- | **Operators** | + | |
- | + | ||
- | ^ Operator ^ Condition ^ | + | |
- | | %%||%% | OR | | + | |
- | | && | AND | | + | |
- | | ! | NO | | + | |
- | + | ||
- | ==Built-in Variables== | + | |
- | + | ||
- | * Format: | + | |
- | * expression1, | + | |
- | * Example: | + | |
- | * NR==7, NR==10 {print $0} | + | |
- | + | ||
- | **Variables** | + | |
- | + | ||
- | ^ Variable ^ Description ^ | + | |
- | | NR | Total number of records | | + | |
- | | NF | Total number of fields | | + | |
- | | FILENAME | Name of current input file | | + | |
- | | FS | The field separator, by default a **space** or **tab** | | + | |
- | | RS | The record separator, by default **newline** | | + | |
- | | OFS | Output field separator, by default a **space** | | + | |
- | | ORS | Output record separator, by default **newline** | | + | |
- | | OFMT | Numeric output format, by default" | + | |
- | + | ||
- | ===Awk Scripts=== | + | |
- | + | ||
- | To combine several //clauses// composed of // | + | |
- | + | ||
- | * **BEGIN** | + | |
- | * This section is executed once, prior to executing the body of the script | + | |
- | * **BODY** | + | |
- | * This sections contains the clauses to be applied to each line | + | |
- | * **END** | + | |
- | * This section is executed once, after executing the body of the script | + | |
- | + | ||
- | For example: | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 tmp]# cat > scriptawk | + | |
- | BEGIN { | + | |
- | print "List of mounted filesystems" | + | |
- | {print $0} | + | |
- | END { | + | |
- | print " | + | |
- | [^D] | + | |
- | </ | + | |
- | + | ||
- | Now apply the awk script to **/ | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 tmp]# awk -f scriptawk /etc/mtab | + | |
- | List of mounted filesystems | + | |
- | sysfs /sys sysfs rw, | + | |
- | proc /proc proc rw, | + | |
- | devtmpfs /dev devtmpfs rw, | + | |
- | securityfs / | + | |
- | tmpfs /dev/shm tmpfs rw, | + | |
- | devpts /dev/pts devpts rw, | + | |
- | tmpfs /run tmpfs rw, | + | |
- | tmpfs / | + | |
- | cgroup / | + | |
- | pstore / | + | |
- | bpf /sys/fs/bpf bpf rw, | + | |
- | cgroup / | + | |
- | cgroup / | + | |
- | cgroup / | + | |
- | cgroup / | + | |
- | cgroup / | + | |
- | cgroup / | + | |
- | cgroup / | + | |
- | cgroup / | + | |
- | cgroup / | + | |
- | cgroup / | + | |
- | cgroup / | + | |
- | none / | + | |
- | configfs / | + | |
- | /dev/sda3 / xfs rw, | + | |
- | selinuxfs / | + | |
- | systemd-1 / | + | |
- | debugfs / | + | |
- | hugetlbfs / | + | |
- | mqueue /dev/mqueue mqueue rw, | + | |
- | /dev/sda1 /boot ext4 rw, | + | |
- | sunrpc / | + | |
- | tmpfs / | + | |
- | binfmt_misc / | + | |
- | tracefs / | + | |
- | ===================================== | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | **Important**: | + | |
</ | </ | ||
- | ===The printf function=== | + | Input and output channels are numbered: |
- | The integrated function | + | * 0 = The Standard Input Channel |
+ | | ||
+ | | ||
- | < | + | The following command |
- | printf (" | + | |
- | </ | + | |
- | + | ||
- | **string** contains as many formats as there are expressions. | + | |
- | + | ||
- | Examples of formats commonly used are: | + | |
- | + | ||
- | ^ Format ^ Description ^ | + | |
- | | %30s | Displays a right-justified string of 30 characters | | + | |
- | | %-30s | Displays a left-justified string of 30 characters | | + | |
- | | %4d | Displays a right-justified decimal number of 4 digits | | + | |
- | | %-4d | Displays a left-justified decimal number of 4 digits | | + | |
- | + | ||
- | ===Control Statements=== | + | |
- | + | ||
- | awk can use the following | + | |
- | + | ||
- | ==if== | + | |
- | + | ||
- | < | + | |
- | if condition { | + | |
- | + | ||
- | | + | |
- | command | + | |
- | ... | + | |
- | } | + | |
- | + | ||
- | else { | + | |
- | + | ||
- | command | + | |
- | command | + | |
- | ... | + | |
- | } | + | |
- | + | ||
- | </ | + | |
- | + | ||
- | or: | + | |
- | + | ||
- | < | + | |
- | if condition | + | |
- | + | ||
- | command | + | |
- | + | ||
- | else | + | |
- | + | ||
- | command | + | |
- | + | ||
- | </ | + | |
- | + | ||
- | ==for== | + | |
- | + | ||
- | < | + | |
- | for variable in list { | + | |
- | + | ||
- | command | + | |
- | command | + | |
- | ... | + | |
- | + | ||
- | } | + | |
- | </ | + | |
- | + | ||
- | or: | + | |
- | + | ||
- | < | + | |
- | for variable in list | + | |
- | + | ||
- | command | + | |
- | + | ||
- | </ | + | |
- | + | ||
- | or in the case of a table: | + | |
- | + | ||
- | <file> | + | |
- | for key in table { | + | |
- | + | ||
- | print key , table[key] | + | |
- | + | ||
- | } | + | |
- | </ | + | |
- | + | ||
- | ==while== | + | |
- | + | ||
- | < | + | |
- | while condition { | + | |
- | + | ||
- | | + | |
- | | + | |
- | ... | + | |
- | + | ||
- | } | + | |
- | </ | + | |
- | + | ||
- | ==do-while== | + | |
- | + | ||
- | < | + | |
- | do { | + | |
- | + | ||
- | | + | |
- | | + | |
- | ... | + | |
- | + | ||
- | } while condition | + | |
- | </ | + | |
- | + | ||
- | ===Command Line Switches=== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option | + | |
- | </ | + | |
- | + | ||
- | ===LAB #6 - Using awk=== | + | |
- | + | ||
- | Create the file **sales.txt**: | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | [root@redhat9 | + | [trainee@redhat9 |
- | # Annual sales by French department | + | [trainee@redhat9 ~]$ cat errorlog |
- | # 83 | + | rmdir: failed to remove ‘training/ |
- | Desktops§100 | + | |
- | Portables§50 | + | |
- | Servers§21 | + | |
- | Ipads§4 | + | |
- | + | ||
- | # 06 | + | |
- | Desktops§99 | + | |
- | Portables§60 | + | |
- | Servers§8 | + | |
- | Ipads§16 | + | |
- | + | ||
- | # 13 | + | |
- | Desktops§130 | + | |
- | Portables§65 | + | |
- | Servers§12 | + | |
- | Ipads§56 | + | |
</ | </ | ||
- | Now create | + | In fact the error is generated because |
- | < | + | You can join file descriptors using the **& |
- | [root@redhat9 tmp]# vi sales.awk | + | |
- | + | ||
- | [root@redhat9 tmp]# cat sales.awk | + | |
- | # BEGIN | + | |
- | BEGIN { | + | |
- | FS=" | + | |
- | } | + | |
- | # TABLE | + | |
- | $1 !~ /^#/ && $1 !~ /^$/ { | + | |
- | sales[$1]+=$2 | + | |
- | } | + | |
- | # END | + | |
- | END { | + | |
- | for (pc in sales) | + | |
- | printf(" | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | This script contains 13 lines. The purpose of this script is to calculate the total number of computers sold in the three French departments from the data present in the **sales** file: | + | |
- | + | ||
- | < | + | |
- | 1 # BEGIN | + | |
- | | + | |
- | | + | |
- | 4 } | + | |
- | 5 # TABLE | + | |
- | 6 $1 !~ /^#/ && $1 !~ /^$/ { | + | |
- | | + | |
- | 8 } | + | |
- | 9 # END | + | |
- | 10 END { | + | |
- | 11 for (pc in sales) | + | |
- | 12 printf(" | + | |
- | 13 } | + | |
- | </ | + | |
- | + | ||
- | It is important that you understand the key lines in the above script: | + | |
- | + | ||
- | | + | |
- | * Defines a new field seperator in a BEGIN section. | + | |
- | * Line **6**, | + | |
- | * Discards all commented and empty lines. | + | |
- | * Line **7**, | + | |
- | * The table' | + | |
- | * Line **12**, | + | |
- | * Uses printf to format the output of each line in the table. | + | |
- | + | ||
- | Now execute the script and check the output is correct: | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | PC Type : Portables | + | |
- | PC Type : Ipads Sales (06+13+83) : 76 | + | |
- | PC Type : Desktops | + | |
- | PC Type : Servers | + | |
</ | </ | ||
- | ====LAB #7 - Other Useful Commands==== | + | The syntax **2>& |
- | ===7.1 - The expand Command=== | + | It is possible to modify the standard input channel |
- | + | ||
- | The **expand** command converts tabulations | + | |
- | + | ||
- | Create the following file: | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | + | 8 | |
- | [root@redhat9 tmp]# cat expand | + | |
- | un deux trois quatre cinq | + | |
- | un deux trois quatre cinq | + | |
- | un deux trois quatre cinq | + | |
- | un deux trois quatre cinq | + | |
- | un deux trois quatre cinq | + | |
- | un deux trois quatre cinq | + | |
- | un deux trois quatre cinq | + | |
- | un deux trois quatre cinq | + | |
- | un deux trois quatre cinq | + | |
- | un deux trois quatre cinq | + | |
</ | </ | ||
- | Now use the **-vet** switches of the **cat** | + | In this example |
- | < | + | Other redirections exist: |
- | [root@redhat9 tmp]# cat -vet expand | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | </ | + | |
- | <WRAP center round important 60%> | + | ^ Redirection |
- | **Important** : As you can see the tabulations are shown as **^I** and the end of each line as a **$**. | + | | %%&>%% | Join file descriptors 1 and 2. | |
- | </WRAP> | + | | %%<< |
+ | | %%<>%% | Allows the use of the same file as STDIN and STDOUT. | | ||
- | Now use the **expand** command to convert the tabulations into spaces and send the result to the **expand1** file: | + | ====1.11 - Pipes==== |
- | < | + | It is also possible to link commands using a **|** pipe. |
- | [root@redhat9 ~]# expand expand > expand1 | + | |
- | </ | + | |
- | View the resulting **expand1** file with the **cat** | + | In this case, the output channel of the command |
< | < | ||
- | [root@redhat9 ~]# cat -vet expand1 | + | [trainee@redhat9 ~]$ ls | wc -w |
- | un deux trois | + | 17 |
- | un deux trois | + | |
- | un deux trois | + | |
- | un deux trois | + | |
- | un deux trois | + | |
- | un deux trois | + | |
- | un deux trois | + | |
- | un deux trois | + | |
- | un deux trois | + | |
- | un deux trois | + | |
</ | </ | ||
- | <WRAP center round important 60%> | + | This command, run in your home directory, takes the output |
- | **Important** : As you can see, the tabulations have been changed into spaces. | + | |
- | </ | + | |
- | + | ||
- | ==Command Line Switches== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option | + | |
- | </ | + | |
- | + | ||
- | ===7.2 - The unexpand Command=== | + | |
- | + | ||
- | The **expand** command converts spaces in a file to tabulations | + | |
- | + | ||
- | Now use the **expand** command to convert | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 ~]# cat -vet expand1 | + | |
- | un deux trois | + | |
- | un deux trois | + | |
- | un deux trois | + | |
- | un deux trois | + | |
- | un deux trois | + | |
- | un deux trois | + | |
- | un deux trois | + | |
- | un deux trois | + | |
- | un deux trois | + | |
- | un deux trois | + | |
- | + | ||
- | [root@redhat9 ~]# unexpand -a expand1 > expand2 | + | |
- | + | ||
- | [root@redhat9 ~]# cat -vet expand2 | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | un^Ideux^Itrois^Iquatre^Icinq$ | + | |
- | </ | + | |
<WRAP center round important 60%> | <WRAP center round important 60%> | ||
- | **Important** : Note that the spaces have been replaced by tabulations. | + | **Important**: |
</ | </ | ||
- | ==Command Line Switches== | + | Remember that the standard output can only be redirected in one direction. In order to be able to redirect |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **unexpand** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ===7.3 - The cut Command=== | + | |
- | + | ||
- | The cut command splits each line of a file into columns starting with column 1. Each column contains | + | |
- | + | ||
- | Select the first 7 columns of the **/etc/passwd** file: | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | root:x: | + | Thu Sep 26 12:54:36 PM CEST 2024 |
- | bin:x:1 | + | [trainee@redhat9 ~]$ cat file1 |
- | daemon: | + | Thu Sep 26 12:54:36 PM CEST 2024 |
- | adm:x:3 | + | |
- | lp:x:4: | + | |
- | sync:x: | + | |
- | shutdow | + | |
- | halt:x: | + | |
- | mail:x: | + | |
- | operato | + | |
- | games:x | + | |
- | ftp:x:1 | + | |
- | nobody: | + | |
- | systemd | + | |
- | dbus:x: | + | |
- | polkitd | + | |
- | avahi:x | + | |
- | tss:x:5 | + | |
- | colord: | + | |
- | clevis: | + | |
- | rtkit:x | + | |
- | sssd:x: | + | |
- | geoclue | + | |
- | libstor | + | |
- | systemd | + | |
- | setroub | + | |
- | pipewir | + | |
- | flatpak | + | |
- | gdm:x:4 | + | |
- | cockpit | + | |
- | cockpit | + | |
- | gnome-i | + | |
- | sshd:x: | + | |
- | chrony: | + | |
- | dnsmasq | + | |
- | tcpdump | + | |
- | trainee | + | |
</ | </ | ||
- | In order to select columns 1 to 5, columns 10 to 15 and columns 30 and higher, us the following command: | + | This same technique allows us to create **two files**: |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | root: | + | [trainee@redhat9 |
- | bin: | + | Thu Sep 26 12:55:11 PM CEST 2024 |
- | daemo2: | + | [trainee@redhat9 |
- | adm: | + | Thu Sep 26 12:55:11 PM CEST 2024 |
- | lp: | + | |
- | sync: | + | |
- | shutdx: | + | |
- | halt: | + | |
- | mail: | + | |
- | operax: | + | |
- | games2: | + | |
- | ftp: | + | |
- | nobod65534: | + | |
- | systeoredumstemd Core Dumper:/:/ | + | |
- | dbus:: | + | |
- | polki: | + | |
- | avahi0: | + | |
- | tss: | + | |
- | color997: | + | |
- | clevi996: | + | |
- | rtkit72: | + | |
- | sssd: | + | |
- | geocl: | + | |
- | libstemgmt: | + | |
- | systeom:x:9 Userspace OOM Killer:/:/ | + | |
- | setroshoot: | + | |
- | pipewx: | + | |
- | flatp: | + | |
- | gdm: | + | |
- | cockps:x:98 cockpit web service:/ | + | |
- | cockpsinstaUser for cockpit-ws instances:/ | + | |
- | gnometial-s::/ | + | |
- | sshd:: | + | |
- | chron980: | + | |
- | dnsma: | + | |
- | tcpdu: | + | |
- | train: | + | |
- | </ | + | |
- | + | ||
- | In order to select the 2nd, 4th and 6th column, use the following command: | + | |
- | + | ||
- | <code> | + | |
- | [root@redhat9 | + | |
- | x:0:/root | + | |
- | x:1:/bin | + | |
- | x:2:/sbin | + | |
- | x: | + | |
- | x: | + | |
- | x:0:/sbin | + | |
- | x:0:/sbin | + | |
- | x:0:/sbin | + | |
- | x:12:/ | + | |
- | x:0:/root | + | |
- | x: | + | |
- | x: | + | |
- | x:65534:/ | + | |
- | x:997:/ | + | |
- | x:81:/ | + | |
- | x:996:/ | + | |
- | x: | + | |
- | x: | + | |
- | x: | + | |
- | x: | + | |
- | x: | + | |
- | x:991:/ | + | |
- | x: | + | |
- | x:988:/ | + | |
- | x:987:/ | + | |
- | x: | + | |
- | x: | + | |
- | x:983:/ | + | |
- | x: | + | |
- | x: | + | |
- | x: | + | |
- | x: | + | |
- | x: | + | |
- | x: | + | |
- | x: | + | |
- | x:72:/ | + | |
- | x: | + | |
- | </ | + | |
- | + | ||
- | ==Command Line Switches== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **cut** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ===7.4 - The uniq Command=== | + | |
- | + | ||
- | The following command is used to extract the Primary Group GIDs from the **/ | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 | + | |
- | 0 | + | |
- | 1 | + | |
- | 2 | + | |
- | 4 | + | |
- | 7 | + | |
- | 12 | + | |
- | 42 | + | |
- | 50 | + | |
- | 59 | + | |
- | 70 | + | |
- | 72 | + | |
- | 74 | + | |
- | 81 | + | |
- | 100 | + | |
- | 172 | + | |
- | 978 | + | |
- | 979 | + | |
- | 980 | + | |
- | 981 | + | |
- | 982 | + | |
- | 983 | + | |
- | 984 | + | |
- | 986 | + | |
- | 987 | + | |
- | 988 | + | |
- | 990 | + | |
- | 991 | + | |
- | 992 | + | |
- | 993 | + | |
- | 996 | + | |
- | 997 | + | |
- | 1000 | + | |
- | 65534 | + | |
</ | </ | ||
<WRAP center round important 60%> | <WRAP center round important 60%> | ||
- | **Important**: | + | **Important**: |
</ | </ | ||
- | ==Command | + | ====1.12 - Command |
- | <WRAP center round todo 60%> | + | It is sometimes interesting, |
- | **To do** : Use the **--help** option of the **uniq** command | + | |
- | </ | + | |
- | + | ||
- | ===7.5 - The tr Command=== | + | |
- | + | ||
- | The **tr** command is used to substitute certain characters by other characters. This command **only** accepts data from standard input (hence | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | ROOT: | + | date |
- | BIN: | + | |
- | DAEMON: | + | |
- | ADM: | + | |
- | LP: | + | |
- | SYNC: | + | |
- | SHUTDOWN: | + | |
- | HALT: | + | |
- | MAIL: | + | |
- | OPERATOR: | + | |
- | GAMES: | + | |
- | FTP: | + | |
- | NOBODY: | + | |
- | SYSTEMD-COREDUMP: | + | |
- | DBUS: | + | |
- | POLKITD: | + | |
- | AVAHI: | + | |
- | TSS: | + | |
- | COLORD: | + | |
- | CLEVIS: | + | |
- | RTKIT: | + | |
- | SSSD: | + | |
- | GEOCLUE: | + | |
- | LIBSTORAGEMGMT: | + | |
- | SYSTEMD-OOM: | + | |
- | SETROUBLESHOOT: | + | |
- | PIPEWIRE: | + | |
- | FLATPAK: | + | |
- | GDM: | + | |
- | COCKPIT-WS: | + | |
- | COCKPIT-WSINSTANCE: | + | |
- | GNOME-INITIAL-SETUP: | + | |
- | SSHD: | + | |
- | CHRONY: | + | |
- | DNSMASQ: | + | |
- | TCPDUMP: | + | |
- | TRAINEE: | + | |
- | </ | + | |
- | ==Command Line Switches== | + | [trainee@redhat9 |
- | + | Thu Sep 26 12:56:02 PM CEST 2024 | |
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **tr** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ===7.6 - The paste Command=== | + | |
- | + | ||
- | The **paste** command concatenates lines from n files. For example: | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 | + | |
- | root: | + | |
- | bin: | + | |
- | daemon: | + | |
- | adm: | + | |
- | lp: | + | |
- | sync: | + | |
- | shutdown: | + | |
- | halt: | + | |
- | mail:x:8:12:mail:/ | + | |
- | operator: | + | |
- | games: | + | |
- | ftp: | + | |
- | nobody: | + | |
- | systemd-coredump: | + | |
- | dbus: | + | |
- | polkitd: | + | |
- | avahi: | + | |
- | tss: | + | |
- | colord: | + | |
- | clevis: | + | |
- | rtkit: | + | |
- | sssd: | + | |
- | geoclue: | + | |
- | libstoragemgmt: | + | |
- | systemd-oom: | + | |
- | setroubleshoot: | + | |
- | pipewire: | + | |
- | flatpak: | + | |
- | gdm: | + | |
- | cockpit-ws: | + | |
- | cockpit-wsinstance: | + | |
- | gnome-initial-setup: | + | |
- | sshd: | + | |
- | chrony: | + | |
- | dnsmasq: | + | |
- | tcpdump: | + | |
- | trainee: | + | |
- | </ | + | |
- | + | ||
- | ==Command Line Switches== | + | |
- | + | ||
- | <WRAP center round todo 60%> | + | |
- | **To do** : Use the **--help** option of the **paste** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ===7.7 - The split Command=== | + | |
- | + | ||
- | The split command is used to divide a large file into smaller segments. Create an empty 250 MB file as follows: | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 tmp]# dd if=/ | + | |
- | 250+0 records in | + | |
- | 250+0 records out | + | |
- | 262144000 bytes (262 MB, 250 MiB) copied, 0.132123 s, 2.0 GB/s | + | |
- | </ | + | |
- | + | ||
- | Now use the split command to divide the file into 5 smaller files each of 50: | + | |
- | + | ||
- | < | + | |
- | [root@redhat9 tmp]# split -b 50m /file filepart | + | |
- | [root@redhat9 | + | [trainee@redhat9 |
- | -rw-r--r--. 1 root root 52428800 | + | Thu Sep 26 12:56:17 PM CEST 2024 |
- | -rw-r--r--. 1 root root 52428800 Sep 25 16:17 filepartab | + | |
- | -rw-r--r--. 1 root root 52428800 Sep 25 16:17 filepartac | + | |
- | -rw-r--r--. 1 root root 52428800 Sep 25 16:17 filepartad | + | |
- | -rw-r--r--. 1 root root 52428800 Sep 25 16:17 filepartae | + | |
</ | </ | ||
<WRAP center round important 60%> | <WRAP center round important 60%> | ||
- | **Important**: | + | **Important**: |
</ | </ | ||
- | You can re-construct the original file by using the **cat** command: | + | ====1.13 |
- | < | + | It is possible to group commands together using a sub-shell : |
- | [root@redhat9 tmp]# cat fileparta* > newfile | + | |
- | [root@redhat9 tmp]# ls -l | grep newf | + | |
- | -rw-r--r--. 1 root root 262144000 Sep 25 16:18 newfile | + | |
- | [root@redhat9 tmp]# ls -l / | grep file | + | |
- | -rw-r--r--. | + | |
- | </ | + | |
- | ==Command Line Switches== | + | $ (ls -l; ps; who) > list [Enter] |
- | <WRAP center round todo 60%> | + | This example sends the results |
- | **To do** : Use the **--help** option | + | |
- | </ | + | |
- | ===7.8 - The diff Command=== | + | The commands can also be chained according to the exit code of the previous command. |
- | The diff command compares two fles and indicates what changes need to be made to the first file in order that it be identical to the second | + | **&& |
- | Copy the **/etc/passwd** file to the **/root** directory: | + | **||** is used to ensure |
- | < | + | The syntax of this command is : |
- | [root@redhat9 ~]# cp /etc/passwd /root | + | |
- | </ | + | |
- | Edit the ***/ | + | Command1 && Command2 |
- | < | + | In this case, Command2 is only executed if Command1 has run without error. |
- | ... | + | |
- | trainee10: | + | |
- | ... | + | |
- | </ | + | |
- | Delete the **tcpdump** entry and add the following line to the end of **/ | + | Or : |
- | < | + | Command1 || Command2 |
- | ... | + | |
- | Linux est super! | + | |
- | </ | + | |
- | Now compare the two files: | + | In this case, Command2 is executed if Command1 has encountered an error. |
- | < | + | ====1.14 |
- | [root@redhat9 tmp]# diff /etc/passwd / | + | |
- | 26,27c26 | + | |
- | < tcpdump: | + | |
- | < trainee: | + | |
- | --- | + | |
- | > trainee10: | + | |
- | 36a36 | + | |
- | > Linux est super! | + | |
- | </ | + | |
- | In this output you will notice | + | A shell variable can be displayed using the command : |
- | The output **26, | + | $ echo $VARIABLE [Input] |
- | The output **36a36** means that at line 36 in /etc/passwd line 36 from / | + | === Main variables === |
- | ==Command | + | ^ Variable ^ Description ^ |
+ | | BASH | Complete path to current shell. | | ||
+ | | BASH_VERSION |Shell version. | | ||
+ | | EUID | EUID of the current user. | | ||
+ | | UID | UID of the current user. | | ||
+ | | PPID | PID of the parent of the current process. | | ||
+ | | PWD | The current directory. | | ||
+ | | OLDPWD | The previous current directory ( like the **cd -**command ).| | ||
+ | | RANDOM | A random number between 0 and 32767. | | ||
+ | | SECONDS | The numbers of seconds since the shell was started.| | ||
+ | | LINES | The number of lines in a screen. | | ||
+ | | COLUMNS | The number of columns in a screen . | | ||
+ | | HISTFILE | The history file. | | ||
+ | | HISTFILESIZE | The history file size. | | ||
+ | | HISTSIZE | The number of commands that can be saved to the history file. | | ||
+ | | HISTCMD | The current command' | ||
+ | | HISTCONTROL | **ignorespace** or **ignoredups** or **ignoreboth** | | ||
+ | | HOME | The user's home directory. | | ||
+ | | HOSTTYPE | Machine type. | | ||
+ | | OSTYPE | The OS type. | | ||
+ | | MAIL | The file containing the user's mail. | | ||
+ | | MAILCHECK | Frequency in seconds that a user's mail is checked. | | ||
+ | | PATH | The paths to executables. | | ||
+ | | PROMPT_COMMAND | Command | ||
+ | | PS1 | User's default prompt. | | ||
+ | | PS2 | User's 2nd level default prompt. | | ||
+ | | PS3 | User's 3rd level prompt. | | ||
+ | | PS4 | User's 4th level prompt. | | ||
+ | | SHELL | User's current shell. | | ||
+ | | SHLVL | The number of shell instances. | | ||
+ | | TMOUT | The number of seconds less 60 before an unused terminal gets sent the **exit** command. | | ||
- | <WRAP center round todo 60%> | + | === Internationalization and Localization Variables === |
- | **To do** : Use the **--help** option of the **diff** command to view the command line switches. | + | |
- | </ | + | |
- | ===7.9 - The cmp Command=== | + | **Internationalization**, |
- | The **cmp** command compares two files character by character. By default, the command stops after finding the first difference: | + | |
+ | | ||
+ | | ||
+ | | ||
+ | * Weights and measures, | ||
+ | * Date/time format, | ||
+ | * Paper sizes, | ||
+ | * Keyboard layout, | ||
+ | * etc ... | ||
- | < | + | The **Localization**, |
- | [root@redhat9 tmp]# cmp /root/passwd | + | |
- | / | + | |
- | </ | + | |
- | The **-l** switch shows all of the differences in a three column format: | + | The complete country code takes the following form: **language-PAYS.character_set**. For example, for the English language the language-PAYS values are : |
- | < | + | * en_GB = Great Britain, |
- | [root@redhat9 tmp]# cmp -l / | + | |
- | cmp: EOF on / | + | |
- | 1300 162 143 | + | |
- | 1301 141 160 | + | |
- | 1302 151 144 | + | |
- | 1303 156 165 | + | |
- | 1304 145 155 | + | |
- | 1305 145 160 | + | |
- | 1306 61 72 | + | |
- | 1307 60 170 | + | |
- | 1309 170 67 | + | |
- | 1310 72 62 | + | |
- | 1311 61 72 | + | |
- | 1312 60 67 | + | |
- | 1313 60 62 | + | |
- | 1314 60 72 | + | |
- | 1316 61 57 | + | |
- | 1317 60 72 | + | |
- | 1318 60 57 | + | |
- | 1319 60 163 | + | |
- | 1320 72 142 | + | |
- | 1321 164 151 | + | |
- | 1322 162 156 | + | |
- | 1323 141 57 | + | |
- | 1324 151 156 | + | |
- | --More-- | + | |
- | </ | + | |
- | The first column represents the character **number**, the second column represents the **ASCII octal value** of the character in the **/ | + | The most important system variables containing regionalisation information are : |
- | ==Command Line Switches== | + | ^ Variable ^ Description ^ |
+ | | LC_ALL | With a non-zero value, this takes precedence over the value of all other internationalisation variables. | | ||
+ | | LANG | Provides a default value for environment variables whose value is null or undefined. | | ||
+ | | LC_CTYPE | Determines the regional parameters for interpreting the sequence of bytes of text data in characters. | | ||
- | <WRAP center round todo 60%> | + | For example: |
- | **To do** : Use the **--help** option of the **cmp** command to view the command line switches. | + | |
- | </ | + | |
- | + | ||
- | ===7.10 - The patch Command=== | + | |
- | + | ||
- | La commande **patch** est utilisée pour appliquer des modifications à un fichier à partir d'un fichier patch qui contient les The **patch** command is used to apply modifications contained within a patch file to an older version of a file so that it becomes the newer version of the file. | + | |
- | + | ||
- | The **patch** command is not installed by default in RHEL 9: | + | |
< | < | ||
- | [root@redhat9 ~]# dnf install patch -y | + | [trainee@redhat9 ~]$ echo $LC_ALL |
- | </ | + | |
- | If you recall, you made some changes to the / | + | [trainee@redhat9 ~]$ echo $LC_CTYPE |
- | < | + | [trainee@redhat9 |
- | [root@redhat9 | + | en_US.UTF-8 |
- | # Starting comment | + | [trainee@redhat9 ~]$ locale |
- | ^ This line will be used to demonstrate the use of fgrep | + | LANG=en_US.UTF-8 |
- | fenestrOS | + | LC_CTYPE=" |
- | fenestros | + | LC_NUMERIC=" |
- | # Another comment | + | LC_TIME=" |
- | 555-5555 | + | LC_COLLATE=" |
- | f | + | LC_MONETARY=" |
- | + | LC_MESSAGES=" | |
- | .fenestros | + | LC_PAPER=" |
- | + | LC_NAME=" | |
- | .fe | + | LC_ADDRESS=" |
- | + | LC_TELEPHONE=" | |
- | £ | + | LC_MEASUREMENT=" |
- | # End comment | + | LC_IDENTIFICATION=" |
- | + | LC_ALL= | |
- | [root@redhat9 tmp]# cat / | + | |
- | fenestrOS | + | |
- | fenestros | + | |
- | 555-5555 | + | |
- | f | + | |
- | .fenestros | + | |
- | .fe | + | |
- | £ | + | |
</ | </ | ||
- | Now create a patch file containing the modifications that need to be applied to / | + | ===Special variables=== |
- | < | + | ^ Variable ^ Description ^ |
- | [root@redhat9 tmp]# diff -u greptest greptest1 > greptest.patch | + | | $LINENO | Contains the current line number of the script or function being executed | |
- | </ | + | | $$ | Contains the PID of the current process | |
+ | | $PPID | Contains the PID of the parent of the current process | | ||
+ | | $0 | Contains the name of the current script | | ||
+ | | $1, $2 ... | Contains respectively the 1st, 2nd etc arguments passed to the script | | ||
+ | | $# | Contains the total number of arguments passed to the script | | ||
+ | | $* | Contains all of the arguments passed to the script | | ||
+ | | $@ | Contains all of the arguments passed to the script | | ||
- | A look at the patch file shows the chnages that need to be made to the **greptest** file: | + | ====1.15 - The env command ==== |
- | < | + | The **env** command sends the values |
- | [root@redhat9 tmp]# cat greptest.patch | + | |
- | --- greptest 2021-04-20 05: | + | |
- | +++ greptest1 2021-04-20 05: | + | |
- | @@ -1,14 +1,7 @@ | + | |
- | -# Starting comment | + | |
- | -^ This line will be used to demonstrate | + | |
- | | + | |
- | | + | |
- | -# Another comment | + | |
- | | + | |
- | f | + | |
- | - | + | |
- | | + | |
- | - | + | |
- | .fe | + | |
- | - | + | |
- | £ | + | |
- | -# End comment | + | |
- | </ | + | |
- | + | ||
- | Now apply the patch file: | + | |
< | < | ||
- | [root@redhat9 | + | [trainee@redhat9 |
- | patching file greptest | + | SHELL=/ |
+ | HISTCONTROL=ignoredups | ||
+ | HISTSIZE=1000 | ||
+ | HOSTNAME=redhat9.ittraining.loc | ||
+ | PWD=/ | ||
+ | LOGNAME=trainee | ||
+ | XDG_SESSION_TYPE=tty | ||
+ | MOTD_SHOWN=pam | ||
+ | HOME=/ | ||
+ | LANG=en_US.UTF-8 | ||
+ | LS_COLORS=rs=0: |