Shell Learning Note 1 "Linux shell programming from beginner to proficient in 2nd edition"

Source: Internet
Author: User
Tags arithmetic arithmetic operators clear screen

    1. number of logged-in users: who | Wc-l
    2. See which users are logged on to the system : who
    3. Create a new file /bin/ct11, enter a command in the file,chmod Set the Execute permission for the file

# cat >/bin/ct11

W.H.O. | Wc-l

Press Ctrl+d

# Cd/bin

# chmod +x ct11

#./ct11

Execution Result:

2

    1. # echo escape sequence:

\c : Indicates that echo does not print the last line break symbol

\a : Warning character, usually the BEL character of ASCII

\b : Backspace

\f : Page Change

\ n : line break

\ r: Enter

\ t: Horizontal tab

\v: Vertical tab

\ \ : backslash character

\ODDD: Converts a character into a 3 -bit octal value

    1. printf statement printf "I said '%s,%s1,%s2,%s3 ' \ n" This is my pen
    2. Clear Screen :Clear
    3. Echo input A text branch append to file cc11: echo-e "WERW\NSFSD" >>cc11
    4. Remove the ASCII carriage return from the Aa.txt file and write the contents to file.txt

# tr-d ' \ R ' < Aa.txt > file.txt

    1. Remove the carriage return from the Aa.txt file and then use the pipeline to sort its file contents and enter it into the file file.txt

# tr-d ' \ r ' <aa.txt | Sort >file.txt

    1. turn off the ability to automatically print input characters Stty-echo
    2. reply to the function of automatically printing input characters stty Echo
    3. trace function set-x is to perform trace function open,set +x is turn off tracking function

Eg:cat > aa.sh

Set-x

Echo SS Echo

Set +x

Echo SDF Echo

Ctrl +d

Chmod +x aa.sh

./aa.sh Execution

    1. Automated Test Course Catalog

    1. Shell Pass Parameters

Create a new tst.sh script,

Nano-w tst.sh

The contents of the edit script are as follows, and then the first second parameter, and so on, is the file name parameter

#!/bin/sh

Name=$1

echo "My name is ${name}!"

Save Ctrl +o When edit is finished, Ctrl + C when exiting

Chmod +x tst.sh has execute permission for script

./tst.sh Zhang

Execution Result:

My name is Zhang

if it is vi mode edit mode press i, save and exit press ESC: wq!

    1. unset command can delete variable, cannot be used again after deletion,unset command cannot delete read-only variable
    2. Example:echo-e "Jdkfja \ n" output result:Jdkfja
    3. here -E indicates the substitution of the escape character. If you do not use the-e option, it is output as-is: jdkfja\n
    4. command substitution refers to the The shell can execute the command first, save the output temporarily, and output it in the appropriate place. Syntax for command substitution:

' Command '

Note that it is an anti-quote, not a single quotation mark, this key is located Below the ESC key.
In the following example, the command execution results are saved in a variable:

    • If...then...fi is a conditional statement that will be explained later.

List of arithmetic operators

Operator

Description

Example

+

Addition

The ' expr a+a+b ' result is 30.

-

Subtraction

The ' expr a?a?b ' result is 10.

*

Multiplication

The ' expr a\*a\*b ' result is 200.

/

Division

The ' expr b/b/a ' result is 2.

%

Take surplus

The result of ' expr bba ' is 0.

=

Assign value

A= $b assigns the value of variable B to a.

==

Equal. Used to compare two numbers, the same returns true.

[A==a==b] returns FALSE.

!=

Not equal. Used to compare two numbers, and returns true if they are different.

[A!=a!=b] returns TRUE.


    1. NOTE: Conditional expressions are placed between square brackets and have spaces, such as [A==a==b] is wrong and must be written in [A==a==b].

Shell Annotations

1.1 Shell Comment # The entire code comment can be enclosed in curly braces, similar to a function

1.2 Shell String

1.2.1 Single quote str= ' This is a string ' single quote cannot appear in single quotation marks

1.2.2 double quotes str= "This is a string" double quotation marks can have variables that can appear escape characters

1.2.3 Stitching string yourname= "JKHJ" greeting= "Hello," $yourname "!"

1.2.4 Get string length string= "DFA" Echo ${#string}

1.2.5 extract substring string= "My name is ending" echo ${string:1:4} output:y na

1.2.6 Find string string= "My name is ane" echo ' Expr index "$string" is "

1.3 Shell Array

Bash supports one-dimensional arrays, does not support multidimensional arrays, and does not limit the size of arrays

1.3.1 defining Arrays

Array_name={1 2 3 4}

or array_name={

1

2

3

4

}

Each component of an array is defined individually:

Array_name[0]=1

array_name[0]=2

1.3.2 reading an array

Value=${array_name[0]}

use @ or * to get all the elements in the array

Value=${array_name[*]}

Value=${array_name[@]}

1.3.3 Gets the length of the array

num=${#Array_name [@]} gets the number of array elements

length=${#Array_name [0]} gets the length of the first element of the array

1.3 Echo Command

1.3.1 echo "\" It is a test\ "" output: "It is a test"

1.4 Line break

1.4.1 echo "ok!\n" echo "It is a cat"

Output Result:

Ok!

It is a cat

1.5 Show No Line break

1.5.1 echo "ok! \c "echo " It is a dog "

output Result: ok! It is a dog

1.6 Display results redirected to file echo "It is a test" > myfile

1.7 output String as- is (not escaped), using single-quote echo ' $name \ '

1.8 Display current date echo ' Date '

1.9 Arithmetic Operations

    1. Echo ' Expr 2 + 2 '
    2. multiplication,echo ' expr 4 \* 2 '

1.10 Relational Operations

If [3-lt 8]

Then

Echo ' true '

Else

Echo ' false '

Fi

1.11 Shutting down the firewall

Systemctl Stop Firewalld.service

1.12 View shell types cat/etc/shells

1.13 new File touch filename

Shell Learning Note 1 "Linux shell programming from beginner to proficient in 2nd edition"

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.