Five methods for assigning values to shell scripts

Source: Internet
Author: User

1. Variable type in Shell

Shell variables can be divided into environment variables, location variables, pre-defined special variables, User-Defined variables,

<! -- [If! Supportlists] --> 2. <! -- [Endif] --> environment variable

Environment variables in shell are shell predefined variables used to set the system runtime environment. environment variables are named by the system. The value of some system variables is set by the system, and the value of some environment variables can be specified by the user.

The environment variable name consists of uppercase letters. Common shell environment variables are as follows:

 

Home: the full path name of the user's home directory. CD $ home can be switched to the user's home directory.

Path: similar to the path in windows, shell searches for binary executable files in sequence.

Echo $ path can display the current path. Add the new path method $ Path = $ path: New Path

Term: terminal type echo $ term

PS1: prompt. The default value is # for root users and $ for common users.

Shell: absolute path of the shell interpreter

LOGNAME: username of the login user

UID: uid echo $ uid of the current user

<! -- [If! Supportlists] --> 3. <! -- [Endif] --> location variable

Location variables are determined based on the location of the parameters that appear on the command line. In the command line that calls the shell program, the parameter location is defined as follows.

$ Command parameter 1 parameter 2 parameter 3

Where $0 corresponds to the name of the executed command

$1 corresponds to parameter 1

$2 corresponds to parameter 2

$3 corresponds to parameter 3

(Be sure to clarify the order !!)

<! -- [If! Supportlists] --> 4. <! -- [Endif] --> predefined special Variables

Predefined special variables have special meanings and cannot be changed. All predefined variables are composed of the "$" symbol and another symbol. The common predefined special variables are as follows:

$ #: Number of location parameters (excluding shell script names)

$ *: A string composed of location parameters

$! : Process number corresponding to the previous background command

$? : The exit status of the previous command, which is a decimal number. If the return value is 0, the execution is successful.

$: Current process ID PID

<! -- [If! Supportlists] --> 5. <! -- [Endif] --> User-Defined variables (Case Sensitive)

Requirement: The variable name must start with a letter or underline followed by any number of letters, numbers, and underscores.

Valid:

_ Name size my_idea

Invalid:

2god! Name Hello & World

<! -- [If! Supportlists] --> 6. <! -- [Endif] --> variable Creation

Two built-in commands declare and typeset can be used to create variables. You can also set the variable creator by setting the options of the command.

In addition to using built-in commands to create and set variables, you can also directly assign values in the following format:

Variable name = variable value

Note: The variable name should not be preceded by the dollar "$" symbol. (Unlike PHP)

There cannot be spaces before and after the equal sign "=.

Unlike C, shell does not require explicit syntax to declare variables.

Variable names cannot be directly connected to other characters. to connect, you must use parentheses: Echo "This is $ (HE) llo !"

<! -- [If! Supportlists] --> 7. <! -- [Endif] --> delete a variable

The system provides the unset command to delete variables, such

Unset name

<! -- [If! Supportlists] --> 8. <! -- [Endif] --> variable assignment

There are five values assigned to variables: read command, direct value assignment, command line parameters, command line output results, read from the file.

Let's start with the READ command :(Mainly used for Interaction)

The read command is a built-in system command in the syntax format:

Read variable 1 variable 2

When the shell script runs the READ command, the execution of the script will be paused and the keyboard input will be waited. After the user completes the input and press enter, the assignment will be completed and the script continues to be executed.

READ command rules:

<! -- [If! Supportlists] --> L <! -- [Endif] --> multiple data or variables are separated by spaces.

<! -- [If! Supportlists] --> L <! -- [Endif] --> if the number of variables is equal to the number of data, the corresponding value

<! -- [If! Supportlists] --> L <! -- [Endif] --> if the number of variables is greater than the number of data, null values are obtained for variables without data input.

<! -- [If! Supportlists] --> L <! -- [Endif] --> if the number of variables is smaller than the number of data, the excess data is assigned to the last variable (that is, the space is treated as a string)

Example:

#! /Bin/bash

Echo-N "Please enter your name" //-N indicates the same line of user input and prompt information

Read name

Echo "Your name is $ name"

#! /Bin/bash

Echo "Please enter your first name and last name :"

Read first last

Echo "your first name is $ first"

Echo "your last name is $ last"

The second method is to directly assign values to variables (This assignment method is used when no interaction is required and the parameter does not need to be modified.)

Example: name = Xiaosu

Gender = man

The third method is to assign values using the command line parameter. (This assignment method is used when parameters need to change frequently and do not need to interact.)

This value assignment method is directly followed by a parameter after the command, and then the system uses $1 to reference the first parameter.

The fourth method is to assign values using the output result of the command (This assignment method can directly process the data generated by the previous command.)

In Shell programs, the output result of a command can be used as a variable, but it must be used in the value assignment statement.Reverse quotation marks

For example, currentdir = 'pwd'

Echo $ currentdir

The last assignment method is to read data from a file.

This method is suitable for processing large volumes of data, Directly Writing the corresponding data to a file, and then running the script.

Generally, a row of data is read through a while loop, that is, a row of data is read from the file until the end of the file is read.

For example:

#! /Bin/bash

Ls *. Sh> execfile

While read line

Do

Chmod A + x $ line

Done <execfile

This script uses the while... do... done structure, and the last line indicates reading data from the file execfile. While read line indicates that a row of data is read cyclically and assigned to the line variable.

<! -- [If! Supportlists] --> 9. <! -- [Endif] --> variable output

The simplest method is to use echo

To output formatted strings, you need to use printf, which is similar to the C language.

Format description of the printf command

\ "Output double quotation marks

\ Output backslash

\ A bell

\ N line feed

\ R press ENTER

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.