1. Initial knowledge of Bash
Start by compiling a simple bash
Vim Hello. SH
Using Vim to edit hello.sh, enter the following code and Save:
#!/bin/bashecho Hello Word
How to run the Bash script:
sh hello. SH # Use Bash to execute bash hello. SH
You can also have executable permissions on the script itself, which chmod
can be modified by commands:
chmod u+x hello. SH # Executes the command, which will be executed using the shell specified in the first line of the script, and if the specified shell does not exist, the system default shell will be used to execute the $ . /hello. SH
2.bash Special Symbols
2.1 # Comments (#! Excluding
#! Is the interpreter used to specify the current script, which we are here for bash
, and should indicate the full path, which we thought/bin/bash
#!/bin/bash echo " the # here does not begin a comment. " echo the # here does not begin a comment. echo echo The # here begins a comment echo ${path#*:} # parameter substitution, not a comment echo $ (2 #101011 )) # System conversion (using binary notation), not a comment
2.2; semicolon (use a semicolon (;) to write two or more commands on the same line. )
#!/bin/BashEchoHelloEchothere filename=ttt.SH if[-R"$filename"]; ThenNote"if"And" Then"need to separateEcho "File $filename exists.";CP$filename $filename. bakElse Echo "File $filename not found.";Touch$filenamefi;Echo "File test complete."
2.3 Terminating case option (double semicolon)
#!/bin/bash varname=case"$varname"in [a echo"ABC";; [0-9echo"123"Esac
The 2.4-point number. Equivalent to Source
$ source Test. SH Hello world$. Test. SH Hello World
2.5 quotation marks
2.5.1 double quotation mark (")" string "will block (interpret) most of the special characters in the string
2.5.2 Single quotation mark (') ' string ' will block the interpretation of all special characters in STRING, which is a more intense form than using ".
2.6 Slash-and-slash
2.6.1 slash (/) filename path delimiter. Separate parts of the file name (such as/home/bozo/projects/makefile). can also be used as a division arithmetic operator.
2.6.2 backslash (\) a reference mechanism for single-character. \x will "escape" the character X. This is equivalent to "X", which is also equivalent to ' X '. \ is usually used to escape double quotation marks (") and single quotation marks (') so that double and single quotes are not interpreted as special meanings.
2.7 Anti-quote (')
cp 'mkdir back ' test. SH ls
2.8 Colon (:)
2.8.1 An empty colon is equivalent to "NOP" (no op, a command with nothing to do). Can also be thought of as true of the shell's built-in command.
#!/bin/bash while :do echo"endless Loop " Done
Equivalent to:
#!/bin/bashwhiletruedo echo"endless loop " Done
You can make a placeholder in If/then:
#!/bin/bashcondition=5if0 ] then : # do nothing, exit the branch Else Echo " $condition " fi
2.8.2 variable extension/substring substitution
2.8.2.1 >
when used in conjunction with the redirect operator, a file is emptied, but the file's permissions are not modified. If this file does not exist before, create the file.
$: > Test. SH # file "Test. SH cat /dev/null > Test. SH function The same # However, this does not result in a new process because ":" is a built-in command
2.8.2.2 ":" is also used /etc/passwd
to $PATH
make delimiters in and variables.
Echo $PATH/usr/local/bin:/bin:/usr/bin:/usr/x11r6/bin:/sbin:/usr/sbin:/usr/games$
2.9 Question mark (? )
In a double-brace structure, the ?
ternary operator of C language
#!/bin/bash A=ten (t=a<? 8:9echo $t
2.10 dollar sign ($)
2.10.1 variable substitution
#!/bin/bashvar1=5var2=23skidooecho $var 1 5 echo $var 2 # 23skidoo
Summary of Linux Bash Script Foundation symbols (i)