Shell Scripting Learning Notes-variable usage and input and output

Source: Internet
Author: User
Tags echo command ftp

1. Definition of variables

1.1 Variable names can consist of letters, numbers, underscores, but numbers cannot be the first character of a variable name.

1.2 Define the value of the variable by assigning the symbol "=", e.g:

Myname= ' Test ', String type, does not parse any characters

Myname= "ABCD", double quotes inside will parse $ and backslash special characters

num=100; price=1.2345

Now_date= ' Date ', the anti-quote executes the system command.

2. References to variables

2.1 Use the dollar sign $ plus variable name to refer to the value of a variable, e.g:

Define variable: test = "Hello demo", Output variable: Echo $test

The curly braces outside the 2.2 variable name are primarily intended to help the interpreter identify the bounds of the variable, e.g.:

Language = ' Java ' echo ' I am a ${language}coder ', in which case the interpreter would mistakenly assume that it was drinking the value of the Lanuagecoder variable without the curly braces, causing an error.

3. Simple example:


4. Environment variables

4.1 Environment variables are some of the parameters used in the operating system for the texture of the ING operating system, e.g. path, etc.

4.2 Common environment variables:

Path: System path

Home: Current User home Directory

Histsize: The number of records that saved history commands

LOGNAME: Current User logon name

Hoatname: Host Name

Shell: What kind of shell is used by the current user

LANG: Language-related environment variables

Mail: The current user's message store directory

4.3 Viewing environment variables:

ENV: Show all environment variables

5. Local Variables

5.1 User-defined variables in shell scripts: scoped to start from the defined place, until the end of the shell script, the function domain is this script, leaving this script, the variable is invalid, E. g.:

Writing a script, defining a variable, outputting the result, exiting the script, and then outputting the variable will not see the result:


5.2 Variables defined in the shell of the user login, the variable disappears after re-login, e.g.


6. Pre-defined variables

6.1 Predefined variables are similar to environment variables and are variables defined at the beginning of the shell. The difference is that the user can only use these variables according to the Shell's definition, and not redefine it. All of the predefined variables are made up of a $ character and another symbol.

6.2 Common shell Pre-defined variables:

$#: Number of positional parameters

$*: Contents of all positional parameters

$?: Status returned after command execution, 0 for success, not 0 for failure

$$: Process number of the current process

$!: The last process number running in the background

$: The currently executing process name

6.2 Simple Example:


7. Output and input

The 7.1 echo command sends data to a standard output device, where the data is in string mode with two important parameters in Echo:

-E: Identify the escape sequence in the output, e.g. echo-e "Hello\tworld"

-N: Ignore end of line break, E.G:ECHO-E-n "Hello\tworld"

7.2 printf commands are similar to Echo's, printf can also output variables, and can output variables in the format, but the default output of printf does not wrap, and the newline needs to add "\ n" to the line, e.g:


The 7.3 Read command reads the next line of the standard input device, and all the characters in the standard input line before the newline character are read and assigned to the corresponding variable, E. g.:


7.4 "<<" is a redirect that redirects a line in the script file as input to a command, the operator << is placed after the command that entered the redirect, followed by the lines of the << as input to the command, the end of the input by the file terminator (Ctrl + D) indicates that you can also define the delimiter, the delimiter after the word as the end of the input lines of the delimiter, the other delimiter needs shelf write, e.g. automatically connect to the FTP server via scripting to download data:

#!/bin/sh

ftpserver=127.0.0.1
ftp-i-N <<!  #。 For the delimiter
open $ftpserver
usr account password #具体的操作账号和密码
cd testdir
bin
get test.txt
bye
!
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.