当前位置:网站首页>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语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型、使用summary函数查看简单模型的汇总统计信息
- 获取两个dataframe的交并差集
- Kubernetes的pod调度
- Attention meets Geometry:几何引导的时空注意一致性自监督单目深度估计
- 一键分析硬件/IO/全国网络性能脚本(强推)
- Informatics Olympiad all in one 1400: count the number of words (string matching)
- 打新债注册开户安全吗,有没有什么风险?
- 文献1
- Is it safe to open an online stock account? Somebody give me an answer
- 710. 黑名单中的随机数
猜你喜欢

Notes on writing questions in C language -- table tennis competition

teamviewer显示设备数量上限解决方法

qt下多个子控件信号槽绑定方法

数据库-视图

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

15 BS object Node name Node name String get nested node content

Unity 利用Skybox Panoramic着色器制作全景图预览有条缝隙问题解决办法

C语言刷题随记 —— 乒乓球比赛

feil_uVission4左侧工目录消失

1.会计基础--会计的几大要素(会计总论、会计科目和账户)
随机推荐
Unity C# 网络学习(十)——UnityWebRequest(二)
Cluster addslots establish a cluster
Declaration and assignment of go variables
Use of subqueries
This is the graceful file system mounting method, which is effective through personal testing
一篇抄十篇,CVPR Oral被指大量抄袭,大会最后一天曝光!
[solo π] ADB connects multiple mobile phones
The heavyweight white paper was released. Huawei continues to lead the new model of smart park construction in the future
[cloud native] codeless IVX editor programmable by "everyone"
Get the intersection union difference set of two dataframes
Use abp Zero builds a third-party login module (II): server development
【雲原生】 ”人人皆可“ 編程的無代碼 iVX 編輯器
Pytorch深度学习代码技巧
Mark一下 Unity3d在Inspector中选中不了资源即Project锁定问题
同花顺注册开户安全吗,有没有什么风险?
R语言dplyr包bind_rows函数把两个dataframe数据的行纵向(竖直)合并起来、最终行数为原来两个dataframe行数的加和(Combine Data Frames)
redis集群的重新分片与ASK命令
Solution to the upper limit of TeamViewer display devices
Sikuli 基于图形识别的自动化测试技术
R语言glm函数逻辑回归模型、使用epiDisplay包logistic.display函数获取模型汇总统计信息(自变量初始和调整后的优势比及置信区间,回归系数的Wald检验的p值)、结果保存到csv