Linux Shell 2

Source: Internet
Author: User
Tags case statement echo command

Chapter 3 shell Input and Output
3.1.1 echo
The echo command can display text lines or variables, or input strings to files.
Echo [Option] string
--E parse escape characters
--N: Do not use line breaks. By default, line breaks are used in Linux.
-Escape Character (\ c, \ f, \ t, \ n)
\ C press enter without line feed
\ F forbidden
\ T Tab
\ N carriage return line feed

3.2.1 read
The read statement can read information from a line of text on the keyboard or file and assign it to a variable.
Read varible1 varible2...
If only one variable is specified, read assigns all input to the variable
The first filename terminator or carriage return. If multiple variables are provided, they are assigned in different order.
. Shell uses space as the separator between variables.

3.3.2 cat
Cat is a simple and common command that can be used to display the file content, create a file, and use
It is used to display control characters.
Cat [Options] filename2... filename2...
--V: display control characters
-When using the cat command, note that it does not stop at the file tab; it displays the complete
File. If you want to display a page each time, you can use the more command or output the cat command through the pipeline
Pass to another command with paging function (more, less ).
-Man cat
Cat myfile1
Cat myfile1 myfile2 myfile3
Cat myfile1 myfile2 myfile3> myfile
Cat myfile

3.4.1 MPs queue
The output of one command can be passed to another command as input through the pipeline. Pipe vertical bars |
Format: command 1 | command 2
DF-k | awk '{print $1}' | grep-V "filesystem"

3.5.1 tee
The Tee command transfers one output copy to the standard output, and the other copy to the corresponding file.
Tee-A files
If the system saves the output to a file, the command is no longer appropriate.
Generally used after a pipe
-A appends the content to the file. If-A is not found, the file content will be overwritten.

3.6.1 standard input, output, and errors
When executing commands in shell, each process is associated with three open files and uses the file descriptor
To reference these files. Because the file descriptor is not easy to remember, shell also provides the corresponding file name.

File descriptor
Input File-standard input 0 (keyboard or file or other command output by default)
Output file-standard output 1 (screen or file by default)
Error output file -- indicates Error 2 (screen or file by default)

There are actually 12 file descriptors in the system. You can use any file descriptor 3 to 9.

3.7.1 file redirection
Change the Input Source and output location of the program running

Command> filename redirects the standard output to a new file (overwrite the original file)
Command> filename redirects the standard output to a file (append)
Command 1> filename redirects standard output to a file
Command> filename 2> & 1 redirects the standard output and standard error to a file.
Command 2> filename redirects a standard error to a file
Command 2> filename redirects a standard error to a file (append)
Command> filename 2> & 1 redirects the standard output and standard error to a file (append)
Command <filename> filename2 command uses the filename file as the standard input,
Output with filename2 file as standard
Command <filename command uses the filename file as the standard input
Command <delimiter reads data in the standard input until the interface of the dilimiter
Command <& m uses the file descriptor m as the standard input
Command> & m redirects standard output to file descriptor m
Command <&-Disable Standard Input

Redirection standard output
Cat file | sort 1> sort. out
Cat file | sort> sort. out commands are equivalent
PWD> path. Out
> Nullfile.txt: Create a 0-Byte File

Redirect standard input
Sort <File
Sort <name.txt> name. Out

[Root @ localhost shell] # Cat> term.txt <Shell
> Hello, there I am using a $ term Terminal
> And my username is $ LOGNAME
> Bye...
> Shell
[Root @ localhost shell] # Cat term.txt
Hello, there I am using a VT100 Terminal
And my username is root
Bye...

Standard Error

[Root @ localhost shell] # grep "Trident" Miss 2> errmsg
[Root @ localhost shell] # Cat errmsg
Grep: Miss: No file or directory

3.8.1 combined use of standard output and standard errors
[Root @ localhost shell] # Cat myfile.txt abbb 1> accounts. out 2> accounts. Err
[Root @ localhost shell] # cat accounts. out
Aa
Ccc
Vvvv
Nnn
Dd
Sss
[Root @ localhost shell] # cat accounts. err
Cat: abbb: No file or directory

3.9.1
Merge standard output and standard errors
When merging standard output and standard errors, remember that shell analyzes the corresponding commands from left to right.
Grep "standard" standart.txt> grep. out 2> & 1
Find the standard content in the file standart.txt and output it to grep. out. If an error occurs, output it
The screen content is output to grep. out.
Both the correct output and the error output are output to the grep. out file.
[Root @ localhost shell] # grep "standard" standart.txt> grep. out 2> & 1
[Root @ localhost shell] # cat grep. out
Grep: standart.txt: No file or directory

3.10.1 exec
The exec command can be used to replace the current shell. In other words, the command does not start the shell.
Any existing environment will be cleared and a shell will be restarted.
Exec command
The command is usually a shell script.
When operating the file descriptor (or only at this time), it will not overwrite your current shell

3.11.1 file descriptor
3-9 file descriptors are available for us, and 0-2 is the default one.

Exec and file descriptor combination
[Root @ localhost shell] # cat file_desc
#! /Bin/bash
# File_desc
Exec 3 <& 0 0 <name.txt
Read line1
Read line2
Exec 0 <& 3
Echo $ line1
Echo $ line2
[Root @ localhost shell] #./file_desc
Aaaa
Ddfd

Chapter 4 Control Flow Structure
4.1 if statement
If condition 1 if condition 1 is true
Then
Command 1 run command 1
Elif condition 2 if condition 1 is not true
Then then
Command 2 Run Command 2
Else is not valid if the condition is 1 or 2.
Command 3: Execute Command 3.
FI complete

If condition 1
Then command
Fi

If ["10"-lt "12"]
Then ECHO "Yes, 10 is less than 12"
# Yes 10 is less than 12
Fi

If ["10"-lt "12"]
Then
# Yes 10 is less than 12
Echo "Yes, 10 is less than 12"
Fi

If ["13"-lt "12"]
Then
Echo "Yes, 13 is less than 12"
# Yes 10 is less than 12
Else
Echo "NO, 1 is greater than 12"
Fi


#! /Bin/bash
# If test3
Echo-n "Enter you name :"
Read NAME
# Did the user just hit return
If ["$ NAME" = ""]
Then
Echo "You did not enter you name"
Else
Echo "You Name is $ {NAME }"
Fi

#! /Bin/bash
# If elif else fi
Echo-n "Enter you name :"
Read NAME
If [-z $ NAME] | ["$ NAME" = ""]; then
Echo "You did not enter you name"
Elif ["$ NAME" = "root"]; then
Echo "Hello root"
Elif ["$ NAME" = "aaa"]; then
Echo "Hellp aaa"
Else
Echo "You are not root or aaa, but hi, $ NAME"
Fi

4.2.1 case statement
Case statements are multiple select statements. You can use the case statement to match a value with a pattern.
The command is successfully executed.

Case value in
Mode 1)
Command 1
;;
Mode 2)
Command 2
;;
Esac
The case value must be followed by the word in, and each mode must end with a right bracket. The value can be a variable or constant.
. After matching finds that the value matches a certain mode, all commands are executed ;;. Pattern match *
Represents any character ,? Represents any single character, and [..] represents any character in the class or range.
#! /Bin/bash
# Case select
Echo-n "Enter a number from 1 to 3 :"
Read NUM
Case $ NUM in
1)
Echo "You select 1"
;;
2)
Echo "You select 2"
;;
3)
Echo "You select 3"
;;
*)
Echo "'basename $ 0': This is not between 1 and 3"> & 2
Exit;
;;
Esac

4.3.1 for Loop
The general format of the for Loop is:
For variable name in list
Do
Command
Command 1
Done
When the variable value is in the list, the for loop executes all the commands once and uses the variable name to access the value in the list.
Command can be any valid shell command and statement. The variable name is any word. In list usage is optional
If not, the for loop uses the location parameter of the command line. The in list can contain replacement, string
And file name
#! /Bin/bash
# Fortest1 \
For loop in 1 2 3 4 5
Do
Echo $ loop
Done

#! /Bin/bash
# Fortest2
For loop in "oranger red bue gray"
Do
Echo $ loop
Done

#! /Bin/bash
# Fortest2
For loop in oranger red bue gray
Do
Echo $ loop
Done

4.4.1 until Loop
The general format of the until loop is:
Until Condition
Do
Command 1
Command 2
...
Done

Note: The condition can be any test condition. The test occurs at the end of the loop, so the loop must be executed at least once.

'Df | grep/backup | awk '{print $5}' | sed's/% // g''

4.5.1 while LOOP
The while loop is generally in the following format:
While command
Do
Command 1
Command 2
...
Done
Note: although only one command is usually used between while and do, you can run several commands:
It is usually used as a test condition.

#! /Bin/bash
# While test input content from the keyboard
Echo "press <ctrl> + D to exit the input. "
While echo-n "Enter your favorite movie:"; read FILM
Do
Echo "Yeah, $ {FILM} is a good movie! "
Done

#! /Bin/bash
# While read content from the file
While read LINE
Do
Echo $ LINE
Done <names.txt

4.6.1 break and continue
Break [n]
Exit Loop
If it is in an embedded loop, you can specify n to jump out of the number of cycles. The default value is 1.

Conitnue
Skip loop step
Note: The continue command is similar to the break command. There is only one important difference. It does not jump out of the loop,
Just jump out of this loop step.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.