Linux Beginner: A simple shell script

Source: Internet
Author: User

What is a shell?

The shell itself is a C language program, it is the user use Unix/linux Bridge, most of the user's work is done through the shell

The shell has two ways of executing commands:

Interactive (Interactive): Explains the execution of a user's command, the user enters a command, and the shell interprets the execution of a single rule.
Batch: The user writes a shell script in advance, with a number of commands that allow the shell to execute the commands at once without having to hit the command one at a time.
First script:

VI hello.sh General shell scripts end with. sh

#!/bin/bashecho'helloworld'//  #! is a convention marker, Tells the system which interpreter the script uses to execute the echo command to output text to the window

./hello.sh, you can print the Hello World string.

Let's write a script that starts MySQL:

VI mysql_start.sh

#!/bin/bashpid= 'PS -ef| grep mysql| grep grep| awk ' {print $} ' the symbol after the '//= sign is the symbol on the top of the Left Tab key, not the single quotation mark, do not add the wrong "two symbols between the command, the variable name and ' = ' after the space //Ps-ef|grep MySQL is used to query the process of MySQL, grep-v grep is used to mask the process of grep, awk used to fetch the query results in the second column of the value, that is, process number kill -9 $pid // kill-9 used to kill the process, $pid is used to take the variable value, to add the $ symbol

As above, a script to start MySQL is finished, we only need to run mysql_start.sh This script can start MySQL automatically

Assuming that MySQL is started and the script is running again, we need to include an if judgment in the shell script.

#!/bin/Bashpid=`PS-ef|grepmysql|grep-Vgrep|awk '{print $}'`if[$pid]//The if statement starts, [] the condition is in parentheses, there must be a space before and after [] Then         //then the structure of the IF statement, and the content after then when the condition satisfiesKill-9$pidElse        //What to do if the condition is not satisfied after elseEcho 'MySQL started'fi           //fi represents the end of the IF statement, it must be added, or the script will be run error

Another situation is that the PS query process has many, then how do we kill the process, then we need to use the Xargs command

#!/bin/Bashpid=`PS-ef|greplampp|grep-Vgrep|WC-l 'if[$pid-ge2]//-ge is one of the shell's functions, equivalent to "greater than equals", the shell can only use shell-specific operators, you can view the detailed list, which means that the query to the LAMPP process row number is greater than 2 o'clock to do then the content Then        PS-ef|greplampp|grep-Vgrep|awk '{print $}'|Xargs Kill-9//in the above statement, the meaning of Xargs: all the process numbers that awk takes are passed to kill-9, killing all processesElse       Echo 'Lampp stopped'fi          

So, some of the commands that are commonly used in the shell are to be remembered.

Linux Beginner: A simple shell script

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.