LinuxShell编程完整指南

admin 2026-01-18 02:32:15 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文详细阐述了LinuxShell编程的核心要素,涵盖脚本结构、变量类型、运算符及流程控制。重点讲解了条件判断、循环控制与函数编程技巧,通过丰富代码示例演示了递归、参数传递及错误处理。内容实用,旨在帮助读者掌握Shell开发技能,提升系统运维自动化效率。 综合评分: 80 文章分类: 安全培训


2.2 循环控制

#!/bin/bash
# loop-control.sh

echo"=== Shell循环控制 ==="

# 1. for 循环
echo"1. for循环示例:"

# 数字范围循环
echo"数字循环:"
for i in {1..5}; do
    echo"  数字: $i"
done

# 数组循环
fruits=("苹果""香蕉""橙子""葡萄")
echo"数组循环:"
for fruit in"${fruits[@]}"; do
    echo"  水果: $fruit"
done

# 文件循环
echo"文件循环:"
for file in /etc/*.conf; do
    if [ -f "$file" ]; then
        echo"  配置文件: $(basename "$file")"
    fi
done

# C风格for循环
echo"C风格循环:"
for&nbsp;((i=0; i<5; i++));&nbsp;do
&nbsp; &nbsp;&nbsp;echo" &nbsp;计数:&nbsp;$i"
done

# 2. while 循环
echo&nbsp;-e&nbsp;"\n2. while循环示例:"

# 条件循环
counter=1
echo"条件循环:"
while&nbsp;[&nbsp;$counter&nbsp;-le 5 ];&nbsp;do
&nbsp; &nbsp;&nbsp;echo" &nbsp;计数:&nbsp;$counter"
&nbsp; &nbsp; ((counter++))
done

# 读取文件行
echo"读取文件行:"
while&nbsp;IFS=&nbsp;read&nbsp;-r line;&nbsp;do
&nbsp; &nbsp;&nbsp;echo" &nbsp;行内容:&nbsp;$line"
done&nbsp;< /etc/passwd

# 无限循环(带退出条件)
echo"无限循环(按Ctrl+C退出):"
whiletrue;&nbsp;do
&nbsp; &nbsp;&nbsp;echo" &nbsp;当前时间:&nbsp;$(date)"
&nbsp; &nbsp; sleep 1
done&nbsp;&

# 3. until 循环
echo&nbsp;-e&nbsp;"\n3. until循环示例:"

count=1
echo"until循环:"
until [&nbsp;$count&nbsp;-gt 5 ];&nbsp;do
&nbsp; &nbsp;&nbsp;echo" &nbsp;计数:&nbsp;$count"
&nbsp; &nbsp; ((count++))
done

# 4. 循环控制语句
echo&nbsp;-e&nbsp;"\n4. 循环控制语句:"

# break 示例
echo"break示例:"
for&nbsp;i&nbsp;in&nbsp;{1..10};&nbsp;do
&nbsp; &nbsp;&nbsp;if&nbsp;[&nbsp;$i&nbsp;-eq 6 ];&nbsp;then
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo" &nbsp;遇到6,跳出循环"
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;break
&nbsp; &nbsp;&nbsp;fi
&nbsp; &nbsp;&nbsp;echo" &nbsp;当前值:&nbsp;$i"
done

# continue 示例
echo"continue示例:"
for&nbsp;i&nbsp;in&nbsp;{1..5};&nbsp;do
&nbsp; &nbsp;&nbsp;if&nbsp;[&nbsp;$i&nbsp;-eq 3 ];&nbsp;then
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo" &nbsp;跳过3"
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;continue
&nbsp; &nbsp;&nbsp;fi
&nbsp; &nbsp;&nbsp;echo" &nbsp;当前值:&nbsp;$i"
done

# 5. 嵌套循环
echo&nbsp;-e&nbsp;"\n5. 嵌套循环示例:"

echo"乘法表:"
for&nbsp;i&nbsp;in&nbsp;{1..3};&nbsp;do
&nbsp; &nbsp;&nbsp;for&nbsp;j&nbsp;in&nbsp;{1..3};&nbsp;do
&nbsp; &nbsp; &nbsp; &nbsp; result=$((i * j))
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo&nbsp;-n&nbsp;" &nbsp;$i×$j=$result"
&nbsp; &nbsp;&nbsp;done
&nbsp; &nbsp;&nbsp;echo
done

# 6. 选择循环
echo&nbsp;-e&nbsp;"\n6. select循环(菜单示例):"

PS3="请选择操作 (1-4): "
options=("查看系统信息""查看磁盘使用""查看内存使用""退出")

select opt&nbsp;in"${options[@]}";&nbsp;do
&nbsp; &nbsp;&nbsp;case$REPLYin
&nbsp; &nbsp; &nbsp; &nbsp; 1)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo"系统信息:"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uname -a
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;;
&nbsp; &nbsp; &nbsp; &nbsp; 2)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo"磁盘使用:"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; df -h
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;;
&nbsp; &nbsp; &nbsp; &nbsp; 3)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo"内存使用:"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; free -h
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;;
&nbsp; &nbsp; &nbsp; &nbsp; 4)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo"退出程序"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;break
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;;
&nbsp; &nbsp; &nbsp; &nbsp; *)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo"无效选项"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;;
&nbsp; &nbsp;&nbsp;esac
done

2.3 函数编程

#!/bin/bash
# function-programming.sh

echo"=== Shell函数编程 ==="

# 1. 基本函数定义和调用
say_hello() {
&nbsp; &nbsp;&nbsp;echo"Hello,&nbsp;$1!"
}

echo"1. 基本函数调用:"
say_hello&nbsp;"World"
say_hello&nbsp;"Linux"

# 2. 函数参数
calculate_sum() {
&nbsp; &nbsp;&nbsp;local&nbsp;sum=0
&nbsp; &nbsp;&nbsp;for&nbsp;num&nbsp;in"$@";&nbsp;do
&nbsp; &nbsp; &nbsp; &nbsp; ((sum += num))
&nbsp; &nbsp;&nbsp;done
&nbsp; &nbsp;&nbsp;echo$sum
}

echo&nbsp;-e&nbsp;"\n2. 函数参数处理:"
result=$(calculate_sum 1 2 3 4 5)
echo"1+2+3+4+5 =&nbsp;$result"

# 3. 函数返回值
is_even() {
&nbsp; &nbsp;&nbsp;local&nbsp;num=$1
&nbsp; &nbsp;&nbsp;if&nbsp;[ $((num % 2)) -eq 0 ];&nbsp;then
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;0 &nbsp;# 偶数,返回成功
&nbsp; &nbsp;&nbsp;else
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;1 &nbsp;# 奇数,返回失败
&nbsp; &nbsp;&nbsp;fi
}

echo&nbsp;-e&nbsp;"\n3. 函数返回值:"
if&nbsp;is_even 10;&nbsp;then
&nbsp; &nbsp;&nbsp;echo"10是偶数"
else
&nbsp; &nbsp;&nbsp;echo"10是奇数"
fi

if&nbsp;is_even 7;&nbsp;then
&nbsp; &nbsp;&nbsp;echo"7是偶数"
else
&nbsp; &nbsp;&nbsp;echo"7是奇数"
fi

# 4. 局部变量
variable_scope() {
&nbsp; &nbsp;&nbsp;local&nbsp;local_var="局部变量"
&nbsp; &nbsp; global_var="全局变量"
&nbsp; &nbsp;&nbsp;echo"函数内: local_var=$local_var, global_var=$global_var"
}

echo&nbsp;-e&nbsp;"\n4. 变量作用域:"
global_var="初始值"
echo"函数前: global_var=$global_var"
variable_scope
echo"函数后: global_var=$global_var"
# echo "函数后: local_var=$local_var" &nbsp;# 这会报错

# 5. 递归函数
factorial() {
&nbsp; &nbsp;&nbsp;local&nbsp;n=$1
&nbsp; &nbsp;&nbsp;if&nbsp;[&nbsp;$n&nbsp;-le 1 ];&nbsp;then
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo&nbsp;1
&nbsp; &nbsp;&nbsp;else
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;local&nbsp;prev=$(factorial $((n-1)))
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo&nbsp;$((n * prev))
&nbsp; &nbsp;&nbsp;fi
}

echo&nbsp;-e&nbsp;"\n5. 递归函数:"
echo"5! =&nbsp;$(factorial 5)"
echo"10! =&nbsp;$(factorial 10)"

# 6. 函数库示例
# 模拟一个数学函数库
math_lib() {
&nbsp; &nbsp;&nbsp;case$1in
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;"add")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo&nbsp;$(($2&nbsp;+&nbsp;$3))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;;
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;"sub")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo&nbsp;$(($2&nbsp;-&nbsp;$3))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;;
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;"mul")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo&nbsp;$(($2&nbsp;*&nbsp;$3))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;;
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;"div")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;[&nbsp;$3&nbsp;-ne 0 ];&nbsp;then
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo&nbsp;$(($2&nbsp;/&nbsp;$3))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;else
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo"错误: 除数不能为0"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;1
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;fi
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;;
&nbsp; &nbsp; &nbsp; &nbsp; *)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo"未知操作:&nbsp;$1"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;1
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;;
&nbsp; &nbsp;&nbsp;esac
}

echo&nbsp;-e&nbsp;"\n6. 函数库使用:"
echo"10 + 20 =&nbsp;$(math_lib add 10 20)"
echo"50 - 30 =&nbsp;$(math_lib sub 50 30)"
echo"6 * 7 =&nbsp;$(math_lib mul 6 7)"
echo"100 / 5 =&nbsp;$(math_lib div 100 5)"

# 7. 匿名函数(使用子shell)
echo&nbsp;-e&nbsp;"\n7. 匿名函数(子shell):"
result=$(
&nbsp; &nbsp;&nbsp;local&nbsp;x=5
&nbsp; &nbsp;&nbsp;local&nbsp;y=3
&nbsp; &nbsp;&nbsp;echo&nbsp;$((x * y))
)
echo"5 * 3 =&nbsp;$result"

# 8. 函数作为参数
apply_function() {
&nbsp; &nbsp;&nbsp;local&nbsp;func=$1
&nbsp; &nbsp;&nbsp;local&nbsp;value=$2
&nbsp; &nbsp;&nbsp;$func"$value"
}

double() {
&nbsp; &nbsp;&nbsp;echo&nbsp;$(($1&nbsp;* 2))
}

square() {
&nbsp; &nbsp;&nbsp;echo&nbsp;$(($1&nbsp;*&nbsp;$1))
}

echo&nbsp;-e&nbsp;"\n8. 函数作为参数:"
echo"double(5) =&nbsp;$(apply_function double 5)"
echo"square(4) =&nbsp;$(apply_function square 4)"

# 9. 错误处理函数
error_handler() {
&nbsp; &nbsp;&nbsp;echo"错误发生在:&nbsp;${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
&nbsp; &nbsp;&nbsp;echo"错误信息:&nbsp;$1"
&nbsp; &nbsp;&nbsp;exit&nbsp;1
}

safe_division() {
&nbsp; &nbsp;&nbsp;if&nbsp;[&nbsp;$2&nbsp;-eq 0 ];&nbsp;then
&nbsp; &nbsp; &nbsp; &nbsp; error_handler&nbsp;"除数不能

免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:运维星火燎原 刘军军 刘军军《Linux Shell编程完整指南》

LinuxShell编程完整指南 网络安全文章

LinuxShell编程完整指南

文章总结: 本文详细阐述了LinuxShell编程的核心要素,涵盖脚本结构、变量类型、运算符及流程控制。重点讲解了条件判断、循环控制与函数编程技巧,通过丰富代码
评论:0   参与:  0