Bootstrap

Linux命令汇总(持续更新中······)

1、历史命令(history)

# 查看执行过的历史命令

history

# 执行 history 命令后,通常只会显示已执行命令的序号和命令本身,如果你想要查看命令历史的时间戳,那么可以执行(仅在当前窗口生效,如果想要永久生效需要在/etc/profile文件中配置环境变量,并刷新环境变量)

export HISTTIMEFORMAT='%F %T '
#执行history命令既可以实现带时间戳查看历史命令
history

# 执行 history 命令忽略 pwd、ls、ls -ltr 等命令(仅在当前窗口生效,如果想要永久生效需要在/etc/profile文件中配置环境变量,并刷新环境变量)

export HISTIGNORE="pwd:ls:ls -ltr:"
#执行 history 命令将忽略 pwd、ls、ls -ltr 
history

 # 查看某个用户(eg:root)执行过的历史命令

cat /root/.bash_history

# 在 history 后面直接跟数字,表示查看最近n条历史命令

#语法(history n)
#eg:查看最近10条历史命令
history 10

# 删除第 n 条历史命令

#语法(history -d n)
#eg:删除序号为10的历史命令
history -d 10

# 执行历史命令中的特定指令

#语法(!n)执行对应序号n的命令,eg:执行序号为10的历史命令
!10
#语法(!-n)执行历史命令的倒数第n条命令,eg:执行倒数第3条的历史命令
!-3

# 清空 history 历史命令(仅当前shell窗口生效)

history -c  #清空当前窗口历史命令

2、shell脚本编写

2.1.shell脚本格式

#!/bin/bash
    ······
    #for循环语句
    for
        do
        执行内容
    done
    #if语句
    if 条件
        then
        指令
    else
        指令
    fi

2.2.获取当前目录脚本

# 编写获取当前目录脚本:getDir.sh,并赋予脚本可执行权限

sudo vim getDir.sh
#脚本内容编写完成后赋可执行权限
sudo chmod +x getDir.sh
ls
#执行脚本并验证
sudo sh getDir.sh
sudo pwd

脚本内容如下所示:

#!/bin/bash

work_dir=$(cd $(dirname $0); pwd)
echo '当前工作目录:' $work_dir

2.3.文件中插入内容脚本

# 编写在当前目录下dockerConf文件夹下生成deamon.json文件的脚本:insertContent.sh,并赋予脚本可执行权限

sudo vim insertContent.sh
#脚本内容编写完成后赋可执行权限
sudo chmod +x insertContent.sh
ls
#执行脚本并验证
sudo sh insertContent.sh
ls dockerConf

脚本内容如下所示:

#!/bin/bash

mkdir dockerConf
tee ./dockerConf/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://rsk59qvc.mirror.aliyuncs.com/"],
  
  "insecure-registries": 
    ["http://harbor.example.com",
     "www.ss.skcom"
    ],

  "data-root": "/data/app/"
}
EOF

;