Shell Scripting Learning

Source: Internet
Author: User
Tags case statement uppercase letter

Shell Scripting Learning

The shell is the command parser that converts the user-entered instruction into a program that the appropriate machine can run.

The shell is a program that acts as an interface between a user and a Linux system, allowing the user to enter commands to be executed to the operating system, which can have multiple shells in Linux.

One: the shell type:

Ash: The SHELL,BSH developed by Bell Labs is a symbolic link to ash.

BASH: is the GNU Bourne Again Shell, which is the default shell on the GNU operating system. SH and Bash2 are symbolic links to it.

TCSH: Is Berkeley UNIX C shell. CSH is a symbolic link to it

Two: Shell command

The basic format for Shell commands is:

command name [options] < parameters 1> < parameters 2> ...

Shell prompt: # $

The GNU tool in Bash, as/bin/sh is installed by default

In most Linux distributions, the shell program/bin/sh is actually a link to the program/bin/bash

Command/bin/sh-version can be used to view the version of Bash.

Three: Shell script

A shell script is a text file that contains a series of command sequences. When the script file is run, the sequence of commands contained in the file will be executed.

The first line of the shell script must be in the following format:

#!/bin/sh

The symbol #! is used to specify the parser for the script file. In the example above, use/bin/sh to parse the script. When you edit a script, you must also have an executable property if you want to execute the script: chmod +x filename.

In shell programming, a sentence that begins with # represents a comment until the end of the line. If a comment is used, even if the script is not used for a considerable amount of time, it can be understood in a short time how the script functions and how it works.

Execution of the shell script: sh hello.sh (assuming there is a hello.sh script).

variables: in shell programming, all variables consist of strings and do not need to be pre-declared for variables .

#!/bin/sh

#set variable A

A=8

#print A

echo "A is:"

Echo $a

Attention to thinking:

Example 1:

A=8

What does echo $ABC output? Why?

A=8

What does echo ${A}BC output? Why?

Example 2:

A=8

What does echo "$A" output? Why?

A=8

Echo ' $A ' output What? Why?

" $A " will be parsed as a variable, output 8, ' $A ' output $ A;

Default variables:

$#: The number of command-line arguments passed in the script

$*: All command line parameter values, with spaces between individual parameter values

$: Command itself (shell file name)

$: First command-line argument

$: Second command-line argument

Local variables:

Add the local keyword when a variable is first assigned to a value

1. When assigning a variable, "=" cannot have spaces on either side

2.A semicolon is not required at the end of a statement in BASH

If statement

if [expression]

Then

#code Block

Fi

if [expression]

Then

#code Block

Else

#code Block
Fi

if [expression]

Then

#code Block

elif [Expression]

Then

#code Block

Else

#code Block

Fi

if [expression]; Then

#code Block

elif [expression]; Then

#code Block

Else

#code Block

Fi

Comparison:

Compare Operations integer Manipulation string Operation

same -eq = 

different -ne  != 

Greater than -GT >

less than -lt <

Greater than or equal to-ge

Less than or equal to-le

is empty - Z

is not empty - N

Compare integers A and b for equality: if [$a = $b] (eq also available)

Determine if integer A is greater than integer b:if [$a-gt $b]

Compare strings A and b for equality: if [$a = $b]

Determine if string A is empty: if [-Z $a]

Determine if integer variable A is greater than b:if [$a-gt $b]

1. Leave a space around the "[" and "]" symbols

2. "=" There are spaces around

- e file already exists

- F files are normal files

- S file size is not zero

- D file is a directory

- R files can be read to the current user

- W files can be written to the current user

- x files can be executed for the current user

Example S5:

#!/bin/sh

Folder=/home

[-R "$folder"] && echo "can read $folder"

[-F "$folder"] | | echo "This was not file"

For loop

The For loop structure differs from the C language in that the basic structure of the For loop in BASH is:

for var in [list]

Do

#code Block

Done

Where $var is the loop control variable, [list] is a collection that Var needs to traverse, Do/done contains the loop body, which is equivalent to a pair of curly braces in the C language. In addition, if do and for are written on the same line, you must precede do with ";". Such as:

For $var in [list]; Do...done

#!/bin/bash

For day in Sun Mon Tue Wed Thu Fri Sat

Do

Echo $day

Done

If the list is contained in a pair of double quotation marks, it is considered an element, such as:

#!/bin/bash

For day in "Sun Mon Tue Wed Thu Fri Sat"

Do echo $day done

In the example above, the variable day is not marked with a "$" in the for row, while in the loop the Echo's row variable is marked with the "$" symbol on the day of the day.

While loop

The basic structure of the while loop is:

while [condition]

Do

#code Block

Done

Untill cycle

The basic structure of the Until loop is:

until [condition]

Do

#code Block

Done

The difference between while and until is that while is true, the until is executed for false

Case statement

The case structure in BASH is similar to the function of a switch statement in C, and can be used for multiple branch control.

Case ' $var ' in

Condition1)

;;

Condition2)

;;

* )

Default statments;;

Esac

Example:

#!/bin/bash

echo "hit a key and then hit return."

Read Keypress

Case ' $Keypress ' in

[[: Upper:]]) Echo "uppercase letter";;

[[: Lower:]]) Echo "lowercase letter";;

[0-9])echo"Digit";;

* )  echo "punctuation, whitespace, or other";;

Esac

Features of the Bash shell

1. command Memory capability

2. Use the TAB key to complete the command and file completion function

3. Set the alias of the command (Eg:alias lm='ls-l')

4. operation control, foreground, background control

5. Program script (shell script)

6. wildcard characters supported

Shell Scripting Learning

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.