bash 字符串处理
欢迎关注微信公众号生信宝典:https://mp.weixin.qq.com/s/lWNp_6W_jLiogmtlk9nO2A Bash特殊字符
用一些单个字、一个连续范围或断续的字符集合作为通配符
Bash变量
用户自定义的变量由字母、数字和下划线组成,并且变量名的第一个字符不能为数字,且变量名大小写敏感。
shell语言是非类型的解释型语言,给一个变量赋值实际上就是定义了变量,而且可以赋不同类型的值。引用变量有两种方式,
ct@ehbio:~$ a="EHBIO"
ct@ehbio:~$ echo ${a}
EHBIO
ct@ehbio:~$ echo $a
EHBIO
#出错了
ct@ehbio:~$ echo $agood
#引用变量时大括号的作用
ct@ehbio:~$ echo ${a}good
EHBIOgood
ct@ehbio:~$ echo $a.good
EHBIO.good
#出错了
ct@ehbio:~$ echo $a_good
#引用变量时大括号的作用
ct@ehbio:~$ echo ${a}_good
EHBIO_good
为了使变量可以在其它进程中使用,需要将变量导出:
可以用 环境变量不限于我们之前讲过的可执行程序的环境变量、动态库、Python模块的环境变量,任何变量都可以的。
位置变量对应于命令行参数,其中
ct@ehbio:~$ cat ehbio_testParam.sh
#!/bin/bash
echo "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;
done
echo 'Each element in $@:'
for i in "$@"; do
echo -ne "t";
echo $i;
done
ct@ehbio:~$ bash ehbio_testParam.sh sheng xin bao dian
EHBIO
great
$*
sheng xin bao dian
$@
sheng xin bao dian
Each element in $*:
sheng xin bao dian
Each element in $@:
sheng
xin
bao
dian
Bash操作符
ct@ehbio:~$ echo ${var:?message}
-bash: var: message
ct@ehbio:~$ var='sheng xin bao dian'
ct@ehbio:~$ echo ${var:6:3}
xin
ct@ehbio:~$ echo ${var:?message}
sheng xin bao dian
ct@ehbio:~$ echo $?
0
ct@ehbio:~$ unset var
ct@ehbio:~$ echo ${var:?message}
-bash: var: message
ct@ehbio:~$ echo $?
1
ct@ehbio:~$ echo ${var:=ehbio}
ehbio
ct@ehbio:~$ echo ${var}
ehbio
个人最常用的是最后一个,常用于 ct@ehbio:~$ var='sheng xin bao dian good'
ct@ehbio:~$ ${var/good/great}
-bash: sheng: command not found
ct@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可以使用
举两个例子 ct@ehbio:~$ touch older
ct@ehbio:~$ touch newer
ct@ehbio:~$ if test -e older; then echo "older esists"; fi
older esists
ct@ehbio:~$ if test -s older; then echo "older is unempty"; fi
ct@ehbio:~$ if [ -s older ]; then echo "older is unempty"; fi
ct@ehbio:~$ if [ ! -s older ]; then echo "older is empty"; fi
older is empty
ct@ehbio:~$ if [ newer -nt older ]; then echo "newer"; fi
newer
# 字符串的大小比较的是最先遇到的不同的ASCII码的大小
ct@ehbio:~$ if test "10">"20"; then echo "10>20"; fi
10>20
ct@ehbio:~$ if test 10>20; then echo "10 < 20"; fi
var1 –eq var2 var1等于var2
需要注意的是常用的数学运算符给了字符串比较,数字比较使用的却是英文缩写 数学表达式也可以 if (( 3>2 )); then echo 'TRUE'; fi
TRUE
Shell流控制
if conditions; then
do sth when conditions are true elif another_conditions; then
do sth when another_conditions are true else: do sth when above condiitons are all false fi
if test $guanzhu_sxbd == "already"; then
echo "Enjoy it"
elif test $guanzhu_hjyz == "already"; then
echo "Enjoy it"
else
echo "Guan zhu them"
fi
Enjoy it
遍历一个列表,取出每个元素,针对性操作。 for i in `ls *_1.fq.gz`; do
echo "$i";
done
declare -i a #定义整数变量
a=1 # 初始化变量
while test $a -lt 3; do
echo $a
a=$a+1
done
echo $a
ct@ehbio:~$ cat select_case.sh
PS3="Input the position of selected parameter (1 for exit):"
select opts in a b c d
do
case $opts in
a)
exit 0;
;;
b)
echo " Parameters $opts"
;;
c)
echo " Parameters $opts"
;;
d)
echo " Parameters $opts"
;;
?)
echo "Unknown"
;;
esac
done
ct@ehbio:~$ bash select_case.sh
1) a
2) b
3) c
4) d
Input the position of selected parameter (1 for exit):2
Parameters b
Input the position of selected parameter (1 for exit):3
Parameters c
Input the position of selected parameter (1 for exit):4
Parameters d
Input the position of selected parameter (1 for exit):1
将存放在位置变量中的命令行参数依次向左传递 ct@ehbio:~$ cat ehbio_testParam.sh
#!/bin/bash
echo 'Each element in $*:'
for i in "$*"; do
echo -ne "t";
echo $i;
done
echo $1
shift
for i in "$*"; do
echo -ne "t";
echo $i;
done
ct@ehbio:~$ bash ehbio_testParam.sh sheng xin bao dian
Each element in $*:
sheng xin bao dian
sheng
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.sh
EHBIO great SXBD great
EHBIO great
输入输出
管道、标准输入输出之前有过详细介绍。
command << label
input…
label
说明: 使一个命令的输入为一段shell脚本(input…),直到标号(label)结束
ftp: USER=anonymous
PASS=YC@163.com
#-i: 非交互模式 -n: 关闭自动登录
ftp –i –n << END
open ftp.163.com
user $USER $PASS
cd /pub
close
END
#END标记输入结束
字符串输出: 命令选项:
命令选项: ct@ehbio:~$ read -p "Enter the best tutorial: " tutorial
Enter the best tutorial: Sheng Xin Bao Dian
ct@ehbio:~$ echo $tutorial
Sheng Xin Bao Dian
# 隐藏输入内容
ct@ehbio:~$ read -s -p "Enter your password: " password
Enter your password:
ct@ehbio:~$ echo $password
haha
命令行处理 命令行处理命令:
选项列表字符串以冒号开头的选项字母排列组成,如果一选项需要一个参数则该选项字母后跟一个冒号
如果某选项有参数,则读取参数到内置变量 在我们推出的一步绘图脚本里面,就是使用 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:" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
f)
file=$OPTARG
;;
m)
melted=$OPTARG
;;
.
.
.
?)
usage
exit 1
;;
esac
done
进程和作业控制命令行运行监测和软件安装文中讲述了部分监测命令。 如果一个命令需要运行比较久,一般使用
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |