Less keyword commands used:
1. at: (automatic) represents the automatic execution of a task
Example: ``` at 10am mar 31 2015 at> echo "taxes due" At> ^D (ctrl+D for storing the task) ``` atq: Show scheduled tasks atrm job-d: Delete a set task
2. crontab: Repeatedly perform a task
``` * * * * * command to be excuted - - - - - | | | | | | | | | +-------day of week(0 to 6) | | | +---- month (1 to 12) | | +---------day of month (1 to 31) | +----- hour (0 to 23) +---------min(0 to 59) ``` //Example: ``` crontab -e 5 * * * * echo "shell command crontab " >> /Users/liudong/shell_learning wq #save and quite crontab job ```Indicates that tasks in a command are executed every 5 minutes
3. head and tail commands: Show how many lines are before or after a text file.
seq 100 > numbers.txt #Generate 1-100 and write to numbers.txt head -3 numbers.txt #Show first 3 lines tail -5 numbers.txt #Show the last 5 lines head -65 numbers.txt | tail -5 >log.txt # Display lines 61-65 and write to log.txt
4.diff: Used to compare two files with different contents.
diff file1 file2
5. cut: Extract the specified column from the text
-c: Specify which character to display -d: Specifies the separator between columns -f: Specify which columns to display
Example:
$ cut -d: -f1-5 /etc/password #Displays the contents of columns 1-5 of the file/etc/password, separated by: output: ## nobody:*:-2:-2:Unprivileged User root:*:0:0:System Administrator daemon:*:1:1:System Services _uucp:*:4:4:Unix to Unix Copy Protocol _taskgated:*:13:13:Task Gate Daemon _networkd:*:24:24:Network Services _installassistant:*:25:25:Install Assistant _lp:*:26:26:Printing Services
$ date | cut -c1-5 #Show the first five characters of date output output:2019 year
6. paste: File content merge display command (Note that merge display is the only thing, and the file content will not change.)
Example: Merge the contents of file file1 file2 file3
First look at the contents of the three files before merging
$ cat file #Content of file xiongdan 200 lihaihui 233 lymlrl 231 $ cat file2 #Contents of the testfile file file liangyuanm ss $ cat file3 #Contents of the testfile1 file huanggai 56 zhixi 73
Then execute the merge display command:
$ paste file1 file2 file3 output: xiongdan 200 lihaihui 233 lymlrl 231 liangyuanm ss huanggai 56 zhixi 73
7. uniq: Remove adjacent duplicate lines (for display only, file content will not be changed).
Example:
$ cat test aa aa cc cc bb bb yy zz $uniq test aa cc bb yy zz
8, tr: Character replacement operation (only for display, does not change the actual contents of the file.)
Example:
tr '[a-z]' '[A-Z]' < filename #Convert lowercase to uppercase tr '|' '~' < filename # Replace the'-'character in the file with'~'.
9. sort: Sort the text file contents (in behavioral units).
$ sort filename #Arabic order by first letter of each line $ sort -d filename # Sort by dictionary (similar to above) $ sort -r filename #Reverse sort $ sort +num filename #Sort by the contents of column num
10, >>> < &> >&: IO related
$ echo "Welcome to Shell Scripting" > log.txt #The contents of log.txt are set to string contents $ echo "Welcomet to shell scriptin" >> log.txt #The contents of the string will be added to the end of the file $ ls > log.txt #Enter the output of the ls command into log.txt $ gcc hello.c 2 > error_file #Compile hello.c and redirect the error content to error_file if an error occurs $ find . -name "*.sh" > success_file 2> /dev/null # Enter the result of the find command into success_file and discard the error if an error occurs (/dev/null means discard any input to the character device) $ find . -name "*.sh" &> log.txt #Redirect output results with errors to log.txt $ find . -name "*.sh" > log.txt 2>&1 #Redirect results to log.txt and errors to output, both log.txt $ echo "File needs an argument" 1>&2 #Redirect output to error stream
11. Wildcard correlation
char | Meaning | Example | Possible Output |
---|---|---|---|
* | Match any character or number | $ ls –l .c file | Sample.c, hello.c, file1, file_2, filebc |
? | Match any character | $ ls -l file? | filea, fileb, file1 |
[...] | Match any character in brackets | $ ls -l file[abc] | filea, fileb, filec |
12. Braces {}
$ touch file{1,2,3} $ ls file1 file2 file3 $ mkdir directory{1,2,3}{a,b,c} $ ls output:directory1a directory1b ....
13. IO redirection related operators.
char | Meaning | Example | output |
---|---|---|---|
> | Output Redirection | ls > ls.out | Write the output of the LS command to the file ls.out |
>> | Output redirection (how added at the end) | ls >>ls.out | |
< | input redirection | tr 'a' 'A' < file1 | Instead of reading from the keyboard, the tr command reads from file1 |
|| Logical or | $ test $x -gt 10 | $x -lt 15 | |
&& | Logical and | $ test $x -gt 10 && $x -lt 15 | Check if the value of x is between 10 and 15 |
14. Vi-based related operations.
Example:
$ vi file3 #Open file3 via vim #In command mode, type the command::set hlsearch #This highlights the string matched by the search. :set ic #Ignore case when searching for matches :set noic #Case sensitive when searching for matches.The mac system is case insensitive by default.
Search and Replace commands:
command | describe |
---|---|
/pat | Search for pat in an open file and move the cursor to a matching string |
/ | Repeat last search |
:%s/old/new/g | Replace globally in the open file, replacing all old s with new |
:#,#s/old/new/g | Replace the old contents between lines with new. |
/^love/ | Highlight lines starting with love |
/love$/ | Highlight lines ending in love |
/^love$/ | Highlight lines with love d content |
/l.ve/ | For wildcards, you can match to love lxve live... |
/o*ve/ | * can match ve ove oove ooooove for a number of wildcards |
/[Ll]ove/ | Can only match to love Love |
/ove[a-z] | Highlight ovea-ovez |
/^[A-Z]...$/ | Match begins with a capital letter followed by a 2-character line |
/^[A-Z]*[a-z]*3[0-5]/ | Highlight any line ending in 30-35 |
/[a-z]*\ ./ | Highlight any line that contains lowercase letters and ends with. |
15. grep: Full-text retrieval command
Both grep content file_name content and file_name can use wildcards
g:globally RE:regular expression p: print out
Grep-n: Display line numbers at the same time
Grep-i: Fuzzy case
Grep-v: Show mismatched rows
Grep-l'Nuts'*: Lists the files containing Nuts in the current directory
Grep-c'Nuts'sample.txt: Displays the total number of rows matched
Grep-w'Nuts'sample.txt: Displays rows where the entire word matches the style
Metacharacter meaning:
Metacharacter | function | Example | describe |
---|---|---|---|
^ | Line Start | '^mango' | Show lines starting with "mango" |
$ | End of line | 'mango$' | Show rows ending in mango |
. | Match a single character | m...o | Show lines containing m followed by 2 characters followed by o |
* | Match characters before 0 or more *signs | '*mango' | Displays rows containing 0 or more spaces followed by mango |
[] | Match any one of the set of brackets | [Mm]ango | Show rows containing Mango or Mango |
[^] | Match does not belong to any of the set of brackets | '[^A-M]ango' | Show rows without A-M followed by ango |
< | Words that begin with what | '< mango' | Show lines starting with mango |
> | What word ends with | 'mango>' | Show rows ending in mango |
16,?: The execution result of the last shell command.
0: on behalf of the previous statement executed successfully; non-0: on behalf of the previous statement executed unsuccessfully.
Example:
$ ls $ echo $? output:0 $ ls /root $ echo $? output: 1
17, ``: Command substitutes.
Characters between two ``` are treated as shell commands.
Example:
$ echo "Hello `whoami`" output:Hello liudong $ echo "Hello $(whoami)"
18, pwd: Current directory.
Example:
$pwd output:/Users/liudong/shell_learning $ dirname="$(basename $(pwd))" #Show only the current directory $ echo dirname outpu:shell_learning
19,;: Separator between two shell commands on the same line
20, (): Multiple commands are grouped together and their output can then be redirected uniformly to a pipe or file.
21. Logical Operator: & && ||
&: The shell command on behalf of this one will run in the background;
&&: The first statement must be executed successfully before the second statement can be executed.
$ ls /home/liudong && echo "Command executed successfully" output: Command executed successfully $ ls /root && echo "Command executed successfully"
||: The first statement must fail before the second statement can be executed.
$ ls /root || echo "Command execution failed"
22,[[ expression ]]
Is an unconditional expression judgment.
23. Single and double quotation marks.
$ person="liudong"
$ echo "$person" # liudong
$ echo '$person' # $person
$ echo $person #$person
24, $$: Displays the process id of the current shell
25.export: Used for variable scope control.
Variables declared by the parent process can be made visible to the child process by adding the keyword export, but keywords declared by the child process cannot be visible to the parent process.
Whenever another shell script is executed, a child process is generated.
$ person="liudong" $ export person
26.read-only: Read-only variable.
$ readonly person="liudong" $ unset person output:bash: unset: person: cannot unset: readonly variable Another way to declare read-only variables: $ Declare -r person="liudong"
27. Script parameter delivery
Symbol | Meaning |
---|---|
$0 | shell script name or command name |
$1-$9 | Parameters 1-9 |
${10} | Parameter 10 |
$# | Total number of parameters |
$* | Show all parameters |
$@ | Same as $*, unless enclosed in double quotes |
"$*" | Display parameters in the format'$1 $2 $3' |
"$@" | Format'$1''$2''$3' |
Set: When executing a script, parameters are not necessarily passed through the command line; they can be set through the set command
$ set USA Canada UK France $ echo $1 $ echo $2 $ echo $3 $ echo $4
shift: parameter offset setting.
#!/bin/bash echo "All arguments passed are as follow:" echo "$*" echo "shift by one position:" shift echo "value of positional parameter $1 after shift:" echo $1 echo "shift by two positions:" shift 2 echo "value of positional parameter $1 after two shifts:" echo $1
inputs:sh shift.sh one two three four
outputs:
All arguments passed are as follow:
one two thress four \n
shift by one position:
value of positional parameter two after shift:
two
shift by two positions:
value of positional parameter four after two shifts:
four
So each shift offset is based on the current position.
Old parameter storage:
$set alan john dennis $1 is alan $2 is john $3 is dennis $ oldargs=$* #Save old parameters in the temporary variable oldargs $ set $oldargs #Retrieve the old parameter.
28.while do case: conditional selection statement
#!/bin/bash USAGE="usage: $0 -x -y" while getopts :xy: opt_char do case $opt_char in x) echo "option x was called." ;; y) echo "option y was called .Argument called is $OPTARG" ;; \?) echo "$OPTARG is not a valid option." echo "$USAGE" ;; esac done
input:sh getopt.sh -x
output:option x was called.
input:sh getopt.sh -y ggggggggggggg
output:option y was called .Argument called is ggggggggggggg
input:sh getopt.sh -y gggggggggg -x
output:option y was called .Argument called is ggggggggggggg
option x was called.
When reading here, because x is followed by no OPTARG, the parameters after X will not be read, and case y is followed by OPTARG, so the parameters after -x will not be read, while case is followed by OPTARG, so the parameters after X will not be read, and case y is followed by OPTARG, so the parameters after -y will be readRead.
29. Default parameters.
Example:
#!/bin/bash MY_PARAM=${1:default} echo $MY_PARAM
Format:
${num:-value} # num: Indicates the default value for the number of times the parameter is set; value represents the value of the default parameter.Note that there is a -
Default parameters, of course, refer to the use of default parameters when no value is passed to a parameter at a specified location, and if a value is passed, the passed parameter is used.
30.array:
Example:
#!/bin/bash FRUITS=(Mango Banana Apple) echo ${FRUITS[*]} echo $FRUITS[*] echo $FRUITS[2] FRUITS[3]=Orange echo $FRUITS[*]
output:
Mango Banana Apple
Mango[]
Mango[2]
Mango[]
There are several ways to declare array variables:
1 #!/bin/bash 2 FRUITS=(Mango Banana Apple) 3 echo ${FRUITS[*]} 4 echo $FRUITS[*] 5 echo $FRUITS[2] 6 FRUITS[3]=Orange 7 echo $FRUITS[*] 8 declare -a fruit=('Mango' 'Banana' 'Apple' 'Orange' 'Papaya') 9 declare -a array_name=(word1 word2 word3 word4) #Note: The left and right sides of the equal sign cannot have spaces. 10 echo ${fruit[0]} 11 echo ${fruit[1]} 12 echo "all the fruits are ${fruit[*]}" 13 echo "the number of elements in the array are ${#fruit[*]}" 14 unset fruit
There is another way to initialize:
$countries=(USA [3]=UK [2]=Spain)