100 Examples of Shell Script "Ten"

91. View all virtual machine disk usage and CPU usage information
#!/bin/bash
virt-df
read -n1 "Press any key to continue" key
virt-top
 
92. Use shell script to print the following graph:
 
 
 
#!/bin/ bash
#Author: Adventures of Tintin (Jacob) #Print
the first group of pictures
#for(()) is the syntax format of C-like language, you can also use the format of for i in;do ;done to replace
#for((i=1; i<=9;i++)) The loop will be executed 9 times, i starts from 1 to 9, and i increments by 1 every time the loop
clear
for (( i=1; i<=9; i++ ))
do
    for (( j= 1; j<=i; j++ ))
  do
      echo -n "$i"
  done
  echo ""
done
read -n1 "Press any key to continue" key #Print the
second set of pictures
clear
for (( i=1; i<= 5; i++ )) do
    for (( j=1; j<=i; j++ ))
  do
      echo -n "|"
  done
  echo "_ "
done
read -n1 "Press any key to continue" key #Print the
third group of pictures
clear
for (( i=1; i<=5; i++ ))
do
    for (( j=1; j<=i; j++ ))
  do
      echo ‐n " *"
  done
  echo ""
done
 
for (( i=5; i>=1; i‐‐ ))
do
  for (( j=1; j<=i; j++ ))
  do
       echo ‐n " *"
  done
  echo ""
done
 
93. According to the current time of the computer, return the greeting, you can set the script to start at boot
#!/bin/bash
#Author: Adventures of Tintin (Jacob)
#00-12 o'clock is morning , 12-18 o’clock is afternoon, 18-24 o’clock is
evening then   msg="Good Morning $USER"



elif [ $tm ‐gt 12 ‐a $tm ‐le 18 ];then
  msg="Good Afternoon $USER"
else
  msg="Good Night $USER"
fi
echo "The current time is: $(date +"%Y‐% m‐%d %H:%M:%S")"
echo -e "\033[34m$msg\033[0m"
 
94. Read the account name entered by the user, and write the account name into the array to save
#! /bin/bash
#Author: Adventures of Tintin (Jacob) #Define
the name of the array as name, the subscript of the array is i, the subscript starts from 0, each time an account name is entered, the subscript increases by 1, and continues to save the next account
#Finally , enter over, the script exits after the script outputs the summary information
i=0
while :
do
  read -p "Please enter the account name, enter over to end:" key
  if [ $key == "over" ];then 
break
  else
 
name[$ i]=$key
 
​​let i++
  fi
done
echo "Number of total account names: ${#name[*]}"
echo "${name[@]}"
 
95. Determine whether a file or directory exists
#!/bin/bash
if [ $# ‐eq 0 ] ;then
echo "No parameters entered, please enter parameters"
echo "Usage: $0 [file name|directory name]"
fi
if [ ‐f $1 ];then
echo "The file exists"
ls -l $1
else
echo "There is no file"
fi
if [-d $1 ];then
     echo "The directory exists"
     ls -ld $2
else
     echo "There is no such directory"
fi
 
96. Print various Time format
#!/bin/bash
#Author: Adventures of Tintin (Jacob)
echo "Display the short name of the week (eg: Sun)"
date +%a
echo "Display the full name of the week (eg: Sunday)"
date +%A
echo "Display the month Abbreviation (eg: Jan)"
date +%b
echo "display the full name of the month (eg: January)"
date +%B
echo "display the numeric month (eg:12)"
date +%m
echo "Display numeric date (eg: 01)"
date +%d
echo "display the numerical year (eg: 01)"
date +%Y echo "display the year-month-day"
date +%F
echo "display the hour (24-hour format)"
date +%H
echo "display minutes(00..59)"
date +%M
echo "display seconds"
date +%S
echo "display nanoseconds"
date +%N
echo "combine display"
date +"%Y%m%d %H:%M :%S"
 
97. Use egrep to filter the MAC address
#!/bin/bash
#Author: Adventures of Tintin (Jacob)
#MAC address consists of hexadecimal, such as AA:BB:CC:DD:EE:FF
#[0‐ 9a-fA-F]{2} represents a hexadecimal value, {5} represents the hexadecimal of 5 consecutive groups of prefixes:
egrep "[0-9a-fA-F]{2}(:[ 0‐9a‐fA‐F]{2}){5}" $1
 
98. Statistics of the winning probability of each number of the two-color ball
#!/bin/bash
#Author:The Adventures of Tintin (Jacob) #The
winning numbers of the past two-color ball are as follows:
#01 04 11 28 31 32  16
#04 07 08 18 23 24  02
#02 05 06 16 28 29 04
#04 19 22 27 30 33 01
#05 10 18 19 30 31 03
#02 06 11 12 19 29 06 #Count
the probability of basketball and red ball data appearing (basketball is not in order, statistics Probability of all basketballs mixed together)
awk '{print $1"\n"$2"\n"$3"\n"$4"\n"$5"\n"$6}' 1.txt |sort |uniq -c | sort
awk '{print $7}' 1.txt |sort |uniq ‐c | sort
 
99. Generate self-signed private key and certificate
#!/bin/bash
#Author:The Adventures of Tintin (Jacob)
read ‐p "Please enter the storage certificate directory:" dir
if [ ! ‐d $dir ];then
  echo "The directory does not exist"
  exit
fi
read ‐p "Please enter the key name:" name
#Use openssl to generate a private key
openssl genrsa ‐out ${dir} /${name}.key #Use
openssl to generate a certificate#The subj option can automatically fill in the Common Name information non-interactively when generating a certificate
openssl req ‐new ‐x509 ‐key ${dir}/${name}.key ‐subj "/CN=common" ‐out ${dir}/${name}.crt
 
100.
wc program written in awk
#! /bin/bash #custom
variable chars variable stores the number of characters, custom variable words variable stores the number of words
#awk built-in variable NR stores the number of lines
#length() is an awk built-in function, used to count the number of characters in each line, Because there will be a hidden $ in each line, +1 is added after each count
#wc program will also count the file ending character $, you can use cat -A file name to check the hidden character
awk '{chars+=length ($0)+1;words+=NF} END{print NR,words,chars}' $1

Guess you like

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