shell脚本编程100例
时间: 2023-09-13 18:08:31 浏览: 125
很抱歉,我无法提供完整的100例shell脚本编程示例,但我可以为您提供一些常见的shell脚本编程示例:
1. 输出“Hello World!”
```
#!/bin/bash
echo "Hello World!"
```
2. 输出当前时间和日期
```
#!/bin/bash
echo "The current date and time is: $(date)"
```
3. 遍历目录下的文件并输出文件名
```
#!/bin/bash
for file in /path/to/directory/*
do
echo "$file"
done
```
4. 输出系统中的所有环境变量
```
#!/bin/bash
printenv
```
5. 检查文件是否存在并输出文件内容
```
#!/bin/bash
if [ -f /path/to/file.txt ]
then
cat /path/to/file.txt
else
echo "File does not exist."
fi
```
6. 使用函数计算两个数字的和
```
#!/bin/bash
function add_numbers {
sum=$(($1 + $2))
echo "The sum of $1 and $2 is: $sum"
}
add_numbers 5 10
```
7. 使用case语句根据用户输入执行不同的操作
```
#!/bin/bash
echo "Enter a number between 1 and 3:"
read number
case $number in
1) echo "You entered 1.";;
2) echo "You entered 2.";;
3) echo "You entered 3.";;
*) echo "Invalid input.";;
esac
```
这些示例只是shell脚本编程的冰山一角,希望能为您提供帮助。