Determines whether the current user is the root administrator, and if so, executes the tar to perform a backup of the/etc/directory.#!/bin/bash# IF Current user was root then backup the/etcIf ["$ (id-u)"-eq 0];thenTar-czf/root/etc.tar.gz/etc
Shell scripting is the most basic knowledge of our OPS management, and here are a few examples of my learning process (compared to Daniel). The purpose of this blog is to record the course of learning scripts, but also to be able to discuss learning
1. Determine if a file is a block or character device file, and if it is copied to the/dev directoryRead-p "Input a file:" filenameIf [-B $filename-O-C $filename]ThenCP $filename/dev/Fi 2. Write a script to perform a simple subtraction operation
1. In the current directory, look for the files that were accessed within 20 minutes, and display the details of the file:Find./-name ' *.log '-mmin-20-exec ls-l {} \;Note: where {} is replaced by the file name, and the colon is used to indicate
There are two ways that Linux shell executesShell scripts start with #!/bin/bash, and the shell is executed with the first row checked and executed internally in the following ways:$/bin/bash script.sh1. Execute with SH.$sh script.sh #脚本位于当前目录下Or$sh/
[grep profile : ]grep is used to filter lines that contain specific charactersusage: grep ' pattern ' fileFor example: grep ' BBB ' aaa.txt--color # Search for the keyword BBB from the file Aaa.txt and highlight it.[Regular meta-character :
The case structure in bash, which can be used for multiple branches.Case "$ Var" inCondition1);;Condition2);;*)Default statments ;;EsacFor example:#! /Bin/bashEcho "hit a key, then hit return"Read keypressCase "$ keypress" in[A-Z]) echo "uppercase
#! /Bin/bashA = $1 # pass the first command line parameter to variable A, and the second command line parameter to BB = $2If [-Z $ A] | [-Z $ B] # determines whether a and B are empty. If one is empty, print the prompt statement and exit.ThenEcho
I. Condition statements
1,
If [expression]
Then
# Code block
Fi
2,
If [expression]
Then
# Code block
Else
# Code block
Fi
3,
If [expression]
Then
# Code block
Else if [expression] # Replace this field with Elif [expression]
Then
# Code block
Else
#
#! /Bin/bash
Hello = "var1"
Echo $ hello
Function func1 # define function func1
{
Local Hello = "var2" # define the internal variable hello
Echo $ hello
}
Func1 # Call a function
Echo $ hello
Execution result:
Conclusion: local variables only work
1. you can set the number of servers to be checked and the server to check. You can set the IP address of the non-Online host and the corresponding time of the non-Online host, and collect statistics on the number of non-Online hosts saved to the
Every time you create a new script, you must add # In front of the script #! Some fixed text such as/bin/bash. The bash Script Creation script can automatically add information such as Bash and date author. After the creation is complete, the system
A variable is a memory space. When a variable is referenced by a variable name, it is called a named memory space.
To define a variable, assign a fixed memory space to the variable. assign a value to the variable, that is, fill the data in the
Script: the source program implemented by the command flow control mechanism based on the actual situation.
Script description:
1. Start with shebang, that is #! /Bin/bash. The path of the script interpreter is specified here. 2. Comments starting
Single branch if statement
If condition; then statement1 statement2fi
Dual-branch if statement
If judgment condition; then statement1 statement2 ...... Else statement3 statement4 ...... Fi
Multi-branch if statement
If condition 1; then statement1
Jiebao
Jiebao
Jiebao
Jiebao
Jiebao
This series of scripts has already opened a forum on GitHub. I name it "Easy shell" (Click here to enter ).
At present, you have added the scripts in the previous phases. More scripts will be added to
Tag: Shell
1. Search for all files larger than 500 mb in the current directory, write these file names to a text file, and count the number of files.
Find/-size + 500 m-type F | tee/root/file.txt | WC-l or find/-size + 50 m-exec ls
Alias LL = 'LS-al'Use_color = false# Set colorful PS1 only on colorful terminals.# Dircolors -- print-database uses its own built-in Database# Instead of using/etc/dir_colors. Try to use the external file# First to take advantage of user additions.
Loop Control Structure of shell script Programming
Loop Control-for Loop
Syntax 1
For Variable in List
Do
Commands
Done
Syntax structure 2
For Variable in List; do
Commands
Done
This List can be a List, variable, command, etc.
A for Loop provides an
In some scenarios with high security requirements, the ssh timeout period is preset by the Administrator. After a period of time, the ssh connection is automatically disconnected. In this case, if you execute the script through ssh and the script
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