当前位置:网站首页>Very practical shell and shellcheck
Very practical shell and shellcheck
2022-07-29 07:52:00 【sg-expert】
shell Scripting is a powerful tool to help programmers and system administrators complete the tedious work that takes time and effort , It is an effective way to interact with computers and manage files and system operations . Just a few lines of code , You can get the computer close to doing what you want .
Kill all scripts
#!/bin/bash
################################################################
# There are some scripts added to cron In , There are situations where the script has not finished running yet and new tasks need to be executed ,
# Causes the system load to rise , So you can write a script , Select the processes that affect the load and kill them all at once .
################################################################
ps aux|grep Specify the process name |grep -v grep|awk '{print $2}'|xargs kill -9
from FTP Server downloads files
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 filename"
fi
dir=$(dirname $1)
file=$(basename $1)
ftp -n -v << EOF # -n automatic logon
open 192.168.1.10 # ftp The server
user admin password
binary # Set up ftp The transmission mode is binary , avoid MD5 Value is different or .tar.gz Compressed package format error
cd $dir
get "$file"
EOF
Batch modify file name
Example :
# touch article_{1..3}.html
# ls
article_1.html article_2.html article_3.html
Purpose : hold article Change it to bbs
Method 1:
for file in $(ls *html); do
mv $file bbs_${
file#*_}
# mv $file $(echo $file |sed -r 's/.*(_.*)/bbs\1/')
# mv $file $(echo $file |echo bbs_$(cut -d_ -f2)
done
Method 2:
for file in $(find . -maxdepth 1 -name "*html"); do
mv $file bbs_${
file#*_}
done
Method 3:
# rename article bbs *.html
Scan host port status
#!/bin/bash
HOST=$1
PORT="22 25 80 8080"
for PORT in $PORT; do
if echo &>/dev/null > /dev/tcp/$HOST/$PORT; then
echo "$PORT open"
else
echo "$PORT close"
fi
done
Judge whether the user input is IP Address
Method 1:
#!/bin/bash
function check_ip(){
IP=$1
VALID_CHECK=$(echo $IP|awk -F. '$1< =255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
if echo $IP|grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$">/dev/null; then
if [ $VALID_CHECK == "yes" ]; then
echo "$IP available."
else
echo "$IP not available!"
fi
else
echo "Format error!"
fi
}
check_ip 192.168.1.1
check_ip 256.1.1.1
Method 2:
#!/bin/bash
function check_ip(){
IP=$1
if [[ $IP =~ ^[0-9]{
1,3}\.[0-9]{
1,3}\.[0-9]{
1,3}\.[0-9]{
1,3}$ ]]; then
FIELD1=$(echo $IP|cut -d. -f1)
FIELD2=$(echo $IP|cut -d. -f2)
FIELD3=$(echo $IP|cut -d. -f3)
FIELD4=$(echo $IP|cut -d. -f4)
if [ $FIELD1 -le 255 -a $FIELD2 -le 255 -a $FIELD3 -le 255 -a $FIELD4 -le 255 ]; then
echo "$IP available."
else
echo "$IP not available!"
fi
else
echo "Format error!"
fi
}
check_ip 192.168.1.1
check_ip 256.1.1.1
Added version :
Add a dead cycle , If IP Exit when available , Do not use the prompt to continue , And use awk Judge .
#!/bin/bash
function check_ip(){
local IP=$1
VALID_CHECK=$(echo $IP|awk -F. '$1< =255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
if echo $IP|grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" >/dev/null; then
if [ $VALID_CHECK == "yes" ]; then
return 0
else
echo "$IP not available!"
return 1
fi
else
echo "Format error! Please input again."
return 1
fi
}
while true; do
read -p "Please enter IP: " IP
check_ip $IP
[ $? -eq 0 ] && break || continue
done
shellcheck Artifact , No longer afraid of shell It's wrong
Provides an online inspection address ,https://www.shellcheck.net/, Enter the URL in italics to use .
边栏推荐
- 《nlp入门+实战:第五章:使用pytorch中的API实现线性回归》
- [note] the art of research - (tell a good story and argument)
- "Swiss Army Knife" -nc in network tools
- The database uses PSQL and JDBC to connect remotely and disconnect automatically from time to time
- Amaze UI 图标查询
- Cross domain problems when downloading webapi interface files
- [flask introduction series] installation and configuration of flask Sqlalchemy
- Keyboard processing in jetpack compose
- MapReduce steps of each stage
- 关于pip升级损坏导致的问题记录
猜你喜欢

Autojs微信研究:微信自动发送信息机器人最终成品(有效果演示)
![[cryoEM] Introduction to FSC, Fourier shell correlation](/img/01/6a4f6041444abfd1d3ca7e0c14b524.png)
[cryoEM] Introduction to FSC, Fourier shell correlation

Amaze UI icon query
功能自动化测试实施的原则以及方法有哪些?

Amaze UI 图标查询

MySQL 45讲 | 08 事务到底是隔离的还是不隔离的?

MySQL uses date_ FORMAT(date,'%Y-%m')

2022年深圳杯A题破除“尖叫效应”与“回声室效应”走出“信息茧房”

Data unit: bit, byte, word, word length

QT connects two qslite databases and reports an error qsqlquery:: exec: database not open
随机推荐
State machine DP 3D
在js中,0表示false,非0表示true
Use custom annotations to verify the size of the list
Excellent urban design ~ good! Design # visualization radio station will be broadcast soon
Jiamusi Market Supervision Bureau carried out special food safety network training on epidemic and insect prevention
207.课程表
In the MySQL connector of flynk CDC, the MySQL field is varbinary, which is officially
输出1234无重复的三位数
NLP introduction + practice: Chapter 5: using the API in pytorch to realize linear regression
[lecture notes] how to do in-depth learning in poor data?
Write some DP
CDM - code division multiplexing (easy to understand)
330. Complete the array as required
Sort out the two NFT pricing paradigms and four solutions on the market
[summer daily question] Luogu p4413 [coci2006-2007 2] R2
Day 014 二维数组练习
Dilworth 定理
[skill accumulation] presentation practical skill accumulation, common sentence patterns
[cryoelectron microscope] relion4.0 pipeline command summary (self use)
Pat class a 1146 topology sequence