Shell Script 100 Example "one"

Source: Internet
Author: User
Tags install php stdin

1. Write the Hello World script
#!/bin/bash
echo "Hello World"

2. Create a Linux system account and password with location variables
#!/bin/bash
#$1 is the first parameter to execute a script, and the second argument to execute the script
Useradd "$!"
echo "$" | Passwd‐‐stdin "$"

3.5 per week using the tar command to back up all log files under/var/log
#vim/root/logbak.sh
#编写备份脚本, the file name after the backup contains a date label to prevent subsequent backups from overwriting the previous backup data
#注意 the date command needs to be enclosed in inverted quotation marks above the keyboard <tab> key.
Tar-czf log-' Date +%y%m%d '. Tar.gz/var/log

# crontab‐e #编写计划任务, execute backup script
* * 5/root/logbak.sh

4. One-click Deployment Lnmp (RPM package version)
#!/bin/bash
#使用 Yum Install deployment lnmp, you need to configure the Yum source in advance or the script will fail
#本脚本使用于 centos7.2 or RHEL7.2
Yum‐y Install httpd
Yum‐y Install mariadb mariadb‐devel mariadb‐server
Yum‐y Install PHP Php‐mysql

Systemctl start httpd mariadb
Systemctl Enable httpd mariadb

5. Real-time monitoring of local memory and hard disk remaining space, the remaining memory is less than 500M, the root partition space is less than 1000M
Send an alert message to the root administrator
#!/bin/bash
#Author: The Adventures of Tintin (Jacob)
#提取根分区剩余空间
disk_size=$ (DF/|awk '/\//{print $4} ')

#提取内存剩余空间
mem_size=$ (Free |awk '/mem/{print $4} ')
While:
Do
#注意内存和磁盘提取的空间大小都是以 Kb
If [$disk _size‐le 512000‐a $mem _size‐le 1024000];then
Mail‐s Warning Root <<eof
Insufficient resources, under-resourced
Eof
Fi
Done

6. The script generates a random number within 100, prompting the user to guess the number, according to the user's input, prompting the user to guess right,
Guess small or guess big, until the user guesses the end of the script.
#!/bin/bash
#Author: The Adventures of Tintin (Jacob)
#RANDOM a system variable with the system, with a value of 0‐32767 random number
Random number of #使用取余算法将随机数变为 1‐100
NUM=$[RANDOM%100+1]

#使用 read prompts the user to guess the number
#使用 If the user guesses the size relationship of the number: ‐eq (equals), ‐ne (Not Equal), ‐GT (greater than), ‐ge (greater than or equal), ‐lt (less than), ‐le (small
Equal to)
While:
Do
Read‐p "The computer generates a random number of 1‐100, you guess:" Cai
If [$cai ‐eq $num];then
echo "Congratulations, guess right."
Exit
elif [$cai ‐gt $num];then
echo "Oops, big guess."
Else
echo "Oops, guess small."
Fi
Done

7. Check whether the current user of the machine is a super administrator, if it is an administrator, use Yum to install VSFTPD, if not
Yes, you are prompted for non-administrators (use a string to compare versions)
#!/bin/bash
if [$USER = = "Root"];then
Yum‐y Install VSFTPD
Else
echo "You are not an administrator and do not have permission to install software"
Fi

8. Check whether the current user of the machine is a super administrator, if it is an administrator, use Yum to install VSFTPD, if not
Yes, you are prompted for non-administrators (use UID numbers to compare versions)

#!/bin/bash
If [$UID ‐eq 0];then
Yum‐y Install VSFTPD
Else
echo "You are not an administrator and do not have permission to install software"
Fi

9. Scripting: Prompt the user to enter a user name and password, the script automatically creates the corresponding account and configuration password. If the user
If you do not enter the account name, you must enter the account name and exit the script, and if the user does not enter a password, use the default
123456 as the default password.
#!/bin/bash
Read‐p "Please enter Username:" User
#使用 ‐z can determine if a variable is empty, if it is empty, prompt the user must enter the account name, and exit the script, the exit code is 2
#没有输入用户名脚本退出后, use $? To view the return code of 2
If [‐z $user];then
echo "You don't need to enter your account name"
Exit 2
Fi
#使用 Stty‐echo Turn off the echo function of the shell
#使用 Stty Echo to open the Echo function of the shell
Stty‐echo
Read‐p "Please enter your password:" Pass
Stty Echo
pass=${pass:‐123456}
Useradd "$user"
echo "$pass" | Passwd‐‐stdin "$user"

10. Prompt the user to enter 3 integers, and the script sequentially outputs 3 digits according to the number size
#!/bin/bash
Read‐p "Please enter an integer:" Num1
Read‐p "Please enter an integer:" num2
Read‐p "Please enter an integer:" num3
#不管谁大谁小, and finally print echo "$num 1, $num 2, $num 3"
The minimum value is always stored in the #num1, the median value is always stored in the num2, and the maximum value is num3 forever.
#如果输入的不是这样的顺序, you change the order in which the numbers are stored, such as: You can swap the values of NUM1 and num2
Tmp=0
#如果 NUM1 is greater than num2, swap the values of NUM1 and num2 to ensure that the minimum value is stored in the NUM1 variable.
If [$num 1‐gt $num 2];then


tmp= $num 1
num1= $num 2
Num2= $tmp
Fi
#如果 NUM1 is greater than num3, swap num1 and num3 to ensure that the minimum value is stored in the NUM1 variable.
If [$num 1‐gt $num 3];then


tmp= $num 1
num1= $num 3
num3= $tmp
Fi
#如果 num2 is greater than num3, the num2 and num3 are labeled to ensure that the NUM2 variable has a smaller value

If [$num 2‐gt $num 3];then
tmp= $num 2
num2= $num 3
num3= $tmp
Fi

echo "Sorted data: $num 1, $num 2, $num 3"


Shell Script 100 Example "one"

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.