linux脚本笔记

目录

1.增加环境变量

2.自定义命令快捷键

3.关闭selinux和防火墙

4.增加别名快捷键

5.Linux链接


1.增加环境变量

新建add_env.sh

#!/bin/bash

app_dir="/root/docker"

# 检查配置文件中是否已存在相同的环境变量
if grep -q -E "^export APP_HOME=.*" ~/.bashrc || grep -q -E "^export APP_HOME=.*" ~/.bash_profile; then
  echo "环境变量已存在,无需添加。"
else
  # 添加环境变量到配置文件
  echo "export APP_HOME=${app_dir}" >> ~/.bashrc
  echo "环境变量已添加到配置文件。"

  # 使环境变量立即生效
  source ~/.bashrc
  echo "环境变量已生效。"
fi

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

chmod +x add_env.sh

验证方式export或者echo $APP_HOME

2.自定义命令快捷键

新建dctl,不用带文件后缀

#!/usr/bin/env bash

# 显示帮助提示
show_help() {
    echo "Usage: $0  [options]"
    echo "Commands:"
    echo "  start    Start containers"
    echo "  stop     Stop containers"
    echo "  restart  Restart containers"
    echo "  build    Build containers"
    echo "  ps       Show container status"
    echo "Options:"
    echo "  [container1 container2 ...]  Specify container names"
}

# 处理参数函数
handle_param(){
   case $1 in
        start)
            # 处理启动逻辑
            echo "Starting..."
            docker-compose -f ${APP_HOME}/docker-compose.yml start "${@:2}"
            ;;
        stop)
            # 处理停止逻辑
            echo "Stopping..."
            docker-compose -f ${APP_HOME}/docker-compose.yml stop "${@:2}"
            ;;
        restart)
            # 处理重启逻辑
            echo "Restarting..."
            docker-compose -f ${APP_HOME}/docker-compose.yml restart "${@:2}"
            ;;
        build)
            # 处理构建逻辑
            echo "Building..."
            docker-compose -f ${APP_HOME}/docker-compose.yml build "${@:2}"
            ;;
        ps)
            # 处理构建逻辑
            echo "Status... ${APP_HOME}"
            docker-compose -f ${APP_HOME}/docker-compose.yml ps
            ;;
        *)
            # 未知参数
            show_help
            ;;
    esac
}

#docker-compose -f ${APP_HOME}/docker-compose.yml $@
# 检查参数个数并处理每个参数
if [[ $# -eq 0 ]]; then
    show_help
else
    handle_param "$@"
fi

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

chmod +x dctl
ln ~/dctl /usr/local/bin

验证方式

[root@localhost docker]# dctl 0
Usage: /usr/local/bin/dctl  [options]
Commands:
  start    Start containers
  stop     Stop containers
  restart  Restart containers
  build    Build containers
  ps       Show container status
Options:
  [container1 container2 ...]  Specify container names

3.关闭selinux和防火墙

新建disable_selinux_firewall.sh

#!/usr/bin/env bash

# 检测发行版
if [ -f "/etc/redhat-release" ]; then
    # CentOS 或 RHEL
    echo "Detected CentOS or RHEL."
    # 关闭SELinux
    echo "Disabling SELinux..."
    sudo setenforce 0
    sudo sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
    echo "SELinux has been disabled."
    # 关闭防火墙
    echo "Stopping firewall..."
    sudo systemctl stop firewalld
    sudo systemctl disable firewalld
    echo "Firewall has been stopped and disabled."
elif [ -f "/etc/debian_version" ]; then
    # Debian 或 Ubuntu
    echo "Detected Debian or Ubuntu."
    # 关闭防火墙
    echo "Stopping firewall..."
    sudo ufw disable
    echo "Firewall has been stopped and disabled."
else
    echo "Unsupported distribution. Cannot disable SELinux or firewall."
    exit 1
fi

4.增加别名快捷键

新建add_alias.sh

#!/usr/bin/env bash
if ! grep -q "alias fs_cli='docker exec -it switch fs_cli'" ~/.bashrc; then
    echo "alias fs_cli='docker exec -it switch fs_cli'" >> ~/.bashrc
fi

if ! grep -q "alias fs='docker exec -it switch bash'" ~/.bashrc; then
    echo "alias fs='docker exec -it switch bash'" >> ~/.bashrc
fi

if ! grep -q "alias exec=" ~/.bashrc; then
echo "function docker_exec() {
      docker exec -it \$1 bash
    }" >> ~/.bashrc
    echo "alias exec='docker_exec'" >> ~/.bashrc
fi

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,整个文件会真正的被删除。

你可能感兴趣的:(linux,笔记,运维)