linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例

一、shell条件语句(if用法)

        if语句结构[if/then/elif/else/fi]

if 条件测试语句

then

action

[elif 条件

action

else

action

]

fi

如果对于:条件测试语句不是很清楚,可以参考:linux shell 逻辑运算符、逻辑表达式详解

shell命令,可以按照分号分割,也可以按照换行符分割。如果想一行写入多个命令,可以通过“';”分割。

如:

[chengmo@centos5 ~]$ a=5;if [[ a -gt 4 ]] ;then echo 'ok';fi;                       
ok

实例:(test.sh)

#!/bin/sh

scores=40;
if [[ $scores -gt 90 ]]; then
    echo "very good!";
elif [[ $scores -gt 80 ]]; then
    echo "good!";
elif [[ $scores -gt 60 ]]; then
    echo "pass!";
else
    echo "no pass!";
fi;

image

条件测试有:[[]],[],test 这几种,注意:[[]] 与变量之间用空格分开。

二、循环语句(for,while,until用法):

  • for循环使用方法(for/do/done)

语法结构:

 1.for … in 语句

for 变量 in seq字符串

do

action

done

说明:seq字符串 只要用空格字符分割,每次for…in 读取时候,就会按顺序将读到值,给前面的变量。

实例(testfor.sh):

#!/bin/sh

for i in $(seq 10); do
    echo $i;
done;

image

seq 10 产生 1 2 3 。。。。10空格分隔字符串。

2.for((赋值;条件;运算语句))

for((赋值;条件;运算语句))

do

action

done;

实例(testfor2.sh):

#!/bin/sh

for((i=1;i<=10;i++));do
    echo $i;
done;

image

  • while循环使用(while/do/done)

while语句结构

while 条件语句

do

action

done;

实例1:

#!/bin/sh
i=10;
while [[ $i -gt 5 ]];do
    echo $i;
    ((i--));
done;

运行结果:========================

sh testwhile1.sh
10
9
8
7
6

实例2:(循环读取文件内容:)

#!/bin/sh

while read line;do
    echo $line;
done < /etc/hosts;

 

运行结果:===================

sh testwhile2.sh


# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 centos5 localhost.localdomain localhost

  • until循环语句

语法结构:

until 条件

do

action

done

意思是:直到满足条件,就退出。否则执行action.

实例(testuntil.sh):

#!/bin/sh

a=10;

until [[ $a -lt 0 ]];do

echo $a;

((a—));

done;

结果:

sh testuntil.sh

10
9
8
7
6
5
4
3
2
1
0

三、shell选择语句(case、select用法)

  • case选择语句使用(case/esac)

语法结构

case $arg in 
    pattern | sample) # arg in pattern or sample 
    ;; 
    pattern1) # arg in pattern1 
    ;; 
    *) #default 
    ;; 
esac 

说明:pattern1 是正则表达式,可以用下面字符:

                 *       任意字串
                 ?       任意字元
                 [abc]   a, b, 或c三字元其中之一
                 [a-n]   从a到n的任一字元
                 |       多重选择

 

实例:

#!/bin/sh 

case $1 in
start | begin)
    echo "start something" 
    ;;
stop | end)
    echo "stop something" 
    ;;
*)
    echo "Ignorant" 
    ;;
esac

运行结果:======================

 

testcase.sh start
start something

  • select语句使用方法(产生菜单选择)

语法:

select 变量name  in seq变量

do

    action

done

实例:

#!/bin/sh 

select ch in "begin" "end" "exit"
do
case $ch in
"begin")
    echo "start something" 
    ;;
"end")
    echo "stop something" 
    ;;
"exit")
    echo "exit" 
    break;
    ;;
*)
    echo "Ignorant" 
    ;;
esac
done;

运行结果:

image

说明:select是循环选择,一般与case语句使用。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用BioPython库来从NCBI下载基因序列,下面是一个示例代码:from Bio import Entrez# Email address Entrez.email = "example@example.com"# Search for the gene handle = Entrez.esearch(db="nucleotide", term="gene_name") record = Entrez.read(handle)# Download the gene handle = Entrez.efetch(db="nucleotide", id=record["IdList"], rettype="fasta", retmode="text") gene_sequence = handle.read()print(gene_sequence) ### 回答2: 要利用PythonNCBI下载基因序列,可以使用biopython库来实现。Biopython是一个专门用于生物信息学的Python库,提供了众多工具和函数来处理基因、蛋白质及其他生物信息的分析和操作。 下面是一个示例代码,演示如何从NCBI下载基因序列: ```python from Bio import SeqIO from Bio import Entrez # 设置NCBI邮箱 Entrez.email = "your_email@example.com" # 查询指定基因 gene_query = "human[Orgn] AND BRCA1[Gene]" handle = Entrez.esearch(db="nucleotide", term=gene_query, retmode="xml") record = Entrez.read(handle) handle.close() # 获取查询结果中的基因序列 gene_id = record["IdList"][0] handle = Entrez.efetch(db="nucleotide", id=gene_id, rettype="fasta", retmode="text") gene_seq = SeqIO.read(handle, "fasta") handle.close() # 打印基因序列 print("Gene ID:", gene_id) print("Gene Description:", gene_seq.description) print("Gene Sequence:") print(gene_seq.seq) ``` 在这个示例代码中,首先需要设置自己的NCBI邮箱(将"your_email@example.com"替换为你的邮箱地址),这样可以方便地与NCBI服务器进行通信。 接下来,通过`Entrez.esearch()`函数来搜索指定的基因。这里以人类的BRCA1基因作为示例,查询条件为"human[Orgn] AND BRCA1[Gene]",即只搜索人类中的BRCA1基因。 然后,可以通过`Entrez.efetch()`函数来根据查询结果中的基因ID获取基因序列信息。设置`rettype`为"fasta"表示以FASTA格式返回基因序列。使用`SeqIO.read()`函数来解析FASTA文件,并将序列保存在`gene_seq`变量中。 最后,打印基因序列的相关信息,包括基因ID、描述以及序列本身。 以上代码仅为简单示例,实际中还可以根据需要进行更复杂的查询和操作。 ### 回答3: 要从NCBI下载基因序列,可以使用Biopython库中的Entrez模块。以下是一个用Python代码示例,用于从NCBI下载一个基因序列: ```python from Bio import Entrez, SeqIO # 设置NCBI邮箱 Entrez.email = 'your_email@example.com' # 设置搜索的关键词和数据库 search_term = 'KRAS[Gene Name]' database = 'nucleotide' # 搜索并获取符合条件的序列的ID search_handle = Entrez.esearch(db=database, term=search_term) search_result = Entrez.read(search_handle) search_handle.close() id_list = search_result['IdList'] # 从ID列表中下载序列 download_handle = Entrez.efetch(db=database, id=id_list[0], rettype='fasta', retmode='text') seq_record = SeqIO.read(download_handle, 'fasta') download_handle.close() # 打印基因序列的描述和序列信息 print('Description:', seq_record.description) print('Sequence:', seq_record.seq) ``` 要运行上述代码,首先需要安装Biopython库,可以使用`pip install biopython`命令进行安装。 在代码示例中,我们首先设置了NCBI邮箱,这是为了提高请求的速度和限制。然后,我们设置了要搜索的关键词和数据库,本例中我们搜索了基因名为KRAS的序列,使用了nucleotide数据库。 接下来,我们使用`Entrez.esearch()`函数搜索符合条件的序列的ID,并使用`Entrez.efetch()`函数根据ID下载序列。最后,我们使用`SeqIO.read()`函数读取下载的序列,并使用`description`和`seq`属性打印序列的描述和序列信息。 请注意,在使用上述代码之前,请确保替换`your_email@example.com`为你自己的邮箱地址,并根据你要下载的特定基因的要求修改`search_term`的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值