shell脚本

打印hello world

#!/bin/bash
echo "Hello, World!"

变量与计算

#!/bin/bash
name="ChatMind"
num1=10
num2=5
sum=$((num1 + num2))
echo "$name says: Sum is $sum"

遍历目录并统计文件

批量重命名文件(添加后缀)--for语句

!/bin/bash

for file in *.txt; do
mv "$file" "${file%.txt}.bak"
done

if语句

!/bin/bash

for file in /path/to/dir/*; do
if [ -f "$file" ]; then
echo "文件: $file | 大小: $(du -h "$file" | cut -f1)"
fi
done

检查服务状态

!/bin/bash

if systemctl is-active --quiet nginx; then
echo "Nginx正在运行"
else
echo "Nginx未运行"
fi

批量Ping测试

!/bin/bash

for ip in 192.168.10.{1..10};do
ping ip
echo "$ip"
if ip==192.168.10.10;then
echo "$ip"
fi
done

posted @ 2025-06-12 10:15  呆呆酱  阅读(6)  评论(0)    收藏  举报