shell运行程序的过程

1、指令查找顺序

  1. 指定路径查找
    • 如使用绝对路径或相对路径,如果路径下能找到对应程序,则执行
  2. alias查找
    • 如果没找到执行命令,则继续往下找
  3. shell内置命令
    • 不同的shell包含不同的内置命令,通常不需要shell到磁盘中去搜索
    • help可以看内置命令
  1. $ type echo
  2. echo is a shell builtin
  1. PATH中查找
    • 在PATH环境变量中查找,按路径找到的第一个程序会被执行
    • 可以用whereis来确定位置
  1. $ whereis ls
  2. ls: /bin/ls /usr/share/man/man1/ls.1.g

2、如何让自己的程序像内置命令一样被识别(如./hello能直接用hello调用)

  • 把自己的程序放到PATH的路径中,如/bin
  1. $ hello
  2. hello world
  3. $ whereis hello
  4. hello: /bin/hello
  • 将当前路径加入到PATH
  1. $ PATH=$PATH:./ #这种方式只在当前shell有效,所有shell生效可修改/etc/profile文件
  2. $ hello
  3. hello world
  • 设置别名
  1. $ alias hello="/temp/hello"
  2. $ hello
  3. hello world
  • (补充)删除别名
  1. unalias printf

自己写的简单脚本

1、将指定文件复制到指定目录下

  1. #!/bin/bash
  2. GIT_PATH="/home/lpc/gitee-graduate-project/graduate-project/clang-tidy-code"
  3. CLANG_TOOLS_EXTRA_PROJECT="/home/lpc/llvm/llvm-project/clang-tools-extra"
  4. CLANG_TIDY_CHECK_MISC="${CLANG_TOOLS_EXTRA_PROJECT}/clang-tidy/misc"
  5. CLANG_TIDY_DOCS="${CLANG_TOOLS_EXTRA_PROJECT}/docs/clang-tidy/checks"
  6. CLANG_TIDY_TEST="${CLANG_TOOLS_EXTRA_PROJECT}/test/clang-tidy/checkers"
  7. CHECKERS=(LpcTestOneCheck)
  8. DOCS_TESTS=(misc-lpc-test-one)
  9. for cppName in ${CHECKERS}; do
  10. cp ${CLANG_TIDY_CHECK_MISC}/${cppName}.cpp ${GIT_PATH}/check
  11. cp ${CLANG_TIDY_CHECK_MISC}/${cppName}.h ${GIT_PATH}/check
  12. done
  13. for name in ${DOCS_TESTS}; do
  14. cp ${CLANG_TIDY_DOCS}/${name}.rst ${GIT_PATH}/doc
  15. cp ${CLANG_TIDY_TEST}/${name}.cpp ${GIT_PATH}/'test'
  16. done

2、git提交

  • 如果没有指定参数,则将时间作为默认commit信息
  1. #!/bin/bash
  2. git add .
  3. echo $#
  4. time=$(date "+%Y-%m-%d %H:%M:%S")
  5. if [ $# == 0 ] ;then
  6. echo ${time}
  7. echo "no commit message, use time ${time} instead"
  8. git commit -m "${time}"
  9. else
  10. echo "commit message is $1"
  11. git commit -m "$1"
  12. fi
  13. git push

后台不挂断的运行

nohup:ssh断了也不会挂断执行,但会在终端输出

&:在后台运行

  1. # 输出结果默认重写到当前目录下的nohup.out
  2. nohup commands &
  3. nohup python test.py > log.txt &

查看当前终端生效的后台进程

  1. jobs -l

建立软链接到自己的可执行文件

  1. sudo ln -s ~/tools/clash/clash-linux-amd64 /usr/bin/clash
  2. sudo ln -s ~/clion/bin/clion.sh /usr/bin/clion

字符串操作

字符串包含

  1. string='My long string'
  2. if [[ $string == *"My long"* ]]; then
  3. echo "It's there!"
  4. fi
  5. # 或者regex的版本
  6. string='My string';
  7. if [[ $string =~ "My" ]]; then
  8. echo "It's there!"
  9. fi
  10. text=" <tag>bmnmn</tag> "
  11. if [[ "$text" =~ "<tag>" ]]; then
  12. echo "matched"
  13. else
  14. echo "not matched"
  15. fi

文本读取

  • 按行读取直到空
  1. #!/usr/bin/bash
  2. while read line
  3. do
  4. if [[ -z $line ]]
  5. then
  6. exit
  7. fi
  8. wget -x "http://someurl.com/${line}.pdf"
  9. done < inputfile
  10. # 其他不同场景的写法
  11. while read line && [ "$line" != "quit" ]; do # ...
  12. # stop at an empty line:
  13. while read line && [ "$line" != "" ]; do # ...
  14. while read line && [ -n "$line" ]; do # ...
  • 一行行读取指定文本内容
  1. cat file.txt | while read line; do
  2. echo "$line"
  3. done
  4. cat peptides.txt | while read line || [[ -n $line ]];
  5. do
  6. # do something with $line here
  7. done
  8. #!/bin/bash
  9. filename='peptides.txt'
  10. echo Start
  11. while read p; do
  12. echo "$p"
  13. done < "$filename"