Shell Basic Introduction and case

Source: Internet
Author: User
Tags gz file disk usage

First, Shell Script Foundation

1. First Script

Vim first.sh

There are three ways to execute scripts, respectively:

./first.sh (need to have executable permissions)

SH first.sh

  . first.sh(or source first.sh)

2. Redirects and Pipe breaks

1) redirected output (>)

2) redirect Output (>> append)

3) REDIRECT Input <

Note: Will Jerry user's password is set to 123456

4) Error redirection (2>)

5) Mixed redirect output (&>)

For example, in an Automation script that compiles a source package, to ignore information about an operation such as Make,make install, you can direct it to an empty file/dev/null

Mount the disc first, unzip the httpd-2.2.17.tar.gz file in the CD to/usr/src, and write a script installation.

Vim httpd_install.sh

chmod +x httpd_install.sh (set executable permissions)

./httpd_install.sh (Execute script, wait to finish, view results)

6) pipe symbol (|)

First look at the lines ending with "/bin/bash"

Output only the user name and login Shell column

7) pipe symbol (|): Extract disk usage information for the root partition

second, use the shell variables

1. Define New variables

2 , a special operation for assigning values to variables

1) Double quotation marks using ("): When the contents of the assignment contain spaces

2) Single quotation mark use ('): The contents of the assignment contain $, ', \, etc. characters with special meanings

3) Anti-apostrophe Use ('): Used to assign the output of a command to a variable

4) $ () to replace the anti-apostrophe operation, you can resolve the nested problem

5) Read command: Used to prompt the user to enter information, for example:

3 , set the scope of the variable

The newly defined variables are valid only in the current shell, and if you want to enter a new shell environment, you will need to export the variables as "global variables" using the Export command.

You can also assign a value to a variable while exporting the global variable, for example:

4 , the operation of a numeric variable

Plus (+), minus (-), multiply (\*), except (/), modulo (%)

third, the special shell variables

1, environment variables (the system itself has)

Environment variable system is installed, there is no need to create, can be used directly, look at the environment variables

We can add the script directly to the $path search path, so we don't have to use it when we execute the script.

such as: first.sh script in the/root directory

2, Position variable (the system itself has)

Positional variables are also called positional parameters, using $1,$2,$3,$4.....$9 to represent

3, pre-defined variables (the system itself has)

$# (indicates the number of positional parameters in the command line)

$* (indicates the contents of all positional parameters)

$? (indicates the return state after the previous command was executed, with a return value of 0 indicating that the execution is correct, not 0 indicates an exception occurred)

$ A (indicates the name of the currently executing script or program)

For example: Write a small backup script

Iv. Comprehensive Cases

1. Experiment Objective: Remotely backup MySQL server database

2. Experimental environment:

A total of 2 MySQL servers A and B, on the B remote backup a database on a, set up Database Benet and ACCP, two hosts shut down the firewall.

(1) Create two databases on server-side A, respectively Benet and ACCP

(2) Set up a dedicated database user on server-side A, where I use the root account to grant the root account to a 192.168.1.200 connection via a remote host

(3) Back up the test on backup Host B to see if the backup was successful.

(4) Write scripts on backup Host B before you need to create a backup directory Mkdir-p/opt/beifen, and then write the script.

Vim mysqlbak.sh

(5) Set the permissions for X and execute the backup script./mysqlbak.sh

(6) View backup results

(7) Set up Scheduled Tasks

MV Mysqlbak.sh/opt/beifen

Crontab-e

v. Experimental cases (II. ):

1. Requirements Description:

Write a login Welcome script welcome.sh

Write the Service control script start, stop, status

2, realize the idea:

New script welcome.sh, reporting various system information in turn

Modify the configuration file ~/.bash_profile, call the Welcome script

Create a new script/root/bin/status, get the service name, and execute the script with the same name under the/etc/init.d/directory

First, login Welcome script welcome.sh

(1) VI welcome.sh

(2) Set permissions for X

(3) Modify the/root/.bash_profile file to use the welcome.sh script every time the root user logs on to the system.

Vi/root/.bash_profile add a line as follows

(4) Verify: Log back in with the root user to verify the output information of the Welcome script

Second, write the service control script start

(1) Create a bin directory under/root

Mkdir-p/root/bin

(2) Under/root/bin, write a script status.sh

VI status.sh

(3) test script

Article reference public number: L Baby talk about it

Shell Basic Introduction and case

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.