Labels: Bash basics and standard I/O pipeline shell programming Basics
1. Bash basics and configurations
From the user's perspective, the shell type:
Logon Shell
Log On Through a terminal normally
Su-Username
Su-l Username
Non-Logon Shell
Su Username
Virtual terminal opened under the graphic Terminal
Automatically executed shell script
Bash configuration file:
Global Configuration:
/Etc/profile,/etc/profile. d/*. Sh,/etc/bashrc
Personal Configuration:
~ /. Bash_profile ,~ /. Bashrc
Profile files
1. Set Environment Variables
2. run commands or scripts
Bashrc files
1. Define local variables
2. Define command alias
How to read the configuration file using a logon shell:
/Etc/profile ---->/etc/profile. d/*. Sh ----> ~ /. Bash_profile ---->/etc/bashrc
How does a non-Logon shell read the configuration file:
~ /. Bashrc --->/etc/bashrc ---->/etc/profile. d/*. Sh
Completely switch between Su-username and Su-l Username
Semi-switching: su Username
The smaller the scope, the more effective it will be.
How to configure a permanent alias command? Personal? Global?
How to configure a permanent global prompt? Personal? Global?
2. Pipelines and IO redirection
Host, controller ----> CPU
Memory: Ram
Input/output devices
Program: Command + Data
Controller: Command
Timer:
Memory:
Address Bus: memory addressing
Data Bus: Data Transmission
Control Bus: control commands
Actually, it is a bus with three functions
Register: CPU temporary memory
I/O: hard disk,
Program:
Input device:
Output device:
System settings:
Default output device: standard output, stdout, 1
Default Input Device: standard input, stdin, 0
Default error output: standard error, stderr, 2
Standard Input Device: keyboard
Standard output and error output: Display (cheap)
I/O redirection:
Linux
Output redirection (greater than the number):> overwrite the output
> Append output
Set-C prohibits overwrite redirection for existing files
To forcibly overwrite the output, use> |
Set + C disable the above functions
2> redirect error output
2> append Mode
&>: Redirects standard output or error output to the same file, overwriting
Input redirection (minor sign): <
<Here document generates the document Terminator: end, EOF
Pipeline: Use the output of the previous command as the input of the next command.
Command 1 | command 2 | command 3 | ....
Exercise:
1. count the number of files in the/usr/bin/directory;
Ls/usr/bin/| WC-l
2. Retrieve the shells of all users on the current system. Each shell is only displayed once and displayed in order;
Cut-D:-F 7/etc/passwd | sort-u
3. Thinking: how to display the content types of each file in the/var/log directory?
4. Extract the 6th rows of the/etc/inittab file;
5. Retrieve the username and shell of the last 9th users in the/etc/passwd file, display them on the screen, and save them to the/tmp/users file;
Cut-D:-F 1,7/etc/passwd | head-9 | tail-1 | tee/tmp/users
6. display all files starting with PA in the/etc directory and count the number of files;
7. If you do not use a text editor, add a line of Alias CLS = clear content to the. bashrc file of the current user;
Regular Expression:
Text Search needs:
Grep, egrep, fgrep
Grep: searches for text based on the mode, and displays the line of conforming text to the screen.
Pattern: A match condition composed of text characters and metacharacters of Regular Expressions
Grep [Options] pattern [file...]
-I (Case Insensitive)
-- Color [matching character display color]
-V: displays the rows not matched by the pattern.
-O only displays the strings matched by the pattern
*: Any character of any length
? : Any single character
[]:
[^]
Regular Expression: Regular Expression, Regexp
Metacharacters:
.: Match any single character
[]: Match a single character in a specified range
[^]: Match a single character out of the specified range
Character Set combination: [: digit:], [: lower:], [: Upper:], [: punct:], [: Space:], [: Alpha:], [: alnum:]
Matching times: (Greedy mode, matching as long as possible)
*: Match any character before it.
A, B, AB, AAB, ACB, ADB, amnb
A * B (indicating any times a, followed by B): AB, AAB
A. * B only needs to start with a and end with B.
A \? B:
. *: Any character of any length
\? : Match the first character once or 0
\ {M, N \}: match the first character at least m times, at most N times
\ {1, \}: at least once
\ {0, 3 \}: up to 3 times
Positioning:
^: Specifies the beginning of a line. Any content after this character must appear at the beginning of the line.
$: Pin the end of a line: any content after this character must appear at the end of a line
^ $: Blank line;
The start of the anchor word \ <or \ B: Any character after it must be used as the header of the word. For example, Root \ B or Root \ <
The anchor at the end of a word \> or \ B: Any character before it must be used as the end of the word to show eg: \ broot or \> root
Word: starts with a letter and ends with no special characters in the middle. r55t
GROUP:
\(\)
\ (AB \)*
Backward reference
\ 1: reference the first left brace and all content contained in the corresponding right Brace
\ 2:
\ 3:
Exercise:
1. display the lines starting with "S" of the/proc/meminfo file with no size difference;
Grep-I '^ s'/proc/meminfo
Grep '^ [ss]'/proc/meminfo
2. display the rows ending with nologin in/etc/passwd;
Grep 'nologin $ '/etc/passwd
Retrieve the list of users whose default shell is/sbin/nologin.
Grep "nologin $ '/etc/passwd | cut-D:-F1
Obtain the username of the user whose default shell is Bash and whose user ID is the smallest
Grep 'bash $ '/etc/passwd | sort-n-t:-K3 | head-1 | cut-D:-F1
3. the/etc/inittab starts with #, followed by one or more blank characters, and then followed by any non-blank lines;
Grep "^ # [[: Space:] \ {1, \} [^ [: Space:]"/etc/inittab
4. the/etc/inittab displays the rows of a number (that is, a number between two colons;
Grep ': [0-9]:'/etc/inittab
5. display the lines starting with one or more blank characters in the/boot/GRUB/grub. conf file;
Grep '^ [[: Space:] \ {1, \}'/boot/GRUB/grub. conf
6. display the rows starting with a number in the/etc/inittab file and ending with the same number as the starting number;
Grep '^ \ ([0-9] \). * \ 1 $'/etc/inittab
Exercise:
1. Find one or two digits in a file;
Grep '[0-9] \ {1, 2 \}'/proc/cpuinfo
Grep -- color '\ <[0-9] \ {1, 2 \} \>'/proc/cpuinfo
2. Locate the integer between 1-In the ifconfig command result;
3. query the account information of the user whose name is student (the row must appear first) on the current system. The file is/etc/passwd.
Grep '^ student \>'/etc/passwd | cut-D:-F3
ID-u student
Student1
Student2
Exercise: analyze the features of the first two rows in the following text in the/etc/inittab file (the numbers in each row must be the same). Write a pattern similar to the two rows:
L1: 1: Wait:/etc/rc. d/RC 1
L3: 3: Wait:/etc/rc. d/RC 3
Grep '^ L \ ([0-9] \): \ 1. * \ 1 $'/etc/inittab
4. Bash basics and configurations, standard I/O, pipelines, and shell programming basics;