Shell script instance
Note: Some variables related to parameters passed to shell:
$ # Number of command line parameters
$? Return Value of the Call Command
$ Process ID of the current process
$! Process Number of the last background command
$0 the first parameter of the command line, that is, the command name
$ N the nth parameter of the command line. If you want to obtain more than 9 parameters $ {n}
$ * All command line parameters, equal to ("$1 $2 ..."
"$ @" All command line parameters, equal to ("$1" "$2 "...)
$ _ The last command line parameter of the previous command
1. Write a script to calculate the factorial of 10 in a loop.
#! /Bin/bash
# Factorialteset
Factorial = 1
# For a in 'seq 1 10'
For a in 1 2 3 4 5 6 7 8 9 10
Do
Factorial = 'expr $ factorial \ * $'
Done
Echo "10! = $ Factorial"
The above small program can achieve 10 !, Note the following:
1) the implementation method of the for loop can be either of the above. One is to directly write for a in 1 2 34 5 6 7 8 9 10, the numbers do not need to be enclosed in quotation marks. Another type is for a in 'seq 1 10', which is caused by single quotation marks. single quotation marks indicate the sentence in execution. Note that single quotation marks cannot be used, and double quotation marks cannot be used.
2) factorial = 1 during initialization, there cannot be any space around the equal sign. Be sure to pay attention.
3) factorial = 'expr $ factorial \ * $ A'. Note that this sentence should also be caused by single quotation marks. In addition, escape with a backslash before the asterisk. The expr command is used to calculate the value of the expression variable.
2. Write a script. After the script is executed, print a row with the prompt "Please input a number:". Ask the user to enter a value, then print the value, and then ask the user to enter a value again. Until the user enters "end" to stop.
#! /Bin/bash
# Inputtest
Var = NULL
While [Var! = "End"]
Do
Echo-N "inputA INTEGER :"
Read VaR
If [$ Var = "end"]
Then
Break
Fi
Echo "var is $ var"
Done
Note the following:
1) if [$ Var = "end"], the necessary spaces must exist. In addition, it is = rather than =
2) Var = NULL. Note that the variable value assignment cannot contain spaces around the equal sign.
3) In the echo-n "inputA INTEGER:" sentence, "-n" indicates that the carriage return does not wrap the line. In this way, the input and the prompt are on one line, of course, you can also use the-n command to indicate the number of inputs in the first row and in the second row.
3. Write a script and use the loop and continue keywords to calculate the sum of the three divisible numbers within 100.
#! /Bin/bash
# Sumtest
Sum = 0
Fora in 'seq 1 100'
Do
If ['expr $ A % 3'-Ne 0]
Continue
Fi
Sum = 'expr $ sum + $'
Done
Echo "the sum is $ sum"
This script has been debugged many times during writing.
1) I was accidentally wrong. In particular, when and when spaces are not added
When assigning values to variables, there must be no space, such as sum = 0. In addition, do not write sum = NULL here.
2) You cannot directly write sum = $ sum + $ A as sum = 'expr $ sum + $ a' when performing two numbers operations'
3) that is to say, there cannot be spaces around the equal sign of the value assignment statement. There must be spaces around the arithmetic operator and an expr value must be assigned to a variable.
4) The = and! cannot be used for integer comparison! -EQ and-ne
4. A function uses shift to calculate the product of all parameters. Assume that all parameters are integers (special variable $ # indicates the number of parameters included)
#! /Bin/bash
# Shifttest
Result = 1
While [$ #-GT 0]
Do
Result = 'expr $1 \ * $ result'
Shift
Done
Echo "the result is $ result"
$1 indicates the first parameter, $ # indicates the parameter format, and $ #-GT 0 indicates that the number of parameters is greater than 0.
Shift indicates shift
Write a script in step 5. You can upload the file name and use the tar.gzor tar.bz2 file to decompress it.
#! /Bin/bash
# Casetest
Case ${1 # *. Tar} in
GZ)
Tar jxvf $1
;;
Bz2)
Tar zxvf $1
;;
*)
Echo "errorfile"
Esac
Note:
$1 indicates the first command line parameter for running the script
${1 # *. Tar} indicates the remaining part after removing *. tar of the first command line parameter.
For example, if you use./mytar.shbak.tar.gzto run the script, the bak.tar.gz, ${1 # *. Tar.} specified by "1", is GZ.
Similarly, ${3 # *. Tar.} indicates that the remaining part of *. Tar. is removed from the third command line parameter.
6. Write a script to help you query RPM information. This script first prompts the user to select the query basis, such as the file name, package name, and all. Then, the user is prompted to select the query information, such as the package name, all files contained in the package, and package information. Then, ask if you want to continue the query.
7,MySQL: shell script for automatically backing up databases in Linux the program on the Linux Server updates the MySQL database every day, so it reminds me to write a shell script, combined with crontab, to regularly back up the database. In fact, it is very simple, mainly because MySQL mysqldump
Command.
The script content is as follows:
#! /Bin/sh
# File:/home/MySQL/backup. Sh
# Database info
Db_name = "test"
Db_user = "username"
Db_pass = "password"
# Others vars
Bin_dir = "/usr/local/MySQL/bin"
Bck_dir = "/home/MySQL/backup"
Date = 'date + % F'
# Todo
$ Bin_dir/mysqldump -- opt-U $ db_user-p $ db_pass $ db_name | gzip> $ bck_dir/db_1_date.gz
Then add the script to the/etc/crontab scheduled task:
01 5 ** 0 MySQL/home/MySQL/backup. Sh
All right, the system will automatically run the backup. Sh file to back up the MySQL database at every Sunday.