Linux Shell script introduction Reading Notes Chapter 1 test and test
I. Introduction
1. Bash (Bourne Again Shell), which is currently the default shell environment for most GNU/Linux systems.
All commands are input and executed in the shell terminal. After the terminal is opened, the prompt format is username @ hostname $ or root @ hostname # ($ indicates a common user, # indicates the administrator user root)
2. The shell script is #! (Shebang) the starting text file is as follows :#! /Bin/bash
Shebang is a text line, where #! Before the interpreter path. /Bin/bash is the path of the Bash interpreter command.
3. Two script running methods.
One is to use the script as a bash-to-command line parameter.
buxizhizhou@ubuntu:~/lssc/chpt1$ bash test_script.sh hello,world
The other is to grant the script execution permission to change it to an executable file. Chmod a + x script. sh
buxizhizhou@ubuntu:~/lssc/chpt1$ chmod a+x test_script.sh buxizhizhou@ubuntu:~/lssc/chpt1$ ./test_script.sh hello,world
The kernel reads the first line of the script and notices that shebang is #! /Bin/bash. It identifies/bin/bash and runs the script like this internally: $/bin/bash test_script.sh
The content of the test_script.sh file is as follows:
#!/bin/bashecho hello,world
. When shell is started, it executes a set of commands at the beginning to define settings such as prompt text and color. This set of commands are in the script file ~ /. In bashrc, for shell login, It is ~ /. Bash_profile. Bash also maintains a history file ~ /. Bash_history: Save the commands run by the user.
4 .~ Indicates the main directory, usually/home/user, where user is the user name; if it is a root user, it is/root.
5. login shell is the shell obtained after login to the host. If a shell is opened after logging on to the graphic interface environment such as GNOME and KDE, it is not a logon shell.
6. In Bash, each command or command sequence is separated by a semicolon or line break. # Specify the start of the comment and extend it to the end of the line. For example, $ cmd1; cmd2 is equivalent to $ cmd1 $ cmd2.
Ii. Terminal Printing
A terminal is an interactive tool that can interact with the shell environment.
1. echo. A line break is added after each call by default. -N ignore the end line break.
2. echo printing in three forms: Direct Printing, single quotation marks, and double quotation marks
buxizhizhou@ubuntu:~/lssc/chpt1$ echo "Welcome to Bash"Welcome to Bashbuxizhizhou@ubuntu:~/lssc/chpt1$ echo Welcome to BashWelcome to Bashbuxizhizhou@ubuntu:~/lssc/chpt1$ echo 'Welcome to Bash'Welcome to Bash
Side effects of the three methods:
Print directly: The semicolon in the text cannot be displayed. The semicolon is the command delimiter in Bash. Echo hello; hello is considered to be two commands echo hello and hello.
Single quotes: variable replacement is invalid in single quotes. Variables are not replaced in single quotes.
buxizhizhou@ubuntu:~/lssc/chpt1$ echo $var and '$var' and "$var"value and $var and value
Double quotation marks: the exclamation point cannot be printed !. Or escape it ,\!. To be exact, the exclamation mark cannot be printed normally if there is no space at the end or after the exclamation mark. As shown in the test below, we can see that the exclamation point can still be printed with spaces.
buxizhizhou@ubuntu:~/lssc/chpt1$ echo "Hello world !"bash: !": event not foundbuxizhizhou@ubuntu:~/lssc/chpt1$ echo "cannot include exclamation - ! within"cannot include exclamation - ! withinbuxizhizhou@ubuntu:~/lssc/chpt1$ echo "c!c"bash: !c": event not foundbuxizhizhou@ubuntu:~/lssc/chpt1$ echo "c ! c"c ! cbuxizhizhou@ubuntu:~/lssc/chpt1$ echo "c! c"c! cbuxizhizhou@ubuntu:~/lssc/chpt1$ echo "c !c"bash: !c": event not found
3. printf. There is no automatic line break. Parameters are similar to the C language. parameters are separated by spaces. As follows:
#! /Bin/bash # file name: printf. shprintf "%-5 s %-10 s %-4s \ n" No Name Markprintf "%-5 s %-10 s %-4.2f \ n" 1 Sarath 80.3456 printf "% -5 s %-10 s %-4.2f \ n "2 James 90.9989 printf" %-5 s %-10 s %-4.2f \ n "3 Jeff 77.564
After running:
buxizhizhou@ubuntu:~/lssc/chpt1$ bash printf.sh No Name Mark1 Sarath 80.352 James 91.003 Jeff 77.56
4. Use echo and printf. The options should be prior to the string. Otherwise, the option is treated as another string by Bash.
Echo-e "contains escape sequences to strings"
<pre name="code" class="plain">buxizhizhou@ubuntu:~/lssc/chpt1$ echo -e "1\t2\t3"123buxizhizhou@ubuntu:~/lssc/chpt1$ echo -e "!"bash: !: event not foundbuxizhizhou@ubuntu:~/lssc/chpt1$ echo -e "\!"\!buxizhizhou@ubuntu:~/lssc/chpt1$ echo "1\t2\t2"1\t2\t2buxizhizhou@ubuntu:~/lssc/chpt1$ echo "\!"\!
Color output
buxizhizhou@ubuntu:~/lssc/chpt1$ echo -e "\e[1;31m This is red text \e[0m" <span style="color:#ff0000;">This is red text</span>
Set \ e [1; 31 to red, and \ e [0 m to reset the color. If you replace 31, you can set other colors, black = 40, Red = 41, Green = 42, yellow = 43, Blue = 44, Magenta = 45, cyan = 46, and white = 47.
Iii. Variables and Environment Variables
Generally, the script language does not need to declare its type before using the variable. You can assign a value directly. In Bash, the value of each variable is a string. Some special variables are used by shell environments and operating systems to store some special values and become environment variables.
1. Run the env command to view the environment variables related to the terminal.
The environment variables returned by the preceding command are given in the form of name = value, which are separated by null characters (\ 0.
$ Pgrep process name obtains the process ID of the program.
Replace the tr' \ 0'' \ n' command to replace the null character with a line break.
izhou@ubuntu:~/lssc/chpt1$ pgrep openvpn1098buxizhizhou@ubuntu:~/lssc/chpt1$ sudo cat /proc/1098/environUPSTART_INSTANCE=runlevel=2UPSTART_JOB=rcTERM=linuxPATH=/sbin:/usr/sbin:/bin:/usr/binRUNLEVEL=2PREVLEVEL=NUPSTART_EVENTS=runlevelPWD=/previous=NVERBOSE=nobuxizhizhou@ubuntu:~/lssc/chpt1$ sudo cat /proc/1098/environ | tr '\0' '\n'UPSTART_INSTANCE=runlevel=2UPSTART_JOB=rcTERM=linuxPATH=/sbin:/usr/sbin:/bin:/usr/binRUNLEVEL=2PREVLEVEL=NUPSTART_EVENTS=runlevelPWD=/previous=NVERBOSE=no
2. variable value assignment var = value
Var is the variable name and value is the value to be assigned. If the value does not contain any blank characters, quotation marks are not required; otherwise, single or double quotation marks must be used.
Var = value is an equal operation.
Use $ variable name or $ {variable name} to obtain the variable value.
buxizhizhou@ubuntu:~/lssc/chpt1$ var=valuebuxizhizhou@ubuntu:~/lssc/chpt1$ echo $varvaluebuxizhizhou@ubuntu:~/lssc/chpt1$ echo ${var}valuebuxizhizhou@ubuntu:~/lssc/chpt1$ var1= valueNo command 'value' found, did you mean: Command 'pvalue' from package 'radiance' (universe)value: command not foundbuxizhizhou@ubuntu:~/lssc/chpt1$ var1 =valuevar1: command not foundbuxizhizhou@ubuntu:~/lssc/chpt1$ var1 = valuevar1: command not found
3. Set environment variables for export. This variable is inherited from any application executed by the current shell script.
Standard environment variables, such as PATH. After the command to be executed is given, shell will automatically find the corresponding executable file in the directory contained in the path environment variable. (Directory paths are separated by colons) paths are usually defined in/etc/environment or/etc/profile or ~ /. Bashrc. To add a new PATH to the PATH, use
Export PATH = "$ PATH:/home/user/bin" or $ PATH = "$ PATH:/home/user/bin", $ export PATH.
Other common environment variables: HOME, PWD, USER, UID, SHELL, etc.
4. Get the variable value length $ {# var}
buxizhizhou@ubuntu:~/lssc/chpt1$ var=1234567890buxizhizhou@ubuntu:~/lssc/chpt1$ echo ${#var}10
Use the PS1 environment variable to customize the prompt text. The default shell prompt text is in the file ~ /. Bashrc.
buxizhizhou@ubuntu:~/lssc/chpt1$ cat ~/.bashrc | grep PS1[ -z "$PS1" ] && return PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"buxizhizhou@ubuntu:~/lssc/chpt1$ PS1="PROMPT>"PROMPT>PROMPT>PROMPT>PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ 'buxizhizhou@ubuntu:~/lssc/chpt1$
Some special characters can be expanded into system parameters. For example, \ u is extended to the user name, \ h is extended to the host name, And \ w is extended to the current working directory.
4. Add environment variables using functions