Linux Shell Basics

Source: Internet
Author: User
Tags case statement echo command

Category: Cat/etc/shells View system-supported shells

Ash consumes the least resources of the shell with only 24 internal commands.

The shell that bash Linux uses by default. You can use the Doskey feature. Can be auto-complete. Self-help group documentation.

Ksh: Very powerful and supports very complex syntax. You can write the game directly.

CSH: The larger shell, conforms to the C language syntax, basically covers the C language function. The shell points to/bin/tcsh. Which means CSH is tcsh.

Zch: One of the largest shells. There are 84 internal commands.

Set to view all environment variables

The user's shell information is saved in/etc/passwd. So to specify a new shell for the user, the most essential thing is to modify the file.

Modify the user's shell:1, usermod-s shellname username such as: Usermod-s/bin/ksh xx1 2, chsh-s shellname username

Shell variables: Similar to Windows registry. The role of the variables: environment variables (global, can be seen regardless of who logged in), local variables (the current reply is valid, exit the session, will disappear). View with the SET command.

When a command is entered, it is searched in the following directory.

The style of the command prompt. \u the currently logged on user \h the currently logged on host \w The current directory is located

Define variables: abc=1234 or export abc=1234. When = There are spaces on both sides, you must enclose them in quotation marks.

Create a shell

VI Test

#!/bin/bash comment, which Shell to execute.

Cal 1 2017

Date

W

Save to exit

Execute sh./test

chmod a+x./test Add executable Properties

./test execution

environment variable Settings

/etc/profile is valid for all shells

/ETC/BASHRC is valid for bash shell

/user Directory/.BASHRC

/user Directory/.bash_profile valid for users

Implement the pass-through parameter./A1 $ ...

VI A1

Al 1 $

Cal 2 $

Cal 3 $

Shift command: The effect of moving the parameter to the left.

The contents of File B are

Echo $ $

Shift

Echo $ $

./b A B. C

Execution results

A b C

b C

Move the $ left into $

Loop control: For while

The contents of the file A3 are:

For I in 1 2 3

Do Cal $i $

Done

Executes./A3 2014 results for 2014 year first three months

File A5 Content

For I

Do Cal $i 2014

Done

Execute SH A5 3 5 6 results for display 2014年3、5、6月 calendar. I do not specify a specific loop machine, it will be obtained from the execution parameters.

For statement

For < loop variable > [in < loop variable value set;] Do < command list >
Done

#!/bin/bashfor I
Do Cal $i 97done

The control flow while is also a loop control structure that uses:

While < discriminant commands >
Do < command list >
Done

The until control flow uses a similar, but opposite, control flow usage. To use:

Until < discriminant commands >
Do < command list >
Done

If Else statement:

#!/bin/bash
If TEST–W/DEV/LP #检车打印机准备好了没有
Then echo Printer are ready ... #显示Printer are ready

else echo Printer is isn't ready ...

Fi

Case statement:

Case < variables > in

< string 1>) {< command manifest 1>};;

...

< string n>) {< command manifest n>};;

Esac

For example:

#!/bin/bash

Case $ in

China) Echo Beijing

USA) Echo Washington

British) Echo London

Russia) Echo Moskow

France) Echo Paris

*) echo out of my knowledge

Esac

Break and continue statements jump out of the loop

The biggest difference between break and continue is that break jumps out of the entire loop, while Contiune skips the loop's current loop step. This means that the next statement is ignored and jumps directly to the next loop.

Here text

In a shell script, you can also use the so-called "here Text" feature to embed a document in a script file as a standard input to the commands used in the script. The specific usage is:

< commands > <<!
< text content as input >
! in which "!" Symbols can also be substituted with "EOF", "END", and so on. For example, the following shell script Tel can import line phone number query: #!/bin/bash

The contents of the A7 are:

#在 <<!! Find the line that matches the input in the text between.

grep $ <<!
Huang W (020) 81833222
Zhang QF (020) 84185331
Liu T (010) 65364321
MA QA (020) 83304587
!

Generally used in installation automation. For example, to install MySQL, you will be prompted for some information, you must enter Yes or on. Yes, you can. These questions are placed in a document for automated installation.

Run

SH A7 Zhang

return results

Zhang QF (020) 84185331

Use functions directly in scripts using function names

#!/bin/bash

#function time

Time ()
{

The return value of the #取出date +%r is assigned to the TM

tm= ' Date +%r '
echo "The system time now is ${TM}"
}
echo "Now was going to the function time"

#调用函数 time
Time
echo "Back from the funtion time"

Passing a parameter function name to a function parameter 1 parameter 2

#!/bin/sh

#function Func_test

Func_test ()

{
echo "$" #注有双引号是传递给函数的参数, no double quotes are arguments when invoking a shell file

}
echo "Start function"

Func_test China #china作为参数传递给func_test
echo "End of function"

Returning a return from a function

#!/bin/sh

Func_test ()
{
If ["$" = ""];

Then

echo "None Input"

return 0
Else
echo "$"

Return 1
Fi
}
Func_test $

Get the function return value with $?

#!/bin/sh

Func_test ()
{
If ["$" = ""]; # "$" calls the function to pass in the arguments.

Then

echo "None Input"

return 0

Else

echo "$"

Return 1

Fi

}
Func_test $ #$1 is the parameter that executes the shell file passed in

If [$ = 0]; #用 $? Snaps the return value of the function execution.

Then

echo "ple check you input" else

echo "All is ok" fi

Condition test:

? Test < expressions >

Common usage is as follows:

Common test File status

-e < file name >:< file name > specified file exists.

-< file Nature > < file name: The < file nature > can be: B (block device), C (character device), d (directory), f (normal file), L (Symbolic Link), p (pipe file), R (readable file), W (writable file), S (not 0-byte file), x (executable file). This expression represents the < file name > The specified file exists and has the specified nature.

< file name 1>-nt < file name 2>: Represents < file name 1> represents a file that is newer than the file represented by the < file name 2>.

< file name 1>-ot < file name 2>: The < file name 1> represents a file that is older than the file represented by the < file name 2>.

String test

Common test string tests for < expressions > are:

-Z < "string";: string is empty.

-N < "string";: string is nonempty.

< string 1> = < string 2>: string 1 is equal to String 2.

< string 1>! = < string 2>: string 1 is not equal to string 2.

! < expressions >:< Expressions > Not set.

< expression 1>-a < expression 2>:< expression 1> and < expression 2> are also established.

< expression 1>-o < expression 2>:< expression 1> or < expression 2> established.

Numerical test

< expressions commonly used in numerical tests > are:

< numeric expression 1> < operator > < numeric expression 2>, where the,< operator > can be:

-eq: The two are equal.

-he: The two are not equal.

-lt: The former is less than the latter.

-le: The former is less than or equal to the latter.

-GT: The former is greater than the latter.

-ge: The former is greater than or equal to the latter.

< numeric expression > can be an integer or an "-l < string >" to denote the length of a string.

Expr usage

The expr command is generally used for integer values, but can also be used for strings. The General format is:

Expr argument operator argument

Expr is also a manual command-line calculator.

Add Count:

Often used in loops, use the following:

Loop=0

loop= ' Expr $loop +1 '

Numerical test:

The numerical test here is not the numerical test mentioned above, and the numerical test here is to check if the variable being measured is an integer. As below:

We have defined the variable number earlier, and now at the prompt enter the following:

Expr $number + >/dev/null 2>&1

echo $?

The system returns "0" to indicate that the number variable is an integer, and to use a non-integer variable test to see if the value returned by the system is not 0

Pattern matching:

Expr also has a pattern-matching function. You can use the expr match string regexp to calculate the number of characters in a string. “. * "meaning that any character repeats 0 or more times, as follows:

Docu=blue.doc

Expr Match $docu ". *"

8 (System return)

You can use the string match operation in expr, where the schema is used. doc extracts the file name. As in the following command:

Expr match $value "\ (. *\). doc"

Blue (System return)

The command is as follows:

TEST-E Test

echo $?

A return of 0 indicates a presence. 1 indicates that it does not exist. The echo command is not required in the script.

A6 content is as follows:

#/bin/bash

If [$1-eq 1];then

echo "\$1 is 1"

else echo "\$1 is not 1"

Fi

Execute SH a6 1 result is $1. SH A6 sdfkj result is $ not 1. The brackets are equivalent to the test

Practice:

1 Determines whether a file specified with a parameter is executable and, if not, executes

if! (Test-x a)

Then

echo Execution chmod

chmod a+x A

Else

Echo has writable permissions

Fi

Ls-l A

2 use while and until loops to achieve the effect of outputting a calendar for 9 months before a year

I=1

While [${i}-le 9];

Do

Cal ${i} $

Let I=i+1

Done

I=1

Until [${i}-GT 9];

Do

Cal ${i} $

Let I=i+1

Done

3 Use the here document to write a script to delete a specified file without answering yes or no and deleting it directly, limiting the RM-F switch

#!/bin/bash

Rm-i $ <<!

Y

!

4, write a script, you can output the year specified by the parameters, as well as a number of months in the year specified by the month calendar, the number of months is not fixed

I=1

While [${i}-le];

Do

Cal ${i} $

Let I=i+1

Done

Linux Shell Basics

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.