赞
踩
大家好,我是一位永远怀着学习的心态的Linux和Python技术持续学习者,在今天的文章中,我将向大家分享一些有趣而实用的Linux Shell脚本编程例子,通过这些例子,你可以系统地学习和掌握Shell脚本编程,从入门到精通。让我们开始这个令人兴奋的编程之旅吧!
在跟随本教程的过程中,你需要准备以下环境:
一台运行Linux操作系统的计算机
任何一种基本的文本编辑器,如Vim、Nano等
初识Shell脚本:Hello World!
- #!/bin/bash
- echo "Hello World!"
打印当前用户信息:
- #!/bin/bash
- echo "当前用户:$USER"
- echo "主目录:$HOME"
- echo "当前工作目录:$PWD"
计算两个数的和:
- #!/bin/bash
- echo "请输入第一个数:"
- read num1
- echo "请输入第二个数:"
- read num2
- sum=$((num1 + num2))
- echo "两个数的和为:$sum"
列出当前目录下的所有文件:
- #!/bin/bash
- for file in $(ls); do
- echo "$file"
- done
列出当前目录下的所有文件夹:
- #!/bin/bash
- for dir in $(ls -d */); do
- echo "$dir"
- done
获取当前日期和时间:
- #!/bin/bash
- now=$(date +"%Y-%m-%d %H:%M:%S")
- echo "当前时间为:$now"
循环输出数字1到10:
- #!/bin/bash
- for (( i=1; i<=10; i++ )); do
- echo "$i"
- done
提示用户输入姓名并打印出来:
- #!/bin/bash
- echo -n "请输入您的姓名:"
- read name
- echo "您的姓名是:$name"
模拟一个简单的计算器:
- #!/bin/bash
- echo "请输入第一个数:"
- read num1
- echo "请输入第二个数:"
- read num2
- echo "请输入要执行的运算符(+、-、*、/):"
- read operator
- result=$(echo "$num1 $operator $num2" | bc)
- echo "结果为:$result"
拷贝当前目录下所有文件到另一个目录:
- #!/bin/bash
- echo "请输入要拷贝的目录:"
- read source_dir
- echo "请输入目标目录:"
- read target_dir
- cp -r "$source_dir"/* "$target_dir"
- echo "拷贝完成!"
判断一个文件是否存在:
- #!/bin/bash
- echo -n "请输入要查询的文件名:"
- read filename
- if [ -e "$filename" ]; then
- echo "$filename 存在"
- else
- echo "$filename 不存在"
- fi
判断当前用户是否是root:
- #!/bin/bash
- if [ $(id -u) = 0 ]; then
- echo "当前用户是root"
- else
- echo "当前用户不是root"
- fi
计算一个目录下所有文件的总大小:
- #!/bin/bash
- echo "请输入目录路径:"
- read dirpath
- size=$(du -sh "$dirpath" | cut -f1)
- echo "$dirpath 目录大小为:$size"
随机生成一个6位数字的验证码:
- #!/bin/bash
- code=$(shuf -i 100000-999999 -n 1)
- echo "随机验证码为:$code"
查找一个目录下所有拥有写权限的文件:
- #!/bin/bash
- echo "请输入目录路径:"
- read dirpath
- find "$dirpath" -type f -writable
16.使用alias命令设置一个自定义命令:
- #!/bin/bash
- alias ll='ls -alh'
统计一个文本文件中指定字符串的出现次数:
- #!/bin/bash
- echo "请输入文件路径:"
- read filepath
- echo "请输入要统计的字符串:"
- read searchstr
- count=$(grep -o "$searchstr" "$filepath" | wc -l)
- echo "$filepath 中 $searchstr 出现的次数为:$count"
计算一个数组元素的和:
- #!/bin/bash
- array=(1 2 3 4 5)
- sum=0
- for i in ${array[@]}
- do
- sum=$(($sum + $i))
- done
- echo "数组元素的和为:$sum"
批量重命名当前目录下的所有文件:
- #!/bin/bash
- echo "请输入文件名的前缀:"
- read prefix
- echo "请输入文件名的后缀:"
- read suffix
- for file in *
- do
- mv "$file" "$prefix$file$suffix"
- done
- echo "文件重命名完成!"
查找一个目录下最后修改时间超过7天的文件:
- #!/bin/bash
- echo "请输入目录路径:"
- read dirpath
- find "$dirpath" -type f -mtime +7
获取当前日期和时间:
- #!/bin/bash
- current_date=$(date +%Y-%m-%d)
- current_time=$(date +%H:%M:%S)
- echo "当前日期:$current_date"
- echo "当前时间:$current_time"
判断一个数是否为素数:
- #!/bin/bash
- echo "请输入一个正整数:"
- read num
- is_prime=1
- for ((i=2; i<$num; i++))
- do
- if (( $num % $i == 0 ))
- then
- is_prime=0
- break
- fi
- done
- if (( $is_prime == 1 ))
- then
- echo "$num 是素数"
- else
- echo "$num 不是素数"
- fi

获取当前机器的CPU信息:
- #!/bin/bash
- cpu_info=$(cat /proc/cpuinfo)
- echo "$cpu_info"
获取当前机器的内存信息:
- #!/bin/bash
- mem_info=$(free -h)
- echo "$mem_info"
统计一个目录下所有.c文件的代码行数:
- #!/bin/bash
- echo "请输入目录路径:"
- read dirpath
- find "$dirpath" -name "*.c" | xargs wc -l
26.下载一个网页并将其转换为PDF文件:
- #!/bin/bash
- echo "请输入网页链接:"
- read webpage
- filename=$(echo "$webpage" | awk -F/ '{print $NF}')
- wget "$webpage" -O "$filename".html
- wkhtmltopdf "$filename".html "$filename".pdf
27.查找一个目录下文件名包含指定字符串的文件:
- #!/bin/bash
- echo "请输入目录路径:"
- read dirpath
- echo "请输入要查找的字符串:"
- read searchstr
- find "$dirpath" -name "*$searchstr*"
28.备份指定目录并打包成tar.gz格式:
- #!/bin/bash
- echo "请输入要备份的目录:"
- read sourcedir
- echo "请输入备份存放位置:"
- read targetdir
- backupfile=$(date +%Y%m%d_%H%M%S)_backup.tar.gz
- tar -czvf "$targetdir/$backupfile" "$sourcedir"
- echo "备份完成!"
29.批量添加用户并设置初始密码
- #!/bin/bash
- for i in {1..5}; do
- username="user$i"
- useradd -m -s /bin/bash "$username"
- echo "设置 $username 的密码为:password$i"
- echo "password$i" | passwd --stdin "$username"
- done
30.统计一个文本文件中的单词数:
- #!/bin/bash
- echo "请输入文件路径:"
- read filepath
- wordcount=$(grep -o '\w\+' "$filepath" | wc -l)
- echo "该文件中的单词数量为:$wordcount"
通过这篇文章,我们带你从零开始玩转Linux Shell脚本编程,并且给出了30个丰富多样的实例,涵盖了Shell脚本编程的基础知识和常见应用场景。希望这些例子能够帮助你系统地学习和掌握Shell脚本编程,成为Shell大神!
如果你对Linux和Python技术感兴趣,欢迎关注并分享我们的微信公众号"运维家"(搜索"运维家"或扫描下方二维码),我们将持续为大家带来更多有趣、实用的技术文章。
欢迎分享、留言、提问,我将竭诚为你解答。让我们一起在技术的世界里探索前行!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。