L4 Shell
Style Guide
variable name in uppercase
Local variables lowercase
Function names lowercase
Local variables add local
Intro and variable
echo -e enable backslash -n do not output the trailing newline
# 1. comment
:<<EOF
mulit-line
comment
EOF
${} is refer to variable
$[] is Numeral Calculations
$() is Command substitution
Operator
String
Array
Conditon
[ ] is same as test
Loop
Function
Lib
Demo
- Guessing game
#!/bin/bash
# Guessing game
# 1. generates a random number and guess
answer=$(date +%s%N | cut -c16-17)
count=0
while :
do
# Note the number of times of selection
count=$[count+1]
echo -n "Enter a number between 0 and 99:"
# 2. Get User Input
read aNum
# 3. If so, then exit
if [ $aNum -eq $answer ]
then
# If you guess right, exit
echo "Congratulations, you guessed it! You guess $count times"
break;
# 4. If the answer is less than or greater than, then prompt
elif [ $aNum -lt $answer ]
then
echo "No, more!"
else
echo "No, less!"
fi
done
- Get the current CPU usage
#!/bin/bash
# Get the current CPU usage
cpu_usage_rate(){
local line=$(cat /proc/stat | grep -B1 -m1 "cpu" | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8" "$9}')
local user=$(echo $line | awk '{print $1}')
local nice=$(echo $line | awk '{print $2}')
local system=$(echo $line | awk '{print $3}')
local idle=$(echo $line | awk '{print $4}')
local iowait=$(echo $line | awk '{print $5}')
local irq=$(echo $line | awk '{print $6}')
local softirq=$(echo $line | awk '{print $7}')
local steal_time=$(echo $line | awk '{print $8}')
local guest=$(echo $line | awk '{print $9}')
local cpu_total=$[user+nice+system+idle+iowait+irq+softirq+steal_time+guest]
local cpu_used=$[user+nice+system+iowait+irq+softirq+steal_time+guest]
# $[cpu_used/cpu_total] The default is an integer
# Reserved two precision
local rate=$(awk 'BEGIN{printf "%.2f\n",'$[cpu_used*100]'/'$cpu_total'}')
echo "CPU Usage: $rate%"
}
cpu_usage_rate
- Probing the local network
#!/bin/bash
# Detecting an IP address is unobstructed
test_ping(){
local ip=$1
if [ -z "$ip" ]
then
echo "Parameters can not be empty"
return
fi
ping -c1 $ip &>/dev/null
# if [ $? -eq 0 ]
# then
# echo "$ip on"
# else
# echo "$ip off"
# fi
[ $? -eq 0 ] && echo "$ip on" || echo "$ip off"
}
for ((i=1; i<255; i++))
do
test_ping "192.168.31.$i" &
done
# Concurrency
wait
echo "Finished"
- Write log
#!/bin/bash
log_path="./tmp/"
log_file="my.log"
log_fullpath="$log_path$log_file"
# 1. Check log file
check_log(){
# Create file if not exits
if [ ! -d $log_path ]; then
mkdir -p $log_path
fi
# Create file if not exits
if [ ! -f $log_fullpath ]; then
touch "$log_fullpath"
fi
# Whether the file is readable
if [ ! -r $log_fullpath ]; then
echo "$log_fullpath 文件不可读"
return 1
fi
# If the file is writable
if [ ! -w $log_fullpath ]; then
echo "$log_fullpath 文件不可写"
return 1
fi
return 0
}
# 2. Backup log
backup_log(){
# Get File Size
local size=`ls -l $log_fullpath | awk '{print $5}'`
if [ $size -gt 1024 ]; then
local back_file="$log_path`date +'%Y%m%d-%H%M%S'`.log"
mv -f $log_fullpath $back_file
echo "Backed up to $back_file"
fi
return
}
# 3. Write log file
write_log(){
check_log
if [ $? -ne 0 ]; then
return 1
fi
backup_log
local now_time=`date +'%Y-%m-%d %H:%M:%S'`
echo "[$now_time] $1" | tee -a ${log_fullpath}
return 0
}
write_log '####################################################'
write_log 'log write testing string 11'
write_log 'log write testing string 22'
write_log '----------------------------------------------------'