当前位置:网站首页>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 .
边栏推荐
- IonIcons图标大全
- Sqlmap(SQL注入自动化工具)
- Analyze the roadmap of 25 major DFI protocols and predict the seven major trends in the future of DFI
- Data unit: bit, byte, word, word length
- Realize the effect of changing some colors of a paragraph of text
- MapReduce各阶段步骤
- [skill accumulation] common expressions when writing emails
- Starting process of raspberry pie
- 写点dp
- [cryoelectron microscope | paper reading] emclarity: software for high-resolution cryoelectron tomography and sub fault averaging
猜你喜欢

Analyze the roadmap of 25 major DFI protocols and predict the seven major trends in the future of DFI

My entrepreneurial neighbors

RoBERTa:A Robustly Optimized BERT Pretraining Approach

Mutationobserver document learning

Sqlmap(SQL注入自动化工具)

【无标题】格式保存

Cs61abc sharing session (VI) detailed explanation of program input and output - standard input and output, file, device, EOF, command line parameters

Prometheus and grafana

Effective learning of medical image segmentation annotation based on noise pseudo tags and adversarial learning

Log4qt memory leak, use of heob memory detection tool
随机推荐
Write some DP
RoBERTa:A Robustly Optimized BERT Pretraining Approach
Zip gzip tar compression Advanced Edition
Autojs微信研究:微信自动发送信息机器人最终成品(有效果演示)
Prepare esp32 environment
【深度学习】数据准备-pytorch自定义图像分割类数据集加载
黑盒测试常见错误类型说明及解决方法有哪些?
CDM - code division multiplexing (easy to understand)
postman接口测试|js脚本之阻塞休眠和非阻塞休眠
[cryoelectron microscope] relation4.0 - subtomogram tutorial
My entrepreneurial neighbors
Why don't you like it? It's easy to send email in cicd
NLP introduction + practice: Chapter 5: using the API in pytorch to realize linear regression
《nlp入门+实战:第五章:使用pytorch中的API实现线性回归》
Monitor the bottom button of page scrolling position positioning (including the solution that page initialization positioning does not take effect on mouse sliding)
MapReduce各阶段步骤
[paper reading] tomoalign: a novel approach to correcting sample motion and 3D CTF in cryoet
[flask introduction series] installation and configuration of flask Sqlalchemy
在js中,0表示false,非0表示true
Go 事,如何成为一个Gopher ,并在7天找到 Go 语言相关工作,第1篇