当前位置:网站首页>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
}
边栏推荐
- 信息学奥赛一本通 1400:统计单词数 (字符串匹配)
- Use abp Zero builds a third-party login module (I): Principles
- 信息学奥赛一本通 1405:质数的和与积 (思维题)
- Halcon C # sets the form font and adaptively displays pictures
- R language uses the aggregate function of epidisplay package to split numerical variables into different subsets based on factor variables, calculate the summary statistics of each subset, and use agg
- 关于 selenium.common.exceptions.WebDriverException: Message: An unknown server-side error 解决方案(已解决)
- Program analysis and Optimization - 8 register allocation
- Unity C# 网络学习(九)——WWWFrom
- R语言epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布、使用cex.X.axis参数指定X轴轴刻度数值标签字体的大小
- clustermeet
猜你喜欢

文献1

一篇抄十篇,CVPR Oral被指大量抄袭,大会最后一天曝光!

小程序:uniapp解决 vendor.js 体积过大的问题

使用卷积对数据进行平滑处理

【雲原生】 ”人人皆可“ 編程的無代碼 iVX 編輯器

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

Unity uses skybox panoramic shader to make panorama preview. There is a gap. Solution

Smoothing data using convolution

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

TS common data types summary
随机推荐
Attention meets geometry: geometry guided spatiotemporal attention consistency self supervised monocular depth estimation
打新债注册开户安全吗,有没有什么风险?
Redis集群消息
It's natural for the landlord to take the rent to repay the mortgage
Mathematical modeling of war preparation 30 regression analysis 2
编译配置in文件
Use abp Zero builds a third-party login module (II): server development
R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, uses step function to realize stepwise re
RestCloud ETL解决shell脚本参数化
Minister of investment of Indonesia: Hon Hai is considering establishing electric bus system and urban Internet of things in its new capital
Unity unitywebrequest download package
聊聊几位大厂清华同学的近况
功能:crypto-js加密解密
R语言使用ggplot2可视化泊松回归模型(Poisson Regression)的结果、可视化不同参量组合下的计数结果
Unity C # e-learning (VIII) -- www
乐鑫 AWS IoT ExpressLink 模组达到通用可用性
使用 Abp.Zero 搭建第三方登录模块(二):服务端开发
Principle of TCP reset attack
teamviewer显示设备数量上限解决方法
关于 selenium.common.exceptions.WebDriverException: Message: An unknown server-side error 解决方案(已解决)