• linux脚本笔记


    目录

    1.增加环境变量

    2.自定义命令快捷键

    3.关闭selinux和防火墙

    4.增加别名快捷键

    5.Linux链接


    1.增加环境变量

    新建add_env.sh

    1. #!/bin/bash
    2. app_dir="/root/docker"
    3. # 检查配置文件中是否已存在相同的环境变量
    4. if grep -q -E "^export APP_HOME=.*" ~/.bashrc || grep -q -E "^export APP_HOME=.*" ~/.bash_profile; then
    5. echo "环境变量已存在,无需添加。"
    6. else
    7. # 添加环境变量到配置文件
    8. echo "export APP_HOME=${app_dir}" >> ~/.bashrc
    9. echo "环境变量已添加到配置文件。"
    10. # 使环境变量立即生效
    11. source ~/.bashrc
    12. echo "环境变量已生效。"
    13. fi

    赋予执行权限,然后关闭当前窗口,重新开新窗口生效

    chmod +x add_env.sh

    验证方式export或者echo $APP_HOME

    2.自定义命令快捷键

    新建dctl,不用带文件后缀

    1. #!/usr/bin/env bash
    2. # 显示帮助提示
    3. show_help() {
    4. echo "Usage: $0 [options]"
    5. echo "Commands:"
    6. echo " start Start containers"
    7. echo " stop Stop containers"
    8. echo " restart Restart containers"
    9. echo " build Build containers"
    10. echo " ps Show container status"
    11. echo "Options:"
    12. echo " [container1 container2 ...] Specify container names"
    13. }
    14. # 处理参数函数
    15. handle_param(){
    16. case $1 in
    17. start)
    18. # 处理启动逻辑
    19. echo "Starting..."
    20. docker-compose -f ${APP_HOME}/docker-compose.yml start "${@:2}"
    21. ;;
    22. stop)
    23. # 处理停止逻辑
    24. echo "Stopping..."
    25. docker-compose -f ${APP_HOME}/docker-compose.yml stop "${@:2}"
    26. ;;
    27. restart)
    28. # 处理重启逻辑
    29. echo "Restarting..."
    30. docker-compose -f ${APP_HOME}/docker-compose.yml restart "${@:2}"
    31. ;;
    32. build)
    33. # 处理构建逻辑
    34. echo "Building..."
    35. docker-compose -f ${APP_HOME}/docker-compose.yml build "${@:2}"
    36. ;;
    37. ps)
    38. # 处理构建逻辑
    39. echo "Status... ${APP_HOME}"
    40. docker-compose -f ${APP_HOME}/docker-compose.yml ps
    41. ;;
    42. *)
    43. # 未知参数
    44. show_help
    45. ;;
    46. esac
    47. }
    48. #docker-compose -f ${APP_HOME}/docker-compose.yml $@
    49. # 检查参数个数并处理每个参数
    50. if [[ $# -eq 0 ]]; then
    51. show_help
    52. else
    53. handle_param "$@"
    54. fi

    赋予执行权限,然后给出硬链接或者软连

    1. chmod +x dctl
    2. ln ~/dctl /usr/local/bin

    验证方式

    1. [root@localhost docker]# dctl 0
    2. Usage: /usr/local/bin/dctl <command> [options]
    3. Commands:
    4. start Start containers
    5. stop Stop containers
    6. restart Restart containers
    7. build Build containers
    8. ps Show container status
    9. Options:
    10. [container1 container2 ...] Specify container names

    3.关闭selinux和防火墙

    新建disable_selinux_firewall.sh

    1. #!/usr/bin/env bash
    2. # 检测发行版
    3. if [ -f "/etc/redhat-release" ]; then
    4. # CentOS 或 RHEL
    5. echo "Detected CentOS or RHEL."
    6. # 关闭SELinux
    7. echo "Disabling SELinux..."
    8. sudo setenforce 0
    9. sudo sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
    10. echo "SELinux has been disabled."
    11. # 关闭防火墙
    12. echo "Stopping firewall..."
    13. sudo systemctl stop firewalld
    14. sudo systemctl disable firewalld
    15. echo "Firewall has been stopped and disabled."
    16. elif [ -f "/etc/debian_version" ]; then
    17. # Debian 或 Ubuntu
    18. echo "Detected Debian or Ubuntu."
    19. # 关闭防火墙
    20. echo "Stopping firewall..."
    21. sudo ufw disable
    22. echo "Firewall has been stopped and disabled."
    23. else
    24. echo "Unsupported distribution. Cannot disable SELinux or firewall."
    25. exit 1
    26. fi

    4.增加别名快捷键

    新建add_alias.sh

    1. #!/usr/bin/env bash
    2. if ! grep -q "alias fs_cli='docker exec -it switch fs_cli'" ~/.bashrc; then
    3. echo "alias fs_cli='docker exec -it switch fs_cli'" >> ~/.bashrc
    4. fi
    5. if ! grep -q "alias fs='docker exec -it switch bash'" ~/.bashrc; then
    6. echo "alias fs='docker exec -it switch bash'" >> ~/.bashrc
    7. fi
    8. if ! grep -q "alias exec=" ~/.bashrc; then
    9. echo "function docker_exec() {
    10. docker exec -it \$1 bash
    11. }" >> ~/.bashrc
    12. echo "alias exec='docker_exec'" >> ~/.bashrc
    13. fi
    14. source ~/.bashrc

    5.Linux链接

    Linux链接分两种:

    一种被称为硬链接(Hard Link)
    一种被称为符号链接(Symbolic Link)

    默认情况下,ln 命令产生硬链接、ln -s 命令产生软链接

    原文件f1,硬链接文件f2,符号链接文件f3

    区别如下:

    1. 删除符号连接 f3,对 f1,f2 无影响;
    2. 删除硬连接 f2,对 f1,f3 也无影响;
    3. 删除原文件 f1,对硬连接 f2 没有影响,导致符号连接 f3 失效;
    4. 同时删除原文件 f1,硬连接 f2,整个文件会真正的被删除。

  • 相关阅读:
    农业大数据概论-按章节复习
    DataFrame窗口函数操作
    常見算法時間複雜度分析
    vue+springboot,easyexcel的excel文件下载
    S-Clustr(影子集群)僵尸网络@Мартин.
    【预测模型-ELM预测】基于麻雀算法优化极限学习机预测附matlab代码
    .net 动态代理实现
    linux内核中list的实现
    2022年7月31日--使用C#迈出第一步--使用 C# 创建具有约定、空格和注释的易读代码
    倒数三天 | WAIC 滴水湖 AI 开发者创新论坛:当数据库遇上 AI 来啦!
  • 原文地址:https://blog.csdn.net/weixin_42211693/article/details/133310811