Linux shell script Programming (i)

Source: Internet
Author: User

Tags: variable quotes in bash environment variable configuration add info no permissions Independent lock

What is a shell?

Shell is the command interpreter, is the UNIX operating system user interface, the program from the user interface to get input information, the shell of the user program and its input translated into the operating system kernel (kernel) can recognize the instructions, and the operating system kernel finishes executing the output returned by the shell to the user, as shown in the relationship between the user, Shell, and operating system:

The shell is also a programming language, shell script, the shell is the scripting language to explain execution, you can call the Linux command directly: Java. class

A system can have multiple shells, and you can view the shells installed in the system with the Cat/etc/shells command, which may be different from the command syntax that the shell might support.

Shell types

The operating system kernel (kernel) is a separate suite from the shell and can be replaced. different operating systems use different shells, and a different shell can be used on the same kernel.

The common shell is divided into two major mainstream:

SH:

Bourne Shell (SH), solaris,hpux default shell

Bourne again Shell (bash), Linux system default shell

csh:

C Shell (CSH)

TC Shell (TCSH)

View using the shell

Shell Environment definitionTEMP environment variable

The so-called temporary variable refers to the user in the current login environment in effect of the variable, the user login system, directly on the command line defined by the environment variables can only be used in the current landing environment. When you exit the system, the environment variable will not continue to be used the next time you log on.

to permanently take an environment variable into effect

By writing an environment variable definition to a configuration file, the system is automatically defined each time the user logs on, and no more needs to be redefined at the command line. The common configuration files that define environment variables are as follows:

/etc/profile for all users of the system, this file is applied to all user's environment variable definitions each time they log on to the system.

$HOME _name/.bash_profile for a specific user, $HOME as the user's host directory, when the user logs on to the system, first inherit the definition in the/etc/profile file,

Then apply the definitions in the $home/.bash_profile file.

system pre-defined environment variables

The system environment variables are valid for all users, such as: $PATH, $HOME, $SHELL, $PWD, and so on, using the echo command to print the above system environment variables:

Shell script Programming

Like traditional programming languages, the shell provides a number of features that make your shell scripting more useful.

Create a shell script

A shell script typically contains the following sections:

First line

The first line is to the left of the first line of the script and represents the shell interpreter that the script will invoke, as follows:

#!/bin/bash

#! The symbol can be identified by the kernel as the beginning of a script, which must be in the first line of the script, and/bin/bash is the absolute path to the bash program, where the subsequent content will be interpreted by the bash program.

   Notes

Note Symbol # is placed before the content you want to comment on, as follows:

content

   Executable content and Shell structure

permissions for shell scripts

In general, scripts created by default do not have Execute permissions.

No permissions are allowed to execute, you need to give executable permissions.

execution of shell scripts1, enter the absolute path or relative path of the script

/root/helloworld.sh

./helloworld.sh

2,bash or sh + script

bash/root/helloworld.sh

SH helloworld.sh

Note: When the script does not have X permissions, the root and file owner can execute it normally.

   3, add "before the path of the script." "or source

source/root/helloworld.sh

. ./helloworld.sh

Difference: The first and second types will open a new bash, and the variables in different bash cannot be shared. but use. ./script. Sh This method is executed in the same shell.

Shell variables

Variable: is a way for the shell to pass data that represents the symbolic name of each value. When a shell script needs to save some information, such as a file name or a number, it is stored in a variable.

variable setting rules:

1, the variable name can be composed of letters, numbers and underscores, but cannot start with a number, the environment variable name is recommended capitalization, easy to distinguish.

2, in bash, the default type of a variable is a string type, and if you want to perform a numeric operation, you must specify the variable type as numeric.

3, the variable is connected with an equal value, and the left and right sides cannot have spaces.

4, the value of the variable if there are spaces, you need to use single or double quotation marks included.

Variable Classification

the variables in the Linux shell are divided into user-defined variables, environment variables, positional parameter variables, and predefined variables. All variables present in the system can be viewed through the SET command.

System variables: Saves data related to the operating environment of the system. $HOME, $PWD, $SHELL, $USER, etc.

Positional parameter variables: used primarily to pass parameters or data to a script, variable names cannot be customized, and variable roles are fixed.

Predefined variables: Variables that are already defined in bash, variable names cannot be customized, and variable functions are fixed.

User-defined variables

A user-defined variable starts with a letter or underscore, consists of a sequence of letters, numbers, or underscores, and has different uppercase and lowercase meanings, with no limit to the length of the variable name.

Setting Variables

It is customary to use uppercase letters to name variables. The variable name begins with a letter-based character and cannot be used as a number.

variable Invocation

When using a variable, precede the variable name with the prefix "$".

  Use the echo command to view variable values. Eg:echo $A

assigning values to variables

1, assign value when defining:

variable = value cannot have spaces on either side of the equals sign

eg

str= "Hello World"

a=9  

2. Assign a command execution result to a variable

A= ' Ls-la ' counter quote, run the command inside and return the result to the variable a

a=$ (Ls-la) equivalent to anti-quote

eg:aa=$ ((4+5))

bb= ' Expr 4 + 5 '

3, assign one variable to another

Eg:a= $STR

Variable Overlay

Eg: #aa =123

eg: #cc = "$AA" 456

Eg: #dd =${aa}789

The difference between single and double quotation marks:

Phenomenon: The contents of single quotes are all output, and the contents in double quotes change

Cause: Single quotation marks will remove all special characters

num=10

sum= "$NUM hehe" echo $SUM output hehe

sum2= ' $NUM hehe ' echo $SUM 2 output $num hehe

list all the variables

Set

Delete a variable

Unset NAME

Eg:

# unset a undo variable a

# readonly b=2 declares a static variable b=2 and cannot unset

A user-defined variable scoped to the current shell environment.

Environment variables

The user-defined variable takes effect only in the current shell, and the environment variable takes effect in the current shell and all of its child shells. If the environment variable is written to the appropriate configuration file, the environment variable will take effect in all shells.

Export variable name = variable Value declaration variable

Scope: The current shell and all child shells.

Positional parameter variables

$n

< P align= "left" >n is a number, and $ A represents the command itself, $1-$9 represents the first to the 9th parameter,

10 the parameters need to be enclosed in curly braces, such as ${10}.

$*

represents all parameters in the command line and regards all parameters as a whole. Output all parameters in the form of "$ $ $n"

[ Email protected]

Represents all parameters in the command line and treats each parameter as distinct. With "$" and "$" ... Output all parameters in the form of "$n"  

$#< /span>

represents the number of parameters in the command line. Number of arguments added to the shell

Shift instruction: Parameter left, each execution, the parameter sequence left one position, the value of $# minus 1, used to process each parameter separately, the removed parameter is no longer available

$* and [email protected] the difference

1,$* and [email protected] All represent all parameters passed to a function or script, and are not enclosed by double quotes "", with "$" and "$" ... Output all parameters in the form of "$n";

2, when they are enclosed in double quotation marks, "$*" takes all parameters as a whole and outputs all parameters in the form of "$".

3, "[email protected]" will separate the various parameters to "$" "$" ... All parameters are output in the form "$n".

  

Pre-defined variables

$?

Execution of the return value of the previous command succeeds, returns 0, execution fails, return not 0 (the specific number is determined by the command)

$$

Process number (PID) of the current process, which is the process number generated when the current script executes

$!

Process number (PID) of the last process running in the background, most recently a process that was put into the background execution &

Read command

Read [option] Value

READ-P (hint statement)-N (number of characters)-T (wait time, in seconds) –s (hidden input)

eg

Read–t 30–p "Please input your name:" Name

Echo $NAME

Read–s–p "Please input your Age:" Age

Echo $AGE

Read–n 1–p "Please input your sex [m/f]:" GENDER

Echo $GENDER

Linux shell script Programming (i)

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.

Tags Index: