[ Shell ] 示例 shell脚本

一、示例 

        其实shell脚本的编写和c语言是非常类似的,因此我们直接给出一个参考的shell脚本来初步了解记录如何编写shell script,之后我们来慢慢讲解其中的内容。 

如何运行此脚本

  1. 将上面的脚本保存到一个文件中,例如命名为 script_example.sh
  2. 给文件添加执行权限:chmod +x script_example.sh
  3. 运行脚本:./script_example.sh
#!/bin/bash

# 定义一个函数来打印欢迎信息
welcome() {
    echo "Welcome to the shell script tutorial!"
}

# 调用欢迎函数
welcome

# 使用 if 语句检查用户是否输入了参数
if [ "$#" -eq 1 ]; then
    echo "You passed one argument: $1"
else
    echo "You did not pass any arguments."
fi

# 使用 for 循环遍历一组数字
echo "Counting from 1 to 5 using for loop:"
for i in {1..5}
do
    echo $i
done

# 使用 while 循环遍历一组数字
echo "Counting from 1 to 5 using while loop:"
j=1
while [ $j -le 5 ]
do
    echo $j
    ((j++))
done

# 使用 case 语句处理不同的选项
echo "Choose an option:"
read choice
case $choice in
    1)
        echo "You chose option 1."
        ;;
    2)
        echo "You chose option 2."
        ;;
    *)
        echo "Invalid option."
        ;;
esac

# 使用 select 语句提供菜单选择
echo "Select an option:"
options=("Option 1" "Option 2" "Option 3")
select opt in "${options[@]}"
do
    case $opt in
        "Option 1")
            echo "You selected Option 1."
            break
            ;;
        "Option 2")
            echo "You selected Option 2."
            break
            ;;
        "Option 3")
            echo "You selected Option 3."
            break
            ;;
        *) echo "Invalid option";;
    esac
done

# 处理文件和目录
echo "Creating a temporary directory and file..."
temp_dir="/tmp/temp_dir"
mkdir -p "$temp_dir"
touch "$temp_dir/test_file.txt"

# 输出文件内容
echo "Writing content to the file..."
echo "This is a test file." > "$temp_dir/test_file.txt"
cat "$temp_dir/test_file.txt"

# 使用 if 语句检查文件是否存在
if [ -f "$temp_dir/test_file.txt" ]; then
    echo "File exists."
else
    echo "File does not exist."
fi

# 使用 if 语句检查目录是否存在
if [ -d "$temp_dir" ]; then
    echo "Directory exists."
else
    echo "Directory does not exist."
fi

# 清除临时目录
rm -rf "$temp_dir"

# 使用函数
echo "Defining and calling a function..."
function hello_world {
    echo "Hello, World!"
}
hello_world

# 使用数组
echo "Using arrays..."
numbers=(1 2 3 4 5)
echo "Array elements: ${numbers[@]}"

# 使用字符串
echo "String manipulation..."
str="Hello, World!"
echo "Length of string: ${#str}"
echo "Substring: ${str:7:5}"

# 使用变量
echo "Using variables..."
name="Alice"
age=30
echo "Name: $name, Age: $age"

# 使用算术运算
echo "Arithmetic operations..."
((sum = 10 + 20))
echo "Sum: $sum"

# 使用环境变量
echo "Environment variables..."
echo "Home directory: $HOME"

# 使用外部命令
echo "Executing external commands..."
echo "Current date and time: $(date)"

# 使用管道
echo "Using pipes..."
echo "First ten lines of /etc/passwd:"
head -n 10 /etc/passwd | grep -v '^#'

# 使用重定向
echo "Redirecting output..."
echo "Redirecting 'ls' command output to a file..."
ls > /tmp/output.txt
cat /tmp/output.txt

# 使用文件描述符
echo "Using file descriptors..."
exec 3< /dev/null # Open /dev/null as file descriptor 3
echo "Reading from file descriptor 3 (should be nothing):"
read -u 3 -r line
echo "$line"

# 使用子进程
echo "Running a command in a subshell..."
(
    echo "Inside a subshell"
) &

# 使用信号
echo "Handling signals..."
trap 'echo "Caught SIGINT signal"' INT

# 结束脚本
echo "Script execution completed."

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值