本文介绍.bash_profile文件的编写规则,给出.bashrc, .tmux.conf, .vimrc中常用的配置,还有一个有趣的配置,可以实现登陆后在命令行输出你喜欢的符号。

shell语句和.bash_profile文件

这个文件用来设置用户环境变量,语法遵循shell

  1. 定义变量时变量名和等号之间不能有空格,如your_name="kly"
  2. 使用一个定义过的变量,只要在变量名前面加美元符号即可。如打印变量名:echo $your_name或者echo ${your_name}
  3. 原本应该换行的代码写到一行时,需要用分号加空格;进行分隔
  4. 使用alias命令给某个路径下的可执行程序起别名:alias [别名]=[指令名称]
  5. 使用export命令设置环境变量
    • export PATH:定义环境变量PATH
    • export CUDA_HOME=/ssd1/shared/local/cuda-10.1:定义并设置环境变量CUDA_HOME
    • export PATH=/ssd1/shared/local/anaconda3/bin:$PATH:为环境变量PATH增加一条记录(linux环境变量是用冒号隔开的)
  6. wait 命令用于等待后台进程完成。具体来说,wait 会暂停脚本的执行,直到所有后台任务(或指定的任务)完成为止。它通常用于同步并发进程,以确保在继续执行脚本的后续部分之前,所有并行任务都已完成。
    command1 &  # 结尾写 & 表示后台运行
    command2 &
    wait  # 等待 command1 和 command2 完成
  7. 循环语句示例,批量修改路径下week01等文件夹名为chapter01
    for ff in week??
    do
    echo ${ff}  # 打印原文件名 week01等
    echo ${ff#week} # 打印去掉week的名字  01等, 掐头用#  去尾用%
    mv $ff chapter${ff#week}
    done
  8. 条件语句示例
    if [ 条件 ]
    then
     # 当条件为真时执行的命令
    else
     # 当条件为假时执行的命令(可选)
    fi
  9. 脚本里写并发运行,参考示例
    infn="xxxx.txt"
    jobnum=100
    num_lines=`cat $infn |wc -l`
    nl=$((num_lines / jobnum + 1))
    rm -r ${infn}_gpttmp/
    mkdir ${infn}_gpttmp/
    # 将文件分割成较小的部分,`-l`选项指定按行数来分割文件,下来是输入文件、输出文件的前缀,`-d` 选项告诉 split 使用数字后缀,而不是默认的字母后缀。`-a 2`:指定后缀的长度为 2 位。
    split -l $nl $infn ${infn}_gpttmp/_ -d -a 2
    # seq 是一个用于生成序列的命令。-w 选项表示用前导零填充生成的数字
    # -e 是一个文件测试选项,检查指定的文件或目录是否存在
    for i in `seq -w 0 $((jobnum-1))` ; do (
     if [ -e ${infn}_gpttmp/_${i} ] ; then
         # sleep $((i*5))
         sleep 5
         python chatgpt_client_unify_debug_gpt4o.py ${infn}_gpttmp/_${i} $outfn $version $id_key 
     fi
    ) & 
    done
    wait

更详细的shell语法规则见:https://www.runoob.com/linux/linux-shell.html

Linux命令大全:https://www.runoob.com/linux/linux-command-manual.html

修改完.bash_profile文件后需要运行命令source ~/.bash_profile来使文件生效

常用的配置文件

.bashrc .tmux.conf .vimrc
不显示中文,在.bashrc编辑

# 先用locale命令看一下现在采用的编码
# 用locale -a看一下支持的编码,然后改.bashrc
export LANGUAGE="zh_CN:zh"
export LANG="zh_CN.UTF-8"
export LC_ALL="zh_CN.UTF-8"

.vimrc

set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8
set number    "显示行号"
set autoread
set autowrite
set nocp
set ruler
set autoindent
set cindent
set tabstop=4   "tab缩进4位"

syn on
color desert

vim ~/.tmux.conf

set-option -g prefix2 `

unbind %
bind | split-window -h -c "#{pane_current_path}"

unbind '"'
bind - split-window -v -c "#{pane_current_path}"

unbind r
bind r source-file ~/.tmux.conf

bind -r j resize-pane -D 5
bind -r k resize-pane -U 5
bind -r l resize-pane -R 5
bind -r h resize-pane -L 5
bind -r m resize-pane -Z


set -g mouse on
set -g mode-keys vi
set -sg escape-time 10 # make delay shorter

### Copy Mode
bind-key -T copy-mode-vi 'v' send -X begin-selection # start selecting text with "v"
bind-key -T copy-mode-vi 'y' send -X copy-selection # copy text with "y"


### Plugins 
set -g @plugin 'tmux-plugins/tpm'

set -g @plugin 'christoomey/vim-tmux-navigator'
set -g @plugin 'jimeh/tmux-themepack'
set -g @plugin 'tmux-plugins/tmux-resurrect' # persist tmux sessions after computer restart
set -g @plugin 'tmux-plugins/tmux-continuum' # automatically saves sessions for you every 15 minutes

set -g @resurrect-capture-pane-contents 'on'
set -g @continuum-restore 'on'
run '~/.tmux/plugins/tpm/tpm' # Initialize TPM

激活:tmux source ~/.tmux.conf
如果开启鼠标模式后无法复制,解决方法:按住Fn,然后选择自己需要的文本。选中后使用command+C/V

好玩的东西

在ssh登录后,默认打印上一次登录的日期时间,其实可以通过设置打印自定义的内容如下图

terminal
vim /etc/ssh/sshd_config
# 加入下面两句话
PrintMotd yes
PrintLastLog no
# 编辑完后重启ssh
sudo service ssh restart
# 或者 sudo service restart sshd

# 在motd文件中写上想输出的内容即可
vim /etc/motd

 __  ___ .______   .______
|  |/  / |   _  \  |   _  \
|  '  /  |  |_)  | |  |_)  |
|    <   |   ___/  |   ___/
|  .  \  |  |      |  |
|__|\__\ | _|      | _|

标签: linux

添加新评论