SHELL脚本一文搞定

#!/bin/bash

# Shell学习点用法演示脚本

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 标题函数
print_title() {
    echo -e "${BLUE}==================== $1 ====================${NC}"
}

# 分隔线
print_separator() {
    echo -e "${YELLOW}------------------------------------------------${NC}"
}

# 主菜单
show_menu() {
    clear
    echo -e "${GREEN}欢迎使用Shell学习脚本${NC}"
    echo ""
    echo "请选择要学习的内容:"
    echo "1. 基本命令"
    echo "2. 文件操作"
    echo "3. 变量"
    echo "4. 条件语句"
    echo "5. 循环"
    echo "6. 函数"
    echo "7. 输入输出重定向"
    echo "8. 管道"
    echo "9. 正则表达式"
    echo "0. 退出"
    echo ""
    read -p "请输入选择 [0-9]: " choice
}

# 基本命令
basic_commands() {
    print_title "基本命令"
    
    echo -e "${YELLOW}1. pwd - 显示当前工作目录${NC}"
    pwd
    print_separator
    
    echo -e "${YELLOW}2. ls - 列出目录内容${NC}"
    ls -l
    print_separator
    
    echo -e "${YELLOW}3. cd - 切换目录${NC}"
    echo "当前目录: $(pwd)"
    cd ..
    echo "切换到上级目录: $(pwd)"
    cd -  # 返回原目录
    echo "返回原目录: $(pwd)"
    print_separator
    
    echo -e "${YELLOW}4. whoami - 显示当前用户${NC}"
    whoami
    print_separator
    
    echo -e "${YELLOW}5. date - 显示当前日期和时间${NC}"
    date
    print_separator
    
    read -p "按Enter继续..."
}

# 文件操作
file_operations() {
    print_title "文件操作"
    
    echo -e "${YELLOW}1. touch - 创建文件${NC}"
    echo "创建test1.txt, test2.txt, test3.txt"
    touch test1.txt test2.txt test3.txt
    ls -l test*.txt
    print_separator
    
    echo -e "${YELLOW}2. mkdir - 创建目录${NC}"
    echo "创建test_dir目录"
    mkdir test_dir
    ls -ld test_dir
    print_separator
    
    echo -e "${YELLOW}3. cp - 复制文件/目录${NC}"
    echo "复制test1.txt到test_dir目录并重命名为copy.txt"
    cp test1.txt test_dir/copy.txt
    ls -l test_dir
    print_separator
    
    echo -e "${YELLOW}4. mv - 移动文件/目录或重命名${NC}"
    echo "将test2.txt移动到test_dir目录"
    mv test2.txt test_dir/
    echo "将test3.txt重命名为renamed.txt"
    mv test3.txt renamed.txt
    ls -l
    ls -l test_dir
    print_separator
    
    echo -e "${YELLOW}5. rm - 删除文件/目录${NC}"
    echo "删除renamed.txt文件"
    rm renamed.txt
    echo "删除test_dir目录及其内容"
    rm -r test_dir
    ls -l
    print_separator
    
    read -p "按Enter继续..."
}

# 变量
variables() {
    print_title "变量"
    
    echo -e "${YELLOW}1. 定义和使用变量${NC}"
    name="John"
    age=30
    echo "我的名字是 $name,年龄是 $age"
    print_separator
    
    echo -e "${YELLOW}2. 环境变量${NC}"
    echo "当前用户: $USER"
    echo "当前工作目录: $PWD"
    echo "PATH环境变量: $PATH"
    print_separator
    
    echo -e "${YELLOW}3. 命令替换${NC}"
    current_date=$(date)
    echo "当前日期和时间: $current_date"
    
    files=$(ls)
    echo "当前目录下的文件和目录:"
    echo "$files"
    print_separator
    
    read -p "按Enter继续..."
}

# 条件语句
conditionals() {
    print_title "条件语句"
    
    echo -e "${YELLOW}1. if语句${NC}"
    num=10
    if [ $num -gt 5 ]; then
        echo "$num 大于 5"
    else
        echo "$num 小于等于 5"
    fi
    print_separator
    
    echo -e "${YELLOW}2. if-elif-else语句${NC}"
    score=85
    if [ $score -ge 90 ]; then
        echo "成绩等级: A"
    elif [ $score -ge 80 ]; then
        echo "成绩等级: B"
    elif [ $score -ge 70 ]; then
        echo "成绩等级: C"
    elif [ $score -ge 60 ]; then
        echo "成绩等级: D"
    else
        echo "成绩等级: F"
    fi
    print_separator
    
    echo -e "${YELLOW}3. 测试文件类型${NC}"
    filename="test.txt"
    touch $filename
    if [ -e $filename ]; then
        echo "$filename 文件存在"
        if [ -f $filename ]; then
            echo "$filename 是普通文件"
        elif [ -d $filename ]; then
            echo "$filename 是目录"
        fi
        if [ -r $filename ]; then
            echo "$filename 可读"
        fi
        if [ -w $filename ]; then
            echo "$filename 可写"
        fi
        if [ -x $filename ]; then
            echo "$filename 可执行"
        fi
    else
        echo "$filename 文件不存在"
    fi
    rm $filename
    print_separator
    
    read -p "按Enter继续..."
}

# 循环
loops() {
    print_title "循环"
    
    echo -e "${YELLOW}1. for循环${NC}"
    echo "打印1到5的数字:"
    for i in 1 2 3 4 5; do
        echo $i
    done
    print_separator
    
    echo -e "${YELLOW}2. C风格for循环${NC}"
    echo "计算1到10的和:"
    sum=0
    for ((i=1; i<=10; i++)); do
        sum=$((sum + i))
    done
    echo "和为: $sum"
    print_separator
    
    echo -e "${YELLOW}3. while循环${NC}"
    echo "打印1到5的数字:"
    i=1
    while [ $i -le 5 ]; do
        echo $i
        i=$((i + 1))
    done
    print_separator
    
    echo -e "${YELLOW}4. until循环${NC}"
    echo "打印1到5的数字:"
    i=1
    until [ $i -gt 5 ]; do
        echo $i
        i=$((i + 1))
    done
    print_separator
    
    echo -e "${YELLOW}5. 循环控制${NC}"
    echo "打印1到10的偶数:"
    for ((i=1; i<=10; i++)); do
        if [ $((i % 2)) -ne 0 ]; then
            continue
        fi
        echo $i
    done
    print_separator
    
    read -p "按Enter继续..."
}

# 函数
functions() {
    print_title "函数"
    
    echo -e "${YELLOW}1. 定义和调用函数${NC}"
    greet() {
        echo "Hello, $1!"
    }
    
    greet "Alice"
    greet "Bob"
    print_separator
    
    echo -e "${YELLOW}2. 函数返回值${NC}"
    add_numbers() {
        return $(( $1 + $2 ))
    }
    
    add_numbers 5 7
    result=$?
    echo "5 + 7 = $result"
    print_separator
    
    echo -e "${YELLOW}3. 函数返回字符串${NC}"
    get_full_name() {
        echo "$1 $2"
    }
    
    full_name=$(get_full_name "John" "Doe")
    echo "全名: $full_name"
    print_separator
    
    read -p "按Enter继续..."
}

# 输入输出重定向
redirection() {
    print_title "输入输出重定向"
    
    echo -e "${YELLOW}1. 输出重定向 >${NC}"
    echo "这是一个测试文件内容" > test.txt
    echo "内容已写入test.txt"
    cat test.txt
    print_separator
    
    echo -e "${YELLOW}2. 追加输出重定向 >>${NC}"
    echo "追加的内容" >> test.txt
    echo "内容已追加到test.txt"
    cat test.txt
    print_separator
    
    echo -e "${YELLOW}3. 输入重定向 <${NC}"
    echo "统计test.txt中的单词数:"
    wc -w < test.txt
    print_separator
    
    echo -e "${YELLOW}4. 错误重定向 2>${NC}"
    echo "尝试访问不存在的文件,错误信息将被重定向到error.log"
    ls non_existent_file 2> error.log
    echo "错误日志内容:"
    cat error.log
    rm error.log
    print_separator
    
    echo -e "${YELLOW}5. 标准输出和错误输出合并 &>${NC}"
    echo "将标准输出和错误输出合并到combined.log"
    ls test.txt non_existent_file &> combined.log
    echo "合并日志内容:"
    cat combined.log
    rm combined.log test.txt
    print_separator
    
    read -p "按Enter继续..."
}

# 管道
pipes() {
    print_title "管道"
    
    echo -e "${YELLOW}1. 基本管道示例${NC}"
    echo "列出当前目录下的文件,并按文件名排序:"
    ls -l | sort -k9
    print_separator
    
    echo -e "${YELLOW}2. 统计文件数量${NC}"
    echo "当前目录下的文件和目录数量:"
    ls | wc -l
    print_separator
    
    echo -e "${YELLOW}3. 查找包含特定文本的行${NC}"
    echo "创建一个示例文件"
    cat > test.txt << EOF
Hello world
This is a test
Hello shell
Shell scripting is fun
EOF
    echo "查找包含'Hello'的行:"
    cat test.txt | grep "Hello"
    print_separator
    
    echo -e "${YELLOW}4. 多管道组合${NC}"
    echo "查找包含'Shell'的行,并统计行数:"
    cat test.txt | grep "Shell" | wc -l
    rm test.txt
    print_separator
    
    read -p "按Enter继续..."
}

# 正则表达式
regular_expressions() {
    print_title "正则表达式"
    
    echo -e "${YELLOW}1. grep基本用法${NC}"
    echo "创建示例文件"
    cat > test.txt << EOF
apple
banana
cherry
date
elderberry
EOF
    echo "文件内容:"
    cat test.txt
    print_separator
    
    echo -e "${YELLOW}2. 匹配以'a'开头的行${NC}"
    grep '^a' test.txt
    print_separator
    
    echo -e "${YELLOW}3. 匹配以'y'结尾的行${NC}"
    grep 'y$' test.txt
    print_separator
    
    echo -e "${YELLOW}4. 匹配包含'na'的行${NC}"
    grep 'na' test.txt
    print_separator
    
    echo -e "${YELLOW}5. 匹配包含两个连续辅音字母的行${NC}"
    grep '[^aeiou][^aeiou]' test.txt
    print_separator
    
    echo -e "${YELLOW}6. 使用扩展正则表达式${NC}"
    echo "匹配包含'ba'或'ch'的行:"
    grep -E 'ba|ch' test.txt
    rm test.txt
    print_separator
    
    read -p "按Enter继续..."
}

# 主程序
while true; do
    show_menu
    case $choice in
        1) basic_commands ;;
        2) file_operations ;;
        3) variables ;;
        4) conditionals ;;
        5) loops ;;
        6) functions ;;
        7) redirection ;;
        8) pipes ;;
        9) regular_expressions ;;
        0) 
            echo -e "${GREEN}感谢使用Shell学习脚本,再见!${NC}"
            exit 0 
            ;;
        *) 
            echo -e "${RED}无效选择,请输入0-9之间的数字${NC}"
            sleep 1
            ;;
    esac
done    

你可能感兴趣的:(服务器,linux,运维)