11 Simple shell programs for Linux system learning notes

Source: Internet
Author: User
Tags arithmetic echo command first string

Do not look at notes, for a long time do not have to forget, or you have to look at the notes often ah.

A simple shell program

Shell Structure 1, #! specifies the shell2, #注释行3, commands, and control structures that execute the script. Step one: Create a file that contains commands and control structures The second step: Modify the permissions of this file to make it executable.using chmod u+xStep three: Execute the shellsh/test/example.sh shell variable variable: is a shell to pass the data of a method, used to represent each value of the symbol name shell has two types of variables: temporary variables and permanent variable temporary variable is inside the shell program defined, its scope of use is limited to define its program, other programs are not visible. Includes   user-defined variables, position variables. A permanent variable is an environment variable whose value does not disappear with the execution of the shell script.    user-defined variables user-defined variables are preceded by a letter or underscore, consisting of a sequence of letters, numbers, or underscores, and the uppercase and lowercase meanings are not   identical, and the length of the permanent variable is unlimited. When using variable values, precede the variable name with the prefix "$"    set and use variables to set the variable: it is customary to name the variable in uppercase letters, the variable name can only begin with the characters in the alphabet, and cannot be used in numbers. Variable Assignment: Assignment Number "=" There should be no space timing assignment, num=1 the execution result of a command to the variable, such as: time= ' Date ' assigns a variable to another variable, such as: a= $B use the echo command to view the value of the variable. For example: The echo $A lists all variables: Set contains variables with multiple words $name=mike Ron runs with an error: $NAME = "Mike Ron" or $name= ' Mike Ron ' the difference between single and double quotes is that the variables in single quotes are not parsed. Delete variables: unset name  positional variables and special variables when the shell interprets the Execute user command, the first part of the command line is used as the command name, and the other part as the parameter. The parameters that are determined by the position that appears on the   command line are called positional parameters. For example: Ls-l file1 file2 file3$0 The file name of this program ls-l$n the nth parameter value of this program,n=1--9  special variable $* All parameters of this program $ #这个程序参数的个数 $$ This program pid$! Execute the pid$ of the previous command? The return value of the previous commands 0 for success   non 0 for unsuccessful   shell command read command: Reading data from the keyboard, assigning variables such as: Red Usernameread Example: #!/bin/ Shread First Second Thirdecho "you have entered a parameter $first" echo "The first parameter you entered $second" echo "The first parameter you entered $third"   The arithmetic operation of the expr command shell variable The expr command: arithmetic operations on integer variables such as: Expr 3 + 5   #加号左右必须有Space expr $varl -5expr $v 1/$v 2expr $v 1 \* 10 complex expr command complex operation  expr ' expr 5 + 7 '/$var 1 assigns the result of the operation to the variable: var4= ' expr $va 1/$v Ar2 ' Example: #!/bin/sha=10b=20c=30v1= ' expr $a + $b + $c ' echo $v 1v2= ' expr $c/$b ' echo $v 2v3= ' $a \* $b ' v4= ' expr $a + $c/$b ' echo $V 4  variable TEST statement variable Test statement: User test variables are equal, empty, file type, etc. format: test test Condition: Integer, String, file string test: Tests STR1=STR2     Test if the string is equal test str1!=str2     test string is not equal to testing str1    test if the string is not empty Test-n str1       test string is not empty test-z str1       test string is empty integer test: Test init1-eq int2   testing integer is equal to test init1-ge int2   test int1 whether >=int2test init1-gt int2   test int1 >int2test init1-le int2   Test Int1 Yes No <=int2test init1-lt int2   test int1 whether <int2test init1-ne int2   Test integer is not equal file test test-d  file   specified file is No directory test-f  file   Specifies whether a file is a regular file test-x  file   Specifies whether the file can be executed Test-r  file   Specifies whether the file is readable test-w   File   Specify whether the files are writable test-a  file   Specify whether the file exists test-s  file   Specify whether the file is not a 0 variable test statement is generally not used alone, generally as a test condition for an if statement, such as: if test-d the then.....fi variable test statement is available [] for simplification, such as test-d equivalent to [-D $1]      #!/bin/sh# A program that compares two strings for equality read-p "Please enter the first string:" Str1read-p "Please enter the second string:" Str2  if [-n $str 1]; Thenecho "The first string you entered is not NULL, the value is:" $str 1elseecho "the first string you entered is null" fi if [-n $str 2]; Thenecho "The second string you entered is not NULL, the value is:" $str 2elseecho "the second string you entered is empty" fi if Test $str 1 = $str 2; Thenecho "Two strings equal" ElSeecho "Two strings Unequal" fi  1, view the current system's environment variables and the variables you define SET command 2, delete a variable unset variable name for example: unset name

11 Simple shell programs for Linux system 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.