如何在 bash 中的文件名前添加 -A、-B 和 -C?类似于Python的zip函数
How to prepend -A, -B, and -C to the names of files in bash? Similar to Python's zip function
我正尝试在 bash
中执行以下操作:
>>> import glob
>>> FILES = glob.glob("testing_output/test/intermediate/metawrap_output/initial_binning/*_bins")
>>> " ".join(map(lambda x:" ".join(x), zip(["-A", "-B", "-C"], FILES)))
'-A testing_output/test/intermediate/metawrap_output/initial_binning/maxbin2_bins -B testing_output/test/intermediate/metawrap_output/initial_binning/concoct_bins -C testing_output/test/intermediate/metawrap_output/initial_binning/metabat2_bins'
我可以到达 FILES
部分,但我不知道如何得到最后一行。我想这样做的原因是有时 FILES
中的文件数会是 1-3
。希望是 3 个,但有时会是 2 个或 1 个。
我的问题是,我如何根据可用文件的数量从根本上预先添加 -A
、-B
和 -C
?
也许这会有所帮助:
FILES=("testing_output/test/intermediate/metawrap_output/initial_binning/concoct_bins" "testing_output/test/intermediate/metawrap_output/initial_binning/maxbin2_bins" "testing_output/test/intermediate/metawrap_output/initial_binning/metabat2_bins")
echo ${FILES[*]}
testing_output/test/intermediate/metawrap_output/initial_binning/concoct_bins testing_output/test/intermediate/metawrap_output/initial_binning/maxbin2_bins testing_output/test/intermediate/metawrap_output/initial_binning/metabat2_bins
我能够做到这一点:
python -c """import glob; FILES = glob.glob('testing_output/test/intermediate/metawrap_output/initial_binning/*_bins'); print(' '.join(map(lambda x:' '.join(x), zip(['-A', '-B', '-C'], FILES))))"""
但我不知道如何将它保存到 bash 变量中。
files=("testing_output/test/intermediate/metawrap_output/initial_binning/concoct_bins" "testing_output/test/intermediate/metawrap_output/initial_binning/maxbin2_bins" "testing_output/test/intermediate/metawrap_output/initial_binning/metabat2_bins")
letters=({A..Z})
for ((i=0; i<${#files[@]}; i++)); do echo "-${letters[$i]} ${files[$i]}"; done
输出:
-A testing_output/test/intermediate/metawrap_output/initial_binning/concoct_bins
-B testing_output/test/intermediate/metawrap_output/initial_binning/maxbin2_bins
-C testing_output/test/intermediate/metawrap_output/initial_binning/metabat2_bins
我正尝试在 bash
中执行以下操作:
>>> import glob
>>> FILES = glob.glob("testing_output/test/intermediate/metawrap_output/initial_binning/*_bins")
>>> " ".join(map(lambda x:" ".join(x), zip(["-A", "-B", "-C"], FILES)))
'-A testing_output/test/intermediate/metawrap_output/initial_binning/maxbin2_bins -B testing_output/test/intermediate/metawrap_output/initial_binning/concoct_bins -C testing_output/test/intermediate/metawrap_output/initial_binning/metabat2_bins'
我可以到达 FILES
部分,但我不知道如何得到最后一行。我想这样做的原因是有时 FILES
中的文件数会是 1-3
。希望是 3 个,但有时会是 2 个或 1 个。
我的问题是,我如何根据可用文件的数量从根本上预先添加 -A
、-B
和 -C
?
也许这会有所帮助:
FILES=("testing_output/test/intermediate/metawrap_output/initial_binning/concoct_bins" "testing_output/test/intermediate/metawrap_output/initial_binning/maxbin2_bins" "testing_output/test/intermediate/metawrap_output/initial_binning/metabat2_bins")
echo ${FILES[*]}
testing_output/test/intermediate/metawrap_output/initial_binning/concoct_bins testing_output/test/intermediate/metawrap_output/initial_binning/maxbin2_bins testing_output/test/intermediate/metawrap_output/initial_binning/metabat2_bins
我能够做到这一点:
python -c """import glob; FILES = glob.glob('testing_output/test/intermediate/metawrap_output/initial_binning/*_bins'); print(' '.join(map(lambda x:' '.join(x), zip(['-A', '-B', '-C'], FILES))))"""
但我不知道如何将它保存到 bash 变量中。
files=("testing_output/test/intermediate/metawrap_output/initial_binning/concoct_bins" "testing_output/test/intermediate/metawrap_output/initial_binning/maxbin2_bins" "testing_output/test/intermediate/metawrap_output/initial_binning/metabat2_bins")
letters=({A..Z})
for ((i=0; i<${#files[@]}; i++)); do echo "-${letters[$i]} ${files[$i]}"; done
输出:
-A testing_output/test/intermediate/metawrap_output/initial_binning/concoct_bins -B testing_output/test/intermediate/metawrap_output/initial_binning/maxbin2_bins -C testing_output/test/intermediate/metawrap_output/initial_binning/metabat2_bins