Linux系统环境

Linux系统一般有4个主要部分:内核shell文件系统应用程序
我们用的命令是shell语言
ps -ef| grep May21 查看自己提交的任务
jobs查找当前登陆提交的任务

变量

变量: 环境变量、状态变量、位置参数变量、自定义变量,
调用变量时,要在变量前面加一个 $ 符号
环境变量: 用于存储有关shell会话和工作环境的系统变量
状态变量: 用于记录命令的运行结果
位置参数变量: 用于用于向命令或程序脚本中传递信息
自定义变量: 由用户自行定义的变量,可用于用户编写的
脚本,多个命令间的值传递等
截屏2022-05-14 21.10.16.png
截屏2022-05-14 21.12.29.png
截屏2022-05-14 21.08.34.png

通配符与参数扩展

截屏2022-05-14 21.35.07.png
截屏2022-05-14 21.35.21.png
截屏2022-05-14 21.35.37.png
参数扩展练习

  1. $ id=abcdcdcba1
  2. (base) May21 21:26:08 ~
  3. $ echo $id
  4. abcdcdcba1
  5. (base) May21 21:26:23 ~
  6. $ echo ${id#*cd}
  7. cdcba1
  8. (base) May21 21:27:59 ~
  9. $ echo ${id##*cd}
  10. cba1
  11. (base) May21 21:28:22 ~
  12. $ echo ${id%*cd}
  13. abcdcdcba1
  14. (base) May21 21:28:35 ~
  15. $ echo ${id%cd*}
  16. abcd
  17. (base) May21 21:28:57 ~
  18. $ echo ${id%%cd*}
  19. ab
  20. (base) May21 21:29:08 ~
  21. $ echo ${id/cd/x}
  22. abxcdcba1
  23. (base) May21 21:29:45 ~
  24. $ echo ${id//cd/x}
  25. abxxcba1
  26. (base) May21 21:29:54 ~
  27. $ echo ${#id}
  28. 10
  29. (base) May21 21:30:35 ~
  30. $ echo ${#id:2}
  31. -bash: ${#id:2}: bad substitution # 报错啦,多写了#
  32. (base) May21 21:30:43 ~
  33. $ echo ${id:2}
  34. cdcdcba1
  35. (base) May21 21:30:58 ~
  36. $ echo ${id:2:4}
  37. cdcd
  38. (base) May21 21:31:34 ~
  39. $ echo ${id:2:-2}
  40. cdcdcb

结构化语句

条件语句 if

有头有尾,一个if 就要对应一个 fi 。有三种结构:

  1. # 1
  2. if [ condition ] #条件放在中括号里,内容与括号需要有空格
  3. then
  4. commands
  5. fi #结束语,固定搭配
  1. # 2
  2. if [ condition ]
  3. then
  4. commands
  5. else
  6. commands
  7. fi
  1. # 3
  2. if [ condition ]
  3. then
  4. commands
  5. else
  6. if [ condition ]
  7. then
  8. commands
  9. fi #一个if需要对应一个fi
  10. fi
  11. # (else if 可以缩写为 elif )
  12. if [ condition ]
  13. then
  14. commands
  15. elif [ condition ]
  16. then
  17. commands
  18. fi
  19. fi

if 条件语句的常见条件: 数值判断、字符串判断、文件判断

循环语句

for 循环

  1. for i in 1 2 3 4 5
  2. do
  3. echo ${i} "Welcome to Biotrainee() !"
  4. done
  5. for i in {1..10}
  6. do
  7. touch file${i}
  8. done
  9. list="CDS exon gene start_codon stop_codon transcript UTR"
  10. for i in ${list}
  11. do
  12. echo "This feature is ${i}"
  13. done

while 循环

结构化语句练习

  1. (base) May21 21:31:47 ~
  2. $ id=example
  3. (base) May21 22:25:24 ~
  4. $ fastqc ~/Data/${id}.fq
  5. Started analysis of example.fq
  6. Approx 100% complete for example.fq
  7. Analysis complete for example.fq
  8. (base) May21 22:26:09 ~
  9. $ if [ $? -eq 0 ]
  10. > then
  11. > echo 'yes'
  12. > else
  13. > echo 'no'
  14. > fi
  15. yes
  16. (base) May21 22:27:38 ~
  17. $ touch file{1..10}
  18. (base) May21 22:28:02 ~
  19. $ ls
  20. bin fa1.txt file2 file5 file8 Miniconda3-latest-Linux-x86_64.sh
  21. biosoft file1 file3 file6 file9 R4.yaml
  22. Data file10 file4 file7 miniconda3 readme.txt
  23. (base) May21 22:28:10 ~
  24. $ ls file* | while read id
  25. > do
  26. > echo mv ${id} ${id}.txt
  27. > done
  28. mv file1 file1.txt
  29. mv file10 file10.txt
  30. mv file2 file2.txt
  31. mv file3 file3.txt
  32. mv file4 file4.txt
  33. mv file5 file5.txt
  34. mv file6 file6.txt
  35. mv file7 file7.txt
  36. mv file8 file8.txt
  37. mv file9 file9.txt
  38. (base) May21 22:31:24 ~
  39. $ ls file* | while read id; do mv ${id} ${id}.txt; done
  40. (base) May21 22:31:43 ~
  41. $ ls
  42. bin fa1.txt file2.txt file5.txt file8.txt Miniconda3-latest-Linux-x86_64.sh
  43. biosoft file10.txt file3.txt file6.txt file9.txt R4.yaml
  44. Data file1.txt file4.txt file7.txt miniconda3 readme.txt

shell脚本编程

Vim编辑器

Vim 编辑器:是从 vi 发展出来的一个文本编辑器。代码补全、编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用。
截屏2022-05-14 22.45.41.png
shell 脚本:为了和普通文本文件区分开,通常shell脚本都 会以 .sh 为后缀名
R 语言脚本:.R 为后缀名
Python 脚本:.py 为后缀名

脚本首行的 #! 是Linux的 Shebang 符号,指定解释
1: 标准输出流 2:标准误输出流