Learn Shell Scripting: From Hello World to Practical Tools
This tutorial walks beginners through creating and executing Bash scripts on Linux, covering basics like the shebang, variables, conditionals, loops, functions, and debugging, and provides practical examples such as file checks, service monitoring, and user interaction to build useful automation tools.
The article is a step‑by‑step guide for beginners to learn Shell scripting on Linux. It encourages readers to bookmark and practice the examples so they can move from merely understanding scripts to writing them confidently.
Step 1: Write the simplest script
#!/bin/bash
echo "你好,Shell 脚本!"Create a file (e.g., hello.sh ), paste the above content, save, make it executable with chmod +x hello.sh , and run it with ./hello.sh . The output will be "你好,Shell 脚本!".
Step 2: Variables are essential
#!/bin/bash
name="小白DBA"
echo "你好,$name"Save as var.sh , give execute permission, and run. Important notes: assignment must not contain spaces ( name="value" ), double quotes allow variable expansion, while single quotes keep the literal string.
Step 3: Add conditionals
Example – check if a file exists:
#!/bin/bash
if [ -f /etc/passwd ]; then
echo "文件存在"
else
echo "文件不存在"
fi-f tests for a regular file, [] is the test syntax, and then / fi delimit the block. Another example shows reading user input and comparing it to "root".
Step 4: Loops for batch operations
Ping a range of IPs:
#!/bin/bash
for ip in 192.168.1.{1..5}
do
ping -c 1 $ip > /dev/null
if [ $? -eq 0 ]; then
echo "$ip 通了"
else
echo "$ip 不通"
fi
doneKey points: for ... in syntax, {1..5} expands to 1‑5, $? is the previous command’s exit status, and > /dev/null suppresses ping output.
Step 5: Functions for modularity
#!/bin/bash
check_port() {
if ss -tunlp | grep ":$1" > /dev/null; then
echo "端口 $1 正在监听"
else
echo "端口 $1 未打开"
fi
}
check_port 22
check_port 80The check_port function receives a port number as $1 and reports whether it is being listened on.
Step 6: Real‑world case – service status checker
#!/bin/bash
SERVICES=("sshd" "mysqld" "nginx")
for svc in "${SERVICES[@]}"
do
systemctl is-active --quiet $svc
if [ $? -eq 0 ]; then
echo "$svc 运行中"
else
echo "$svc 没有运行"
fi
doneThis script iterates over an array of service names, uses systemctl is-active --quiet to test each, and prints whether it is running.
Step 7: User interaction and arguments
#!/bin/bash
if [ $# -lt 1 ]; then
echo "用法:$0 <文件路径>"
exit 1
fi
file=$1
if [ -f "$file" ]; then
echo "$file 是一个文件"
else
echo "$file 不存在"
fiExplanation of special variables: $# (argument count), $0 (script name), $1 (first argument), and exit 1 to terminate with an error.
Step 8: Debugging tricks
Run a script with tracing:
bash -x myscript.shOr insert set -x at the start of a script and set +x to stop tracing.
What to learn next?
Try writing your own small scripts, such as batch backing up files, scheduled log cleanup with crontab , disk‑usage monitoring with alerts, or an interactive menu‑driven script.
END
IT Xianyu
We share common IT technologies (Java, Web, SQL, etc.) and practical applications of emerging software development techniques. New articles are posted daily. Follow IT Xianyu to stay ahead in tech. The IT Xianyu series is being regularly updated.
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.