当前位置:网站首页>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
}
边栏推荐
- R语言epiDisplay包的tableStack函数制作统计汇总表格(分组的描述性统计、假设检验等)、不设置by参数计算基础描述性统计信息、指定对于大多数样本负相关的变量进行反序
- R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息、使用aggregate.data.frame函数计算分组汇总统计信息
- RestCloud ETL抽取動態庫錶數據實踐
- 【云原生】 ”人人皆可“ 编程的无代码 iVX 编辑器
- cluster addslots建立集群
- Solution to the upper limit of TeamViewer display devices
- Get the intersection union difference set of two dataframes
- TS common data types summary
- Pod scheduling of kubernetes
- TCP congestion control details | 1 summary
猜你喜欢

人力资源导出数据 excel VBA

Redis-集群

TCP拥塞控制详解 | 1. 概述

Halcon C # sets the form font and adaptively displays pictures

RestCloud ETL抽取动态库表数据实践

Restcloud ETL extracting dynamic library table data

Mathematical modeling of war preparation 30 regression analysis 2

Attention meets Geometry:几何引导的时空注意一致性自监督单目深度估计

刷题笔记(十九)--二叉树:二叉搜索树的修改与构造

Authoritative announcement on the recruitment of teachers in Yan'an University in 2022
随机推荐
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
获取两个dataframe的交并差集
Numpy basic use
Redis cluster messages
nvidia-smi 报错
kubernetes的Controller之deployment
一键安装gcc脚本
信息学奥赛一本通 1400:统计单词数 (字符串匹配)
Is it safe to open an account by digging money? Is there any risk?
Unity 利用Skybox Panoramic着色器制作全景图预览有条缝隙问题解决办法
R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息、使用aggregate.data.frame函数计算分组汇总统计信息
redis集群的重新分片与ASK命令
Optimizing for vectorization
Talk about the RPA direction planning: stick to simple and valuable things for a long time
R language dplyr package summary_ The at function calculates the mean and median of multiple data columns (specified by vectors) in the dataframe data, and specifies na RM parameter configuration dele
Mark一下 Unity3d在Inspector中选中不了资源即Project锁定问题
Idea shortcut key
One copy ten, CVPR oral was accused of plagiarizing a lot, and it was exposed on the last day of the conference!
Informatics Olympiad all in one 1400: count the number of words (string matching)
Smoothing data using convolution