本篇是我最开始学习 Bash特殊字符1. 通配符:
2. 集合运算符 用一些单个字、一个连续范围或断续的字符集合作为通配符
3. 花括号展开式(可以嵌套):
4. 其它特殊字符:
Bash变量1. 自定义变量 用户自定义的变量由字母、数字和下划线组成, 并且变量名的第一个字符不能为数字, 且变量名大小写敏感。
shell语言是非类型的解释型语言, 给一个变量赋值实际上就是定义了变量, 而且可以赋不同类型的值。引用变量有两种方式, {varname}, 为防止变量在字符串中产生歧义建议使用第二种方式, 引用未定义的变量其值为空。 ct@ehbio:~$ a='EHBIO'ct@ehbio:~$ echo ${a}EHBIOct@ehbio:~$ echo $aEHBIO#出错了ct@ehbio:~$ echo $agood#引用变量时大括号的作用ct@ehbio:~$ echo ${a}goodEHBIOgoodct@ehbio:~$ echo $a.goodEHBIO.good#出错了ct@ehbio:~$ echo $a_good#引用变量时大括号的作用ct@ehbio:~$ echo ${a}_goodEHBIO_good 为了使变量可以在其它进程中使用, 需要将变量导出: 2. 环境变量 可以用 环境变量不限于我们之前讲过的可执行程序的环境变量、动态库、Python模块的环境变量,任何变量都可以的。 3. 位置变量 位置变量对应于命令行参数, 其中 4. 其它变量
ct@ehbio:~$ cat ehbio_testParam.sh#!/bin/bashecho 'EHBIO${IFS}great'echo '$*'echo -ne '\t';echo $*echo '$@'echo -ne '\t';echo $@echo 'Each element in $*:'for i in '$*'; do echo -ne '\t'; echo $i;doneecho 'Each element in $@:'for i in '$@'; do echo -ne '\t'; echo $i;donect@ehbio:~$ bash ehbio_testParam.sh sheng xin bao dianEHBIO great$* sheng xin bao dian$@ sheng xin bao dianEach element in $*: sheng xin bao dianEach element in $@: sheng xin bao dian Bash操作符1. 字符串操作符(替换操作符)
ct@ehbio:~$ echo ${var:?message} 2. 模式匹配操作符
个人最常用的是最后一个,常用于 ct@ehbio:~$ var='sheng xin bao dian good'ct@ehbio:~$ ${var/good/great}-bash: sheng: command not foundct@ehbio:~$ echo ${var/good/great}sheng xin bao dian great 比如获取fastq文件的名字部分 for i in `ls *_1.fq.gz`; do j=${i/_1.fq.gz/}; echo '$j'; done Shell中条件和test命令Bash可以使用 1. 文件测试操作符
举两个例子 ct@ehbio:~$ touch olderct@ehbio:~$ touch newerct@ehbio:~$ if test -e older; then echo 'older esists'; fiolder esistsct@ehbio:~$ if test -s older; then echo 'older is unempty'; fict@ehbio:~$ if [ -s older ]; then echo 'older is unempty'; fict@ehbio:~$ if [ ! -s older ]; then echo 'older is empty'; fiolder is emptyct@ehbio:~$ if [ newer -nt older ]; then echo 'newer'; finewer 2. 字符串操作符
# 字符串的大小比较的是最先遇到的不同的ASCII码的大小ct@ehbio:~$ if test '10'>'20'; then echo '10>20'; fi10>20ct@ehbio:~$ if test 10>20; then echo '10 < 20';=""> 3. 整数操作符 var1 –eq var2 var1等于var2
需要注意的是常用的数学运算符给了字符串比较,数字比较使用的却是英文缩写 数学表达式也可以 if (( 3>2 )); then echo 'TRUE'; fiTRUE 4. 逻辑操作符
Shell流控制1. 条件语句: if
if conditions; then do sth when conditions are trueelif another_conditions; then do sth when another_conditions are trueelse: do sth when above condiitons are all falsefi if test $guanzhu_sxbd == 'already'; then echo 'Enjoy it'elif test $guanzhu_hjyz == 'already'; then echo 'Enjoy it'else echo 'Guan zhu them'fiEnjoy it 2. 确定性循环: 遍历一个列表,取出每个元素,针对性操作。 for i in `ls *_1.fq.gz`; do echo '$i'; done 3. 不确定性循环: declare -i a #定义整数变量a=1 # 初始化变量while test $a -lt 3; do echo $a a=$a+1done echo $a 4. 选择结构: ct@ehbio:~$ cat select_case.shPS3='Input the position of selected parameter (1 for exit):'select opts in a b c ddo case $opts in a) exit 0; ;; b) echo ' Parameters $opts' ;; c) echo ' Parameters $opts' ;; d) echo ' Parameters $opts' ;; ?) echo 'Unknown' ;; esacdonect@ehbio:~$ bash select_case.sh 1) a2) b3) c4) dInput the position of selected parameter (1 for exit):2 Parameters bInput the position of selected parameter (1 for exit):3 Parameters cInput the position of selected parameter (1 for exit):4 Parameters dInput the position of selected parameter (1 for exit):1 5. 命令 将存放在位置变量中的命令行参数依次向左传递 ct@ehbio:~$ cat ehbio_testParam.sh#!/bin/bashecho 'Each element in $*:'for i in '$*'; do echo -ne '\t'; echo $i;doneecho $1shiftfor i in '$*'; do echo -ne '\t'; echo $i;donect@ehbio:~$ bash ehbio_testParam.sh sheng xin bao dianEach element in $*: sheng xin bao diansheng xin bao dian Shell函数
ct@ehbio:~$ cat test_func.sh function show_ehbio () { echo $@ echo $1}show_ehbio 'EHBIO great' 'SXBD great'ct@ehbio:~$ bash test_func.shEHBIO great SXBD greatEHBIO great 输入输出1. I/O重定向 管道、标准输入输出之前有过详细介绍。
command < label="" input…="" label="" 说明:="" 使一个命令的输入为一段shell脚本(input…),=""> ftp: USER=anonymous PASS=YC@163.com #-i: 非交互模式 -n: 关闭自动登录 ftp –i –n < end="" open="" ftp.163.com="" user="" $user="" $pass="" cd="" ub="" close="">
2. 字符串I/O操作 字符串输出: 命令选项: 3. 字符串输入: 命令选项: ct@ehbio:~$ read -p 'Enter the best tutorial: ' tutorialEnter the best tutorial: Sheng Xin Bao Dianct@ehbio:~$ echo $tutorialSheng Xin Bao Dian# 隐藏输入内容ct@ehbio:~$ read -s -p 'Enter your password: ' passwordEnter your password: ct@ehbio:~$ echo $passwordhaha 命令行处理 命令行处理命令
选项列表字符串以冒号开头的选项字母排列组成, 如果一选项需要一个参数则该选项字母后跟一个冒号
如果某选项有参数, 则读取参数到内置变量 在我们推出的一步绘图脚本里面,就是使用 while getopts 'hf:m:a:A:b:I:t:x:l:j:J:d:F:G:H:P:L:y:V:D:c:C:B:X:Y:R:w:u:r:o:O:s:S:p:z:Z:v:e:E:i:' OPTIONdo case $OPTION in h) usage exit 1 ;; f) file=$OPTARG ;; m) melted=$OPTARG ;; . . . ?) usage exit 1 ;; esacdone 进程和作业控制命令行运行监测和软件安装文中讲述了部分监测命令。 如果一个命令需要运行比较久,一般使用
Ref: 1.http://bbs./forum.php?mod=viewthread&tid=575412 2. 未整理的bash tips http://blog./2011/05/bash-tips-record/ 内 容比较杂,都是平时问题的记录。 |
|