Windows batch/shell script commonly used commands

Port occupancy/process processing/script calling

Windows

rem 查找占用某个端口号的应用的PID
netstat -ano | findstr "8895"

rem 通过PID反查应用的名称
tasklist | findstr "22224"

rem 杀掉某个进程
taskkill /f /t /im "demo.exe"

tasklist | find>nul 2>nul /i "demo.exe"
if %errorlevel%==0 (
taskkill>nul 2>nul /im demo.exe /f
)

rem 程序后台运行
start>nul 2>nul /B "" demo.exe

rem 调用脚本
call other.bat

rem 调用跳转
if /i "%~1" equ "client" goto client

:client
echo client mode

Linux

# 查找占用端口的应用
netstat -tuln | grep <端口号>
lsof -i :<端口号>

# 查找特定名称的应用并删除
ps -ef | grep test_name | grep -v grep | awk '{print $2}' | xargs kill -9  

# 统计某个应用的个数
process_count=`ps -elf |grep -w myapp |grep -v 'grep' | wc -l`

# 杀掉某个进程
process_pid=`ps -ef | grep myapp | grep -v 'grep' | awk '{print $2}'`
kill -9 $process_pid

# 程序后台运行
nohup ./myapp > /dev/null 2>&1 &

# 调用脚本
./other.sh

Handle command line arguments

Windows

REM 获取输入变量的个数
set var_count=0
for %%x in (%*) do set /a var_count+=1

REM 获取所有变量
%*

REM 使用第几个变量
%~1 %~2 %~3 %~4  

Linux

# 获取变量的个数
$#  
if [ $# -ne 6 ]; then
echo $#
fi

# 获取所有输入的变量
args=("$@")
for arg in "${args[@]}"
do
    echo "input arguments:$arg"
done

# 使用第几个变量
$1 $2 $3 $4

Compare and judge the size

Windows

rem 定于两个变量
set var1=10
set var2=20

if %var1% gtr %var2% (
    echo %var1% 大于 %var2%.
) else if %var1% lss %var2% (
    echo %var1% 小于 %var2%.
) else (
    echo %var1% 等于 %var2%.
)

if "%var1%" == "%var2%" (
    echo 变量 var1 和 var2 相等。
) else (
    echo 变量 var1 和 var2 不相等。
)

Linux

# 定义两个变量
num1=10
num2=20

# 使用条件语句判断大小
if [ $num1 -eq $num2 ]; then
    echo "$num1 等于 $num2"
elif [ $num1 -ne $num2 ]; then
    echo "$num1 不等于 $num2"
elif [ $num1 -gt $num2 ]; then
    echo "$num1 大于 $num2"
elif [ $num1 -lt $num2 ]; then
    echo "$num1 小于 $num2"
elif [ $num1 -ge $num2 ]; then
    echo "$num1 大于等于 $num2"    
elif [ $num1 -le $num2 ]; then
    echo "$num1 小于等于 $num2"          
fi

if [ "$num1" = "$num2" ]; then
    echo "变量 num1 和 num2 相等。"
else
    echo "变量 num1 和 num2 不相等。"
fi

close terminal output

windows

@echo off

Linux

echo "$var1" >/dev/null 2>&1

Hibernation/character replacement/judging whether the file exists/file replacement

Windows

rem 休眠
TIMEOUT /T 3 

rem 字符替换
set client_path=client.ini
sed -i  "/^server_name = /c server_name = %~1" %client_path%

rem 文件是否存在
set file_path=file.txt
IF EXIST "%file_path" (
  del %file_path%
)

rem 文件替换(存在则替换)
copy /y sourcefile targetfile

Linux

# 休眠
sleep 2

# 字符替换
client_config_path="./client.ini"
sed -i  "/^server_name = /c server_name = $1" ${client_config_path}

# 文件是否存在
filename="example.txt"
if [ -e "$filename" ]; then
    echo "文件存在"
else
    echo "文件不存在"
fi

# 文件替换(存在则替换)
cp -rf sourcefile targetfile

get the current path

Windows

%0       批处理文件自身(绝对路径"D:\test\test.bat",注意有双引号)
%~d0     是指批处理所在的盘符,其中d代表drive
%~p0     是指批处理所在的目录(扩充到路径 \test\)
%~dp0    是批处理所在的盘符加路径(D:\test\)
%cd%     来获取当前路径

Linux

# 获取当前路径
current_path=$(pwd)
SHELL_FOLDER=`pwd`
echo "当前路径:$current_path"

# 获取脚本文件所在目录
script_path=$(dirname "$0")
echo "脚本文件所在目录:$script_path"

other commands

Windows PowerShell command to view the size of all subdirectories under a certain directory

Get-ChildItem -Path "C:\Users\username\AppData\Local" -Directory | ForEach-Object{
    
    $f=$_;Get-ChildItem -Path $f.FullName -Recurse -File | Measure-Object -Property Length -Sum | Select-Object @{
    
    N="Name";E={
    
    $f.Name}}, @{
    
    N="Size";E={
    
    "{0:N2} MB" -f ($_.Sum / 1MB)}}}

The windows bat command to view the size of all subdirectories:

for /f "usebackq delims=:" %d in (`dir D:\Folder /a /w /b`) do @echo %d: && @dir /s /a /w /b "D:\Folder\%d"|find /v ""|find "File(s)" && @echo.

Linux closes the firewall

#关闭防火墙
sudo systemctl stop iptables
sudo systemctl stop firewalld
sudo ufw disable

Linux modify dependent library directory

#将依赖库的路径添加到环境变量中
path=`pwd`
export LD_LIBRARY_PATH=${path}/depend:$LD_LIBRARY_PATH
./executable

Linux start up

# 将自己的脚本添加到这个文件中
/etc/rc.local

# 使用crontab计划任务
crontab -e 
@reboot /home/ok/auto_run_script.sh

#使用 systemd 服务  
vim auto_run_script.service  
[Unit]
Description=Run a Custom Script at Startup
After=default.target

[Service]
ExecStart=/home/ok/auto_run_script.sh

[Install]
WantedBy=default.target

Put this script in the /etc/systemd/system/ directory, and then we run the following two commands to update the systemd configuration file and start the service

systemctl daemon-reload
systemctl enable auto_run_script.service

Linux periodically deletes log files

while true
do
    # 超过30天的日志删除
	find ./media/log/ -atime +30 -name "*.log" -exec rm -rf {
    
    } \;
    # 30天内的日志大于100M则删除
	find ./media/log/ -atime -30 -name "*.log" -size +100M -exec rm -rf {
    
    } \;
	sleep 12h
done

Linux setting version number

read -p "请输入版本号" version  
echo -n $version > version.txt  

Create a Linux directory if it does not exist

if  [  ! -d $dir_path ]; then
mkdir $dir_path
fi

Guess you like

Origin blog.csdn.net/yang1fei2/article/details/131738712