Linux op-dimensional notes----Bash Scripts Foundation Ii__linux

Source: Internet
Author: User
Tags case statement logical operators
Bash Scripts Foundation II 1.Bash Position Parameters
There are two simple ways to read user input into a variable in bash.
-Use Read to prompt the user for input (using the-P option) and store it directly to one or more variables:
    Interactive input
    read-p ' Enter your-i-Last name: ' Top last
-use positional parameters Number to read the command-line arguments or option inputs passed to the script. Option number for various special variable storage delivery.
the individual parameters that bash resolves or the entire original command line.
    total number of positional parameters specified: $#
    position parameter itself:  $, $, $,
    $ .... All positional parameters: $@, $*


2. Exit Status

When the Linux command completes, the exit status is returned. When the program completes successfully, it returns a 0 exit status. This is considered by bash as a logical true value. A non-0 exit state usually indicates an error occurred and is considered as a logical false value by bash.

The value of the eject state is stored in the "?" , you can use the following command to view:

echo $?

For example: The meaning of the exit status of Grep:

0– found mode in specified file
>1– Some other errors (unable to open the file, incorrect search expression, and so on) are not found in the specified file

<1> Exit successfully

<2> Error exits

3.test Conditional Judgment

The test command can be used to evaluate an expression in a bash script. It evaluates the expression specified by its parameters, returns a 0 exit state if the expression is true, and returns a non-0 exit state if the expression is false. Test has an alternative syntax, which is easier to read by enclosing the expression with the square brackets "[]".
syntax: Test EXPRESSION or [EXPRESSION]

<1> non 0 or 0 length string operator: Test-{n|z} string
Practice: Use interactive login, when user input add then create a user, enter del Delete a user.


<2> string comparison operator: =,!=

<3> Numeric comparison operators:-eq (equals),-ne (not equal to),-lt (less than),-le (less than equal),-GT (greater than),-ge (greater than or equal)

<4> File status operator:

Test-{b|c|e|f|d|r|w|x|s| L} file/directory-
b  block device file-           C  character special file-
e  file Presence-                F  Normal file-
D directory-                  R Readable  file-
w writable  file-                x  executable
-s  file not null-           l  link file


<5> binary file operators:-ef,-nt,-ot

File1   -ef   File2     Two files point to the same device and node number
File1-nt File2 File1     is newer than File2: File1 exists, File2 does not exist
File1   -ot   File2 File1 older than File2; File1 does not exist, File2 exists


<6> logical operators:-O,-a 、!、 &&, | |

-  A: With logic, you can use && instead of-
o: or logic, which can be replaced with | |
: Non-operational

4.if Statement

The IF command checks the exit value of the command or list after if. If the first command evaluates to true/0, the list of commands after then is run until either else. If the first command evaluates to false/Non-zero, run the list of commands between else and fi (reverse equal write if, mark the end of the If block).
syntax:
if command; then command; command2; else Command3; fi

Exercise: Decide whether a file is a link or a directory


Practice: Increase deletion of user improvements. Can detect whether an increased user exists

5.case Statement

Case statement: It can match the contents of a variable with multiple templates, and then decide which part of the code should be executed based on a successful matching template.

Exercise: Enter the Apple output Banan instead.

6.expect Statement

Use expect to implement automatic answer scripts in the shell.
!/usr/bin/expect
This line tells the code in the operating system script to use that Shell to execute. Set

timeout
sets the timeout for waiting responses for all subsequent expect commands, in seconds. The

spawn talk
spawn is the expect internal command that adds a shell to the back shell instruction, which is used to pass interactive instructions.

expect "who" to
determine whether the last output of the "who" string, if there is a return immediately, otherwise wait for the timeout time to return.

send "westos\n"
performs interactive actions, equivalent to manually entering "Westos". The

expect EOF
function is to search for the end of the file in the output, and if not, the script exits immediately and does not get the correct result.

interact
after execution to maintain the interactive state, control power to the console, this time can be manually operated. Otherwise log out.

$argv parameter Array
expect script can accept arguments passed from bash. You can use [lindex $argv n] to get, n starting from 0, respectively, the first, the second, the third .... Parameters.

Practice: Automatically answer SSH login
7. Environment Variables

The shell and the script use variables to store the data, and some variables can be passed along with their contents to the child processes, which we call environment variables.

Three ways to set environment variables:
<1> is set in the shell, it works for the current shell, and a shell is not available.

<2> Modify/etc/profile files, all users have the right to use this environment variable, may be involved in some security issues.
At the end of the file add: $PATH = "New_path"
Execute after add: Source/etc/profile reload File
<3> Modify the ~/._profile file, this method can control the use of environment variables to the user level.
Using aliases
The alias command can be written to the ~/.BASHRC file for permanent use, using a system command from which it is defined.
View Alias: Alias

To set an alias:

Working with functions

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.