我哪里错了?
我有一些文件如下:
filename_tau.txt
filename_xhpl.txt
fiename_fft.txt
filename_PMB_MPI.txt
filename_mpi_tile_io.txt
Run Code Online (Sandbox Code Playgroud)
我通过tau
,xhpl
,fft
,mpi_tile_io
和PMB_MPI
定位参数给脚本如下:
./script.sh tau xhpl mpi_tile_io fft PMB_MPI
Run Code Online (Sandbox Code Playgroud)
我想要grep在循环内搜索,首先搜索tau,xhpl等等.
point=$1 #initially points to first parameter
i="0"
while [$i -le 4]
do
grep "$str" ${filename}${point}.txt
i=$[$i+1]
point=$i #increment count to point to next positional parameter
done
Run Code Online (Sandbox Code Playgroud)
像这样设置你的for循环.使用这种语法,循环遍历位置参数,依次将每个参数分配给"点".
for point; do
grep "$str" ${filename}${point}.txt
done
Run Code Online (Sandbox Code Playgroud)
有多种方法可以做到这一点,尽管我会使用shift
,但还有另一种方法可以实现多样化。它使用Bash的间接功能:
#!/bin/bash
for ((i=1; i<=$#; i++))
do
grep "$str" ${filename}${!i}.txt
done
Run Code Online (Sandbox Code Playgroud)
这种方法的一个优点是您可以在任何地方启动和停止循环。假设您已经验证了范围,则可以执行以下操作:
for ((i=2; i<=$# - 1; i++))
Run Code Online (Sandbox Code Playgroud)
另外,如果您想要最后一个参数: ${!#}
归档时间: |
|
查看次数: |
6550 次 |
最近记录: |