249 Ready-to-Use Shell Scripts to Boost Your Linux Ops Skills
Discover a curated collection of 249 practical shell script examples, complete with clear documentation and usage guidelines, designed to help Linux operations engineers improve efficiency, master scripting conventions, and quickly solve common admin tasks, all available for free download via the provided QR code.
As a Linux operations engineer, writing good scripts can improve work efficiency and free up time for other activities.
This article shares a collection of 249 ready‑to‑use shell script cases, encouraging readers to study, practice, and master scripting.
The 249 scripts span 147 pages, with a clear directory, searchable indexing, and copy‑ready code, making the electronic version ideal for learning, skill enhancement, and interview preparation.
1. Create Linux system account and password using positional parameters
#!/bin/bash
# 通过位置变量创建 Linux 系统账户及密码
#$1 是执行脚本的第一个参数,$2 是执行脚本的第二个参数
useradd "$1"
echo "$2" | passwd --stdin "$1"2. Backup logs weekly
#!/bin/bash
# 每周 5 使用 tar 命令备份/var/log 下的所有日志文件
# vim /root/logbak.sh
# 编写备份脚本,备份后的文件名包含日期标签,防止后面的备份将前面的备份数据覆盖
# 注意 date 命令需要使用反引号括起来,反引号在键盘<tab>键上面
tar -czf log-`date +%Y%m%d`.tar.gz /var/log
# crontab ‑e #编写计划任务,执行备份脚本
00 03 * * 5 /root/logbak.sh3. Input three numbers and sort them in ascending order
#!/bin/bash
# 依次提示用户输入 3 个整数,脚本根据数字大小依次排序输出 3 个数字
read -p "请输入一个整数:" num1
read -p "请输入一个整数:" num2
read -p "请输入一个整数:" num3
# 确保 num1 为最小值
if [ $num1 -gt $num2 ]; then
tmp=$num1
num1=$num2
num2=$tmp
fi
if [ $num1 -gt $num3 ]; then
tmp=$num1
num1=$num3
num3=$tmp
fi
# 确保 num2 为中间值
if [ $num2 -gt $num3 ]; then
tmp=$num2
num2=$num3
num3=$tmp
fi
echo "排序后数据(从小到大)为:$num1,$num2,$num3"4. Random name picker script
#!/bin/bash
# 编写一个点名器脚本
# 该脚本,需要提前准备一个 user.txt 文件
# 该文件中需要包含所有姓名的信息,一行一个姓名,脚本每次随机显示一个姓名
while :
do
line=$(cat user.txt | wc -l)
num=$[RANDOM%line+1]
sed -n "${num}p" user.txt
sleep 0.2
clear
done5. Sum of all positive integers up to 100
#!/bin/bash
# 对 100 以内的所有正整数相加求和(1+2+3+...+100)
sum=0
for i in `seq 100`
do
sum=$[sum+i]
done
echo "总和是:$sum"6. Count login‑capable accounts on the current Linux system
#!/bin/bash
# 统计当前 Linux 系统中可以登录计算机的账户有多少个
#方法 1:
grep "bash$" /etc/passwd | wc -l
#方法 2:
awk -F: '/bash$/{x++}END{print x}' /etc/passwd7. Print various date and time formats
#!/bin/bash
# 打印各种时间格式
echo "显示星期简称(如:Sun)"
date +%a
echo "显示星期全称(如:Sunday)"
date +%A
echo "显示月份简称(如:Jan)"
date +%b
echo "显示月份全称(如:January)"
date +%B
echo "显示数字月份(如:12)"
date +%m
echo "显示数字日期(如:01)"
date +%d
echo "显示数字年(如:2023)"
date +%Y
echo "显示年‑月‑日"
date +%F
echo "显示小时(24 小时制)"
date +%H
echo "显示分钟(00..59)"
date +%M
echo "显示秒"
date +%S
echo "显示纳秒"
date +%N
echo "组合显示"
date +"%Y%m%d %H:%M:%S"Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
