100 Examples of Shell Scripts "One"

1. Write a hello world script
#!/bin/bash
echo "hello world"
 
2. Create a Linux system account and password through location variables
#!/bin/bash
#$1 is the first parameter to execute the script, $2 is to execute the script The second parameter
useradd "$!"
echo "$2" | passwd ‐‐stdin "$2"
 
3. Use the tar command to backup all log files under /var/log every 5th week
#vim /root/logbak.sh #write
backup Script, the backed-up file name contains a date label to prevent subsequent backups from overwriting the previous backup data #Note
that the date command needs to be enclosed in backticks, and the backticks are on the keyboard <tab> key
tar -czf log-`date +% Y%m%d`.tar.gz /var/log
 
# crontab ‐e #Write a scheduled task and execute a backup script
00 03 * * 5 /root/logbak.sh
 
4. One-click deployment of LNMP (RPM package version)
#! /bin/bash
#Using yum to install and deploy LNMP, you need to configure the yum source in advance, otherwise the script will fail
#This script is used in 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. Monitor the local memory and remaining hard disk space in real time, the remaining memory is less than 500M, and the remaining space of the root partition is less than 1000M
, send an alarm email to the root administrator
#!/bin/bash
#Author: Adventures of Tintin (Jacob) #Extract
the remaining space of the root partition
disk_size=$(df / |awk '/\//{print $4}')

#Extract the remaining memory space
mem_size=$(free |awk '/Mem/{print $4}')
while :
do #Note
that the size of memory and disk extraction is in Kb
if [ $disk_size ‐le 512000 ‐a $ mem_size ‐le 1024000 ]; then
     mail ‐s Warning root <<EOF
Insufficient resources, insufficient resources
EOF
fi
done
 
6. The script generates a random number within 100, prompting the user to guess the number, and according to the user's input, prompting the user to guess correctly,
Guess small or guess big, until the user guesses correctly the script ends.
#!/bin/bash
#Author: Adventures of Tintin (Jacob)
#RANDOM is a system variable that comes with the system, and the value is a random number of 0-32767 #Use the
remainder algorithm to change the random number to a random number of 1-100
num= $[RANDOM%100+1]
 
#Use read to prompt the user to guess the number #Use
if to judge the size relationship of the user guessing the number: -eq (equal), -ne (not equal), -gt (greater than), -ge (greater than or equal to) ),‐lt (less than),‐le (less
than or equal to)
while :
do
      read -p "The computer generated a random number of 1-100, you guessed it: " cai
      if [ $cai ‐eq $num ]; then
             echo "Congratulations, you guessed correctly"
             exit
      elif [ $cai ‐gt $num ]; then
             echo "Oops, guess big"
      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 , prompt You are not an administrator (use string comparison version)
#!/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. Detect whether the current user of the machine is a super administrator. If it is an administrator, use yum to install vsftpd. If not
, it will prompt you to be a non-administrator (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. Write a script: prompt the user to enter a username and password, the script will be created automatically The corresponding account and configuration password. If the user
does not enter the account name, the user will be prompted to enter the account name and exit the script; if the user does not enter the password, the default 123456 will be uniformly used
as the default password.
#!/bin/bash
read -p "Please enter the user name: " user
#Use -z to judge whether a variable is empty, if it is empty, prompt the user to enter the account name and exit the script, the exit code is 2
#No After entering the username script to exit, use $? to view the return code of 2
if [ ‐z $user ];then
      echo "You don't need to enter the account name"
      exit 2
fi
#Use stty ‐echo to turn off the shell echo function
#Use stty echo Open the shell's echo function
stty ‐echo
read ‐p "Please enter the password: " pass
stty echo
pass=${pass:‐123456}
useradd "
echo "$pass" | passwd ‐‐stdin "$user"
 
10. Prompt the user to enter 3 integers in turn, the script will output 3 numbers in order according to the size of the numbers
#!/bin/bash
read -p "Please enter an integer:" num1
read -p "Please enter an integer:" num2
read -p "Please enter an integer:" num3 #No
matter who is bigger or smaller, print echo at the end "$num1,$num2,$num3"
#num1 always keeps the smallest The value of num2, the middle value is always stored in num2, and the maximum value is always stored in num3
#If the input is not in this order, change the storage order of the numbers, such as: you can swap the values ​​of num1 and num2
tmp=0
#If 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 [ $num1 ‐gt $num2 ];then   
 
 
  tmp=$num1
  num1=$num2
  num2=$tmp
fi
#If num1 is greater than num3, then Swap num1 and num3 to ensure that the minimum value is stored in the num1 variable
if [ $num1 ‐gt $num3 ];   then   
 
 
  tmp=$num1
  num1=$num3
  num3=$tmp
fi
#If num2 is greater than num3, match num2 and num3 to ensure that the smaller value is stored in the num2 variable

if [ $num2 ‐gt $num3 ];then
  tmp=$num2
  num2=$num3
  num3=$tmp
fi
 
echo "The sorted data is: $num1,$num2,$num3"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324452738&siteId=291194637