当前位置:网站首页>Sorted out a batch of script standard function modules (version 2021)
Sorted out a batch of script standard function modules (version 2021)
2022-06-26 15:09:00 【BOGO】
from 1 Overlay to 100{
echo $[$(echo +{1..100})]
echo $[(100+1)*(100/2)]
seq -s '+' 100 |bc
}
Judge whether the parameter is empty - Empty exit and print null{
#!/bin/sh
echo $1
name=${1:?"null"}
echo $name
}
Circular array {
for ((i=0;i<${#o[*]};i++))
do
echo ${o[$i]}
done
}
Judge the path {
if [ -d /root/Desktop/text/123 ];then
echo " eureka 123"
if [ -d /root/Desktop/text ]
then echo " eureka text"
else echo " Did not find text"
fi
else echo " Did not find 123 Folder "
fi
}
Find the most frequent occurrences {
awk '{print $1}' file|sort |uniq -c|sort -k1r
}
Determine whether the script parameters are correct {
./test.sh -p 123 -P 3306 -h 127.0.0.1 -u root
#!/bin/sh
if [ $# -ne 8 ];then
echo "USAGE: $0 -u user -p passwd -P port -h host"
exit 1
fi
while getopts :u:p:P:h: name
do
case $name in
u)
mysql_user=$OPTARG
;;
p)
mysql_passwd=$OPTARG
;;
P)
mysql_port=$OPTARG
;;
h)
mysql_host=$OPTARG
;;
*)
echo "USAGE: $0 -u user -p passwd -P port -h host"
exit 1
;;
esac
done
if [ -z $mysql_user ] || [ -z $mysql_passwd ] || [ -z $mysql_port ] || [ -z $mysql_host ]
then
echo "USAGE: $0 -u user -p passwd -P port -h host"
exit 1
fi
echo $mysql_user $mysql_passwd $mysql_port $mysql_host
# result root 123 3306 127.0.0.1
}
Regular matching mailbox {
^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$
}
Print forms {
#!/bin/sh
clear
awk 'BEGIN{
print "+--------------------+--------------------+";
printf "|%-20s|%-20s|\n","Name","Number";
print "+--------------------+--------------------+";
}'
a=`grep "^[A-Z]" a.txt |sort +1 -n |awk '{print $1":"$2}'`
#cat a.txt |sort +1 -n |while read list
for list in $a
do
name=`echo $list |awk -F: '{print $1}'`
number=`echo $list |awk -F: '{print $2}'`
awk 'BEGIN{printf "|%-20s|%-20s|\n","'"$name"'","'"$number"'";
print "+--------------------+--------------------+";
}'
done
awk 'BEGIN{
print " *** The End *** "
print " "
}'
}
Judge whether the date is legal {
#!/bin/sh
while read a
do
if echo $a | grep -q "-" && date -d $a +%Y%m%d > /dev/null 2>&1
then
if echo $a | grep -e '^[0-9]\{4\}-[01][0-9]-[0-3][0-9]$'
then
break
else
echo " The date you entered is illegal , Please input again !"
fi
else
echo " The date you entered is illegal , Please input again !"
fi
done
echo " The date is $a"
}
Print all dates in the date range {
#!/bin/bash
qsrq=20010101
jsrq=20010227
n=0
>tmp
while :;do
current=$(date +%Y%m%d -d"$n day $qsrq")
if [[ $current == $jsrq ]];then
echo $current >>tmp;break
else
echo $current >>tmp
((n++))
fi
done
rq=`awk 'NR==1{print}' tmp`
}
A small algorithm for mathematical calculation {
#!/bin/sh
A=1
B=1
while [ $A -le 10 ]
do
SUM=`expr $A \* $B`
echo "$SUM"
if [ $A = 10 ]
then
B=`expr $B + 1`
A=1
fi
A=`expr $A + 1`
done
}
Multi line merge {
sed '{N;s/\n//}' file # Merge two rows into one row ( Remove the newline )
awk '{printf(NR%2!=0)?$0" ":$0" \n"}' # Merge two rows into one row
awk '{printf"%s ",$0}' # Merge all rows
awk '{if (NR%4==0){print $0} else {printf"%s ",$0}}' file # take 4 The rows merge into one ( Scalable )
}
Horizontal and vertical conversion {
cat a.txt | xargs # Column turned
cat a.txt | xargs -n1 # Transfer line column
}
Turn from vertical to horizontal {
cat file|tr '\n' ' '
echo $(cat file)
#!/bin/sh
for i in `cat file`
do
a=${a}" "${i}
done
echo $a
}
Get the root directory of the user {
#! /bin/bash
while read name pass uid gid gecos home shell
do
echo $home
done < /etc/passwd
}
Remote packaging {
ssh -n $ip 'find '$path' /data /opt -type f -name "*.sh" -or -name "*.py" -or -name "*.pl" |xargs tar zcvpf /tmp/data_backup.tar.gz'
}
Turn Chinese characters into encode Format {
echo Forum | tr -d "\n" | xxd -i | sed -e "s/ 0x/%/g" | tr -d " ,\n"
%c2%db%cc%b3
echo Forum | tr -d "\n" | xxd -i | sed -e "s/ 0x/%/g" | tr -d " ,\n" | tr "[a-f]" "[A-F]" # uppercase
%C2%DB%CC%B3
}
Change the file names of directories with uppercase letters to all lowercase letters {
#!/bin/bash
for f in *;do
mv $f `echo $f |tr "[A-Z]" "[a-z]"`
done
}
Find consecutive lines , Insert... Before discontinuous lines {
#/bin/bash
lastrow=null
i=0
cat incl|while read line
do
i=`expr $i + 1`
if echo "$lastrow" | grep "#include <[A-Z].h>"
then
if echo "$line" | grep -v "#include <[A-Z].h>"
then
sed -i ''$i'i\\/\/All header files are include' incl
i=`expr $i + 1`
fi
fi
lastrow="$line"
done
}
Query other database engines {
#/bin/bash
path1=/data/mysql/data/
dbpasswd=db123
#MyISAM or InnoDB
engine=InnoDB
if [ -d $path1 ];then
dir=`ls -p $path1 |awk '/\/$/'|awk -F'/' '{print $1}'`
for db in $dir
do
number=`mysql -uroot -p$dbpasswd -A -S "$path1"mysql.sock -e "use ${db};show table status;" |grep -c $engine`
if [ $number -ne 0 ];then
echo "${db}"
fi
done
fi
}
Batch modify database engine {
#/bin/bash
for db in test test1 test3
do
tables=`mysql -uroot -pdb123 -A -S /data/mysql/data/mysql.sock -e "use $db;show tables;" |awk 'NR != 1{print}'`
for table in $tables
do
mysql -uroot -pdb123 -A -S /data/mysql/data/mysql.sock -e "use $db;alter table $table engine=MyISAM;"
done
done
}
take shell Insert the retrieved data mysql database {
mysql -u$username -p$passwd -h$dbhost -P$dbport -A -e "
use $dbname;
insert into data values ('','$ip','$date','$time','$data')
"
}
Days between two dates {
D1=`date -d '20070409' +"%s"`
D2=`date -d '20070304 ' +"%s"`
D3=$(($D1 - $D2))
echo $(($D3/60/60/24))
}
while perform ssh Only one cycle {
cat - # Give Way cat Read connection file stdin Information about
seq 10 | while read line; do ssh localhost "cat -"; done # According to the 9 The next time was ssh Eat
seq 10 | while read line; do ssh -n localhost "cat -"; done # ssh add -n Parameter to avoid looping only once
}
ssh Batch execution command {
# edition 1
#!/bin/bash
while read line
do
Ip=`echo $line|awk '{print $1}'`
Passwd=`echo $line|awk '{print $2}'`
ssh -n localhost "cat -"
sshpass -p "$Passwd" ssh -n -t -o StrictHostKeyChecking=no [email protected]$Ip "id"
done<iplist.txt
# edition 2
#!/bin/bash
Iplist=`awk '{print $1}' iplist.txt`
for Ip in $Iplist
do
Passwd=`awk '/'$Ip'/{print $2}' iplist.txt`
sshpass -p "$Passwd" ssh -n -t -o StrictHostKeyChecking=no [email protected]$Ip "id"
done
}
Print characters in the same position {
#!/bin/bash
echo -ne "\t"
for i in `seq -w 100 -1 1`
do
echo -ne "$i\b\b\b"; # The key \b Backspace
sleep 1;
done
}
Simple control of multi process background concurrency {
#!/bin/bash
test () {
echo $a
sleep 5
}
for a in `seq 1 30`
do
test &
echo $!
((num++))
if [ $num -eq 6 ];then
echo "wait..."
wait
num=0
fi
done
wait
}
shell Concurrent {
#!/bin/bash
tmpfile=$$.fifo # Create pipe name
mkfifo $tmpfile # Create pipes
exec 4<>$tmpfile # Create file labels 4, Operate the pipeline in read-write mode $tmpfile
rm $tmpfile # Clear the created pipe file
thred=4 # Specify the number of concurrent
seq=(1 2 3 4 5 6 7 8 9 21 22 23 24 25 31 32 33 34 35) # Create a task list
# Create a corresponding number of placeholders for concurrent threads
{
for (( i = 1;i<=${thred};i++ ))
do
echo; # because read The command reads one line at a time , One echo The default output is a line break , So output a placeholder newline for each thread
done
} >&4 # Write occupation information to the pipeline
for id in ${seq} # From the task list seq Get each task in order in
do
read # Read a line , namely fd4 A place holder in
(./ur_command ${id};echo >&4 ) & # Perform tasks in the background ur_command And put the task ${id} Assign to the current task ; After the task is completed fd4 Write a placeholder
done <&4 # Appoint fd4 For the whole for Standard input for
wait # Waiting for all here shell The background task started in the script is completed
exec 4>&- # Close the pipe
}
shell Concurrent function {
function ConCurrentCmd()
{
# Number of processes
Thread=30
# List file
CurFileName=iplist.txt
# Definition fifo file
FifoFile="$$.fifo"
# Create a new one fifo Files of type
mkfifo $FifoFile
# take fd6 With this fifo Type files are connected by reading and writing
exec 6<>$FifoFile
rm $FifoFile
# In fact, it is in the file descriptor 6 Placed in $Thread A carriage return
for ((i=0;i<=$Thread;i++));do echo;done >&6
# The standard input will then come from fd5
exec 5<$CurFileName
# Start looping through the lines in the file list
Count=0
while read -u5 line
do
read -u6
let Count+=1
# Here, a child process is defined to be executed in the background
# One read -u6 The order is executed once , From fd6 Subtract a carriage return from , Then go down
# fd6 When there is no carriage return in , Right here , Thus, the process quantity control is realized
{
echo $Count
# This code box is to perform specific operations
function
echo >&6
# When the process is over , Again to fd6 A carriage return is appended to the , That is to say read -u6 The minus one
} &
done
# Wait for all backtable processes to end
wait
# close fd6
exec 6>&-
# close fd5
exec 5>&-
}
Concurrency example {
#!/bin/bash
pnum=3
task () {
echo "$u start"
sleep 5
echo "$u done"
}
FifoFile="$$.fifo"
mkfifo $FifoFile
exec 6<>$FifoFile
rm $FifoFile
for ((i=0;i<=$pnum;i++));do echo;done >&6
for u in `seq 1 20`
do
read -u6
{
task
[ $? -eq 0 ] && echo "${u} Success " || echo "${u} Secondary failure "
echo >&6
} &
done
wait
exec 6>&-
}
}
function {
ip(){
echo "a 1"|awk '$1=="'"$1"'"{print $2}'
}
web=a
ip $web
}
Check whether the software package exists {
rpm -q dialog >/dev/null
if [ "$?" -ge 1 ];then
echo "install dialog,Please wait..."
yum -y install dialog
rpm -q dialog >/dev/null
[ $? -ge 1 ] && echo "dialog installation failure,exit" && exit
echo "dialog done"
read
fi
}
边栏推荐
- MongoDB系列之适用场景和不适用场景
- 详解C语言编程题:任意三条边能否构成三角形,输出该三角形面积并判断其类型
- 【 Native cloud】 Éditeur ivx Programmable par tout le monde
- R language dplyr package bind_ The rows function merges the rows of the two dataframes vertically. The final number of rows is the sum of the rows of the original two dataframes (combine data frames)
- 5张图诠释了容器网络
- Mathematical modeling of war preparation 30 regression analysis 2
- R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息、使用aggregate.data.frame函数计算分组汇总统计信息
- Is it safe to open a stock account with the account manager online??
- Informatics Olympiad 1405: sum and product of prime numbers (thinking problem)
- TCP/IP协议竟然有这么多漏洞?
猜你喜欢

TCP congestion control details | 1 summary

Smoothing data using convolution

Restcloud ETL extracting dynamic library table data

The heavyweight white paper was released. Huawei continues to lead the new model of smart park construction in the future

数据库-视图

TS common data types summary

About selenium common. exceptions. Webdriverexception: message: an unknown server side error solution (resolved)

Use abp Zero builds a third-party login module (I): Principles

ETL过程中数据精度不准确问题

Unity 利用Skybox Panoramic着色器制作全景图预览有条缝隙问题解决办法
随机推荐
Execution of commands in the cluster
Common operation and Principle Exploration of stream
SAP 销售数据 实际发货数据导出 销量
Idea shortcut key
English语法_形容词/副词3级 - 原级句型
RestCloud ETL解决shell脚本参数化
Pytoch deep learning code skills
5张图诠释了容器网络
Login authentication service
Redis-集群
The tablestack function of the epidisplay package of R language makes a statistical summary table (descriptive statistics of groups, hypothesis test, etc.), does not set the by parameter to calculate
Declaration and assignment of go variables
[solo π] ADB connects multiple mobile phones
710. random numbers in the blacklist
Keil4打开单片机工程一片空白,cpu100%程序卡死的问题解决
Program analysis and Optimization - 8 register allocation
TS常用数据类型总结
kubernetes的Controller之deployment
数据库-完整性约束
The R language cartools package divides data, the scale function scales data, and the KNN function of the class package constructs a k-nearest neighbor classifier