Shell Learning Notes

Source: Internet
Author: User

I. Shell INTRODUCTION

The shell is a command-line interpreter for Unix/linux, and a series of commands executed by the user in the shell are executed directly by the shell command-line interpreter. Modern, popular command-line interpretation features have been built into the Linux Shell for support such as wildcards, pipelines, variables, conditional actions, commands, and help files. Simply put, we can understand the shell as an interface for users to interact with the Linux operating system.

There are many types of shell in Linux, common: Bourne shell (/usr/bin/sh or/bin/sh), Bourne Again Shell (/bin/bash), C Shell (/usr/bin/csh), K Shell ( /usr/bin/ksh), Shell for Root (/sbin/sh), and so on. Different shell language syntax differs, so it cannot be exchanged for use. Each shell has its own characteristics, and basically, mastering either of these is enough. In this article, we focus on bash, the Bourne Again Shell, which is widely used in daily work due to ease of use and free, and bash is the default shell for most Linux systems. So, in the following text, the bash shell is the example.

Two. Shell variables and special variables

Variables are an essential part of any programming language, and variables are used to store various data. The scripting language usually does not need to specify the type when defining the variable, it can be assigned directly, and the Shell variable follows this rule.

In the Bash shell, the value of each variable is a string, and the value is stored as a string, regardless of whether you use quotation marks when assigning a value to a variable.

This means that the Bash shell does not differentiate between variable types by default, and even if you assign integers and decimals to variables, they are also treated as strings, which differs from most programming languages.

Shell programming for Linux is a very mature programming language that supports various types of variables. There are three main types of variables: environment variables, internal variables, and user variables.
Environment variables (environment variable) are part of the system environment and do not have to define them. They can be used in shell programs, and certain variables, such as path, can also be modified in the shell.
The internal variable (built-in variable) is provided by the system. Unlike environment variables, you cannot modify them.
The user variable is defined when you write a shell script. You can use them and modify them in a shell program.

1. User Variables

? variable assignment

In shell programming, the use of user variables requires no prior declaration, and the name of the variable names follows the following rules:

1). The first character must be a letter (a-z,a-z);
2). Cannot have spaces in the middle, you can use the underscore (_);
3). Punctuation cannot be used;
4). You cannot use the keywords in bash (you can view the reserved keywords with the help command).

The format of the variable assignment:

Variable name = value

such as: VAR1=ABC

? variable reference

When referencing a variable, precede the variable name with $, such as:

#!/bin/bashvar1=abcecho $var1
2. Environment variables

Bash Shell uses a feature called environment variable (environment variable) to store information about the shell session and the work environment (which is why they are called environment variables). This feature allows you to store data in memory so that scripts running in programs or shells can easily access them. This is also an easy way to store persistent data.

3. Special variables
    • $ A: The file name of the current script
    • $num: Num is a 1-based number, and $ is the first parameter,\ (2 is the second argument, \){10} is the tenth parameter
    • $#: Number of arguments passed in the script
    • $*: All positional parameters (as a single string)
    • [Email protected]: all positional parameters (each as a separate string).
    • \ (?: The return value of the previous command in the current shell process, if the previous command executed successfully \)? The value is 0, otherwise the other non-0 value, commonly used to do if statement conditions
    • $$: pid of the current shell process
    • $!: PID of the last process running in the background
    • $-: Shows the current options used by the shell
    • $_: Last parameter of previous command
Three. Shell input and output and redirection

The Linux command obtains the input from the standard input device (stdin) By default, outputting the result to the standard output device (STDOUT) display. In general, the standard input device is the keyboard, the standard output device is the terminal, that is, the display. When a Linux shell executes a command, each process is associated with three open files and uses file descriptors to refer to them. Because the file descriptor is not easy to remember, the shell also gives the corresponding file name:

Standard input 0 defaults to keyboard; 0 is the output of a file or other command

Standard output 1 defaults to screen; 1 for file

Error Output 2 defaults to screen; 2 for file

Command > file is generally used to write command commands output to files

        command < file 将文件重定向给command命令        command >> file 将输出重定向给文件        1 > file  将标准输出重定向文件        2 > file  将错误输出重定向到文件        2>&1    将错误和标准输出合并        2>/dev/null  将错误输出丢掉不显示
Four. Shell (), (()), [],[[]],{}, ', ' "different applications
The use of various parentheses in #!/bin/bash#bash # () is used for command substitution, assigning the return value of the command to a variable, equivalent to ' var1=$ (ifconfig |grep inet |awk ' {print $} ' |head-1) #上面的赋值相当于 var= ' ls ' var2=2var3=3# can also be used for array definitions array= (1 2 3 4) echo "var1 = $var 1" echo "VAR2 = $var 2" echo "VAR3 = $var 3" echo "array = ${arra  Y[@]} "# (()) is used for mathematical calculations, such as the comparison and calculation of integers, belonging to the C language style var_sum=$ ((var2 + var3)) echo" var2 + var3 = $var _sum "if (($var 2! = $var 3)); Thenecho "Var2! = Var3" Fiif (($var 2 > $var 3); Thenecho "Var2 > Var3" Else echo "Var2 < VAR3" fi#["can also be used for integer calculations # can also be used for integers and characters string comparison, which belongs to Shell style #也用于数组的索引, such as array[0] var_sum1=$[$var 2 + $var 3] echo "var2 + var3 = $var _sum1" [$var 2-lt $var 3] && E Cho "Var2 < Var3" | | echo "VAR2 is not less than Var3" echo "array array No. 0 subscript value is: ${array[0]}" #[[] "is used for conditional judgment, compared to [] more general, the comparison operator used differs if [[$var 2-GT 0 && $    VAR3-GT $var 2]];then echo "var2 > 0 and var3 > var2" fiif [$var 2-gt 0] && [$var 3-gt $var 2];then     echo "var2 > 0 and var3 > var2" fiif [$var 2-gt 0-a $var 3-gt $var 2];then echo "var2 > 0 and var3 > var2" fi #{} for expansion,For a regular file, touch {a,b,c,d}.txt# can be used to identify the variable echo "${var1}:is IP" #可用于函数代码块包围function sum () {total=0 for (i=0;i<=100;i++) Do (total=i+total)-Done echo $total}sum# can be used for string interception and replacement Str=/etc/passwdeecho ${str:3}echo ${str: -6}echo ${ STR: ( -6)}echo ${str:0:7}echo ${str/e/k}echo ${str//e/k} #可用于多条命令执行, such as: {cmd1;cmd2;cmd3;} Executes the command {Ls;free;ps;} in the current shell order; # "The content within the WYSIWYG echo ' var2= $var 2 ' #" will explain the special symbol in echo "var2= $var 2"

Shell Learning notes

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.