LinuxShell programming Learning

Source: Internet
Author: User
Tags bitwise operators
LinuxShell programming learning-Linux general technology-Linux programming and kernel information. The following is a detailed description. I. Shell variables mainly include local variables and environment variables.

1. Local variable -- Used in the script currently running by the user
1) define the local variable format: variable-name = value
Example: [root @ jike1/root] # LOCALTEST = "test"
[Root @ jike1/root] # echo $ LOCALTEST (Note: echo $ LOCALTEST and echo $ {LOCALTEST} have the same effect)
(Add $ before the variable name to obtain the value of this variable. You can use the echo command to display the value of this variable)
2) display local variable format: set
Example: [root @ chinaitlab root] # set
3) Clear the local variable format: unset variable-name
For example: [root @ jike1/root] # unset LOCALTEST
Then execute echo $ LOCALTEST, and the output of the variable LOCALTEST will not be displayed.

2. environment variables -- used in all sub-Processes
1) define the environment variable format: export variable-name = value (an export keyword is missing compared with the definition of the local variable)
Example: [root @ chinaitlab/root] # export DOMAIN = "chinaitlab.com"
[Root @ chinaitlab shell] # vi testenv. sh
#! /Bin/bash # indicates parsing scripts using bash.
# Testenv. sh
Echo $ DOMAIN
[Root @ chinaitlab shell] # chmod + x testenv. sh
[Root @ chinaitlab shell] #./testenv. sh
Chinaitlab.com
2) display environment variable format: env (set is used for local variable display and env is used for environment variable display)
Example: [root @ chinaitlab test] # env
3) Clear the environment variable format: unset variable-name (the usage is the same as that of local variables, both use unset)
Example: [root @ chinaitlab shell] # unset DOMAIN
Execute./testenv. sh again, and the output of the variable DOMAIN will not be visible.

3. Other variables
1) Location variables $0, $1, $2, $3 ...... $9
2) readonly variable
Note: Read-Only variables cannot be cleared or their values are changed. Therefore, use them with caution.
3) special variables $ #, $ ?, $ (PID of the current process )......

Ii. Operators and expressions

1. The operator is a command sent to a computer. The operator types include:
Arithmetic Operators (+ ,-,*,/)
Bitwise operators (~ , <,>, &, |, ^)
Logical operators (&, |,>, =, <,! =)
Assignment operators (=, + =,-=, * =,/=, % =, & =, ^ =, |=, <=, >>=)

2. Expressions are the combination of operators and operation objects.
1) $ []: an expression that can accept numbers of different bases
Echo $[10 + 1] (output: 11)
Echo "$ [2 + 3], $ HOME" (output: 5,/root)
Echo $[2 <3], $ [8> 1] (output: 16, 4)
Echo $ [2> 3], $ [3> 2] (output: 0 when expression 0 is set to false, and 1 when expression 0 is set to true)
2) character expression: directly written, caused by single quotation marks and double quotation marks.
Echo "$ HOME, That is your root directory." (output:/root, That is your root directory .)
Echo '$ HOME, That is your root directory.' (output: $ HOME, That is your root directory .)
The difference between single quotation marks and double quotation marks is that single quotation marks are displayed as is, and double quotation marks show variable values.
3) test expression

Iii. Control Structure

1. if statement
Example :#! /Bin/bash
# If. sh
If ["10"-lt "12"] # Note: There are spaces between if and [, between [and "10", "12" and]. if no space is added, A syntax error occurs.
Then
Echo "Yes, 10 is less than 12"
Fi

2. case statement
Example :#! /Bin/bash
# Case. sh
Echo-n "Enter a start or stop :"
Read ANS
Case $ ANS in
Start)
Echo "You select start"
;;
Stop)
Echo "You select stop"
;;
*)
Echo "'basename $ 0': You select is not between start and stop"> & 2
# Note: There is no space between> and & 2.> & 2 indicates that the output will be displayed on the standard output (usually on the screen ).
Exit;
;;
Esac

3. for Loop statements
Format: for variable name in list
Do
Command 1
Command 2
......
Done

4. until statement
Format: until Condition
Do
Command 1
Command 2
......
Done

5. while loop statement
Format: while command
Do
Command 1
Break
Command 2
Continue
Command 3
......
Done

Iv. Input and Output

1. Several important tools
1) echo
For example, echo-n "Enter a number from 1 to 2:" (-n indicates no line break, and the cursor stays at the end of the line)
2) read
Example: read ANS (storing user input in the ANS variable)
3) cat (display file content)
4) pipeline (|) (the output of one program serves as the input of another program)
Example: ls-l | grep "d"
5) file redirection (> and>)
For example, ls-l>/tmp/a.txt (re-write the output result to the.txt file)
Ls-l>/tmp/a.txt (add the output result to the.txt file, which is often used to record logs)
6) standard input ($0), standard output ($1), and standard error ($2)
It is defined by file descriptors ($0, $1, and $2.

2. instance: readme. sh
#! /Bin/bash
# Readname. sh
Echo-n "First Name :"
Read firstname
Echo-n "Last Name :"
Read lastname subname
Echo-e "Your First Name is :$ {firstname} \ n" # added-e to parse \ n as an escape character
Echo-e "Your Last Name is: $ {lastname} \ n"
Echo-e "Your Subname is: $ {subname} \ n"
Echo "Your First Name is :$ {firstname} \ n"> firstname.txt # This line does not contain-e, SO \ n is displayed as is in the output.
Echo "Your Last Name is :$ {lastname} \ n"> lastname.txt
Echo "Your Subname is :$ {subname} \ n"> & 1

5. Text Filtering

1. Regular Expression (that is, pattern matching)
2. find (find a file)
Example: find./-name "*. txt"-print
3. grep (search characters)
Example: grep "[5-8] [6-9] [0-3]" access_log
4. awk (divides a series of data into columns)
Example: awk '{print $1 "\ t" $4}' access_log
5. sed (search and replace Data)
For example, sed-n's/chinaitlab/hello/P' myfile.txt (replace chinaitlab in myfile.txt with hello and print it to the screen. s indicates replacement, and p indicates printing. If a redirection character is added, the replaced content can be output to a file .)
6. sort)
Example: sort ip.txt
7. uniq (display whether this column is unique or not. It can be unique)
Example: uniq ip.txt
8. split (you can split the file)
Example: split myfile.txt
9. instance: kill_process.sh
#! /Bin/bash
# Kill_process.sh
Current_PID =$ $
Ps-aux | grep "/usr/sbin/sshd" | grep-v "grep" | awk '{print $2}'>/tmp/invalid values current_pid=.txt
For pid in 'cat/tmp/folder then current_pid=.txt'
Do
{
Echo "kill-9 $ pid"
Kill-9 $ pid
}
Done
Rm-f/tmp/folder names current_pid=.txt

Vi. Shell functions

1. There are two formats for defining functions:
Function Name ()
{
Command 1
......
}
Function Name ()
{
......
}

2. instance: func. sh
#! /Bin/bash
# Func. sh
# Source function library. #/etc/rc. d/init. d/functions stores a large number of functions already written.
./Etc/rc. d/init. d/functions # introduce functions in/etc/rc. d/init. d/functions, which is equivalent to include
Function hello ()
{
Echo "Hello, $1 today is 'date '"
}
Echo "now going to the function hello"
Hello chinaitlab
Echo "back from the function"
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.