【发布时间】:2018-07-14 19:48:35
【问题描述】:
直接变量赋值和通过while循环从bash中的文件读取是有区别的。请参见下面的示例。 url1 和 url2 是不同的。如何将url1 设为url2 代表bash 中的while 循环?谢谢你。
它不适用于 Debian 中的 zsh 5.3.1 (x86_64-debian-linux-gnu) 或 GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)。
cat 1.csv(注意:1.csv的最后一行有一个新的空行。)
mgm4447943.3
mgm4447944.3
代码:
#! /bin/bash
while read -r MGRAST_ID; do
echo $MGRAST_ID
url1=http://api.metagenomics.anl.gov/1/download/${MGRAST_ID}?file=050.1
echo $url1
MGRAST_ID1="mgm4447943.3"
url2=http://api.metagenomics.anl.gov/1/download/${MGRAST_ID1}?file=050.1
echo $url2
done < 1.csv
输出将是
mgm4447943.3
?file=050.1metagenomics.anl.gov/1/download/mgm4447943.3
http://api.metagenomics.anl.gov/1/download/mgm4447943.3?file=050.1
mgm4447944.3
?file=050.1metagenomics.anl.gov/1/download/mgm44GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)47944.3
http://api.metagenomics.anl.gov/1/download/mgm4447943.3?file=050.1
【问题讨论】:
-
我已经使用 bash v4.4.23 执行了您的代码,并且 url1 打印正确。你用的是什么外壳?
-
@hnicke GNU bash,版本 4.4.12(1)-release (x86_64-pc-linux-gnu) 在 Debian 拉伸中