Linux Shell Scripting Learning Summary

Source: Internet
Author: User

Shell Programming:

Bash:#!/bin/bash must be specified at the beginning of the shell

How the Shell executes:

1../1.sh executes the 1.sh,1.sh in the current directory if the executable file

2. bash/usr/local/1.sh

Define variable aa= ' QQQ ' = cannot have spaces on either side, use variable ${AA}

Shell Special meaning variables

$$ The process ID of the current script, which is the PID

Fetch the current file name

$n N is a number greater than 0, N is the first parameter

$# number of parameters to be taken

$* Take all parameters

$? Take the most command exit status, 0 is no error

For example:

./1.sh a B C

Echo $$

Echo

Echo $, $

Echo $#

Echo [email protected]

Echo $?

Shell operator +-*% subtraction

Shell variable Operation $ (()), $ ((4-2)), $ (($a-$b))

Shell Relational operators

-eq equals judgment, equal return True

-ne not equal to judgment, not equal to return true

-GT greater than judgment, if greater than return true

-lt less than judgment, less than return true

-ge is greater than or equal, returns True

-le is less than or equal, returns True

Shell Boolean operations

! Take counter, [!false] =true

-O or operation, [$a –lt 20–o $b –GT 100]

-A with budget, [$a –lt 20–a $b –GT 100]

Shell String Operations

= = Determines whether two strings are equal and returns true [$a = = $b]

! = two strings are not equal, returns true [$a! = $b]

-Z detects if string length is 0, returns true [-Z $a]

-N detects whether the string length is not 0 and does not return true for 0 [-N $a]

STR detects if the string is empty and does not return true for null, [$a]

Shell File Test Operators

-D file detection is a directory file, return true,[-d file]

-F file detects if the files are normal and returns true

-R file detects if the files are readable and returns true

-W file detection is modifiable

-X file to detect whether files are executable

-S file detects if the files are empty, the file size is greater than 0, is not empty, returns true

-e file detects the existence of files that contain directory files

Use of Shell single and double quotation marks

' cannot contain single quotation marks, the translated characters in single quotes \ and the variable $ characters are output as is, with no practical meaning

"" cannot contain double quotes, double quotes in the translation character \ and the variable $ character is the actual content, can be translated after and after the output of the variable

Shell Common pass-and-lose characters

"*" matches 0 or more characters

? Match any one of the characters, A?b can match the ACB, cannot match AB

A[xyz]b Match AB has a string of either x, Y, z, and there is only one, 0 and more are not available

A[!xyz]b Matching AB can only have one, and not X, Y, z data, such as ABB

A[1-9] matches any one of 1-9, such as A1,a9

A{abc,123} matches any one of the list, such as aabc,a123

Shell Special Characters

= Variable Assignment a=1

$ take variable $a

">" Output redirect cat 1.txt 2.txt >3.txt, will overwrite 3.txt

">>" output append redirect cat 1.txt 2.txt >> 3.txt, append to 3.txt

"<" input redirection

& Background Run command

; command separator, allow one line to write multiple commands

&& if the previous command executes successfully, proceed to the next command

|| The previous command did not succeed and the next command was executed

! 4 Execute the 4th command in the history

Shell Array Operations

Array= (A1 a2 A3) array elements are separated by spaces, and the shell supports only 1-dimensional arrays

A[0]=1; a[1]=2 defines an array by assigning a value

How arrays are evaluated

${array name [index]} like ${a[0]}

${a[*]},${a[@]} Gets all elements of the array

len=${#a [*]} to get the length of the array

If judgment of the Shell

Format:

IF [Condition established]

Then

Statement

Else

Do not set the execution statement

Fi

Cases:

#!/bin/bash

File= "/usr/local/1.sh"

If [-X $file]

Then

Echo $file

Else

Touch $file

Chmod +x $file

Fi

Shell Case ESAC Judgment

Format:

Case value in

Value 1)

Command

;;;; same break, jump statement

Value 2)

Command

;;

*)

Command

;;

Esac

Cases:

#/bin/bash

A=$1

Case $a in

R

bash/usr/local/1.sh

;;

C

touch/usr/local/2.sh

;;

D

rm–rf/usr/local/2.sh

;;

*)

bash/usr/local/1.sh

;;

Esac

Shell for Loop

Format:

For variable in list

Do

Command

Command

Done

Example: using for and if, traverse all the files in the/root directory, print out the executable file

#! /bin/bash

Path= '/root '

For file in $path/*

Do

If [-X $file –a–f $file] #file是可执行文件and file is normal

Then

Echo $file

Fi

Done

Shell while loop

Format:

While condition

Do

Command

Done

Functions in the shell

Function name ()

{

Command

Return x

}

Cases:

#!/bin/bash

Functionshowsh ()

{

For file in $1/*.sh

Do

Echo $file

Done

}

Showsh $

To configure a scheduled task:

Vi/etc/crontab timed Task Configuration

crontab file configuration m h d m d cmd m minutes (0-59) H hours (0-23) D days (1-31)

M month (1-12), d days of the week (0-6, 0 bit Sunday)

*/5 * * * * * sh/usr/locat/1.sh is not executed every 5 minutes

* * * */usr/local/apache/bin/apachectl restart restart Apache every 21:30

Software Installation:

RPM-IVH JDK.RMP Install JDK,-I installation,-v display installation information,-H display installation progress

RPM-E jdk.rpm Uninstalling the JDK

Rpm-qa to query installed packages

Yum Install httpd Apache installation

Yum list|grep httpd Query the desired package from the Yum list

Yum Update httpd upgrade Apache

Yum List installed |grep httpd query if Apache is installed

Yum remove MySQL uninstall yum installed MySQL

SOURCE Installation:

Yum Install gcc* gcc C + + compiler

1. Generate Makefile Compilation files

The./configure generic installation package has a configure file that is used to generate makefile compiled files

./configure--prefix=/usr/local/apache--with=/aa/cc--prefix Specify installation directory,--with specify dependent file directory

2.make Compiling makefile Files

Make also needs to be installed, yum install Make,makec,makeg

3.make Install

Make install copies the compiled files to the directory specified by--prefix, if not specified, is the current directory

Linux related directory file storage content:

The bin is stored in a normal program and can be used by all users

Boot storage kernel and files required for boot

Dev storage device related files

ETC Storage System configuration file

Home directory where user files are stored

Lib holds the library file at startup

MNT stores temporary mapping files that can be used to mount USB

Proc Store current system state, process

Root Super User home directory

Sbin storage of required system management procedures

TMP holds temporary files generated by startup

Frequently changing files generated by Var storage system

System CPU and memory information file:

/proc/cpuinfo CPU Information

/PROC/DMA DMA Channel

/proc/filesystems File System Information

/proc/interrupts Host Segment Information

/proc/ioports I/O port information

/proc/meminfo Viewing memory information

/ETC/PASSWD User Details

Linux Shell Scripting Learning Summary

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.