bash脚本。运行带有计数数字和名称的命令

时间:2016-09-24 14:23:06

标签: linux bash shell

我尝试编写脚本代码。它问我有多少"什么"你要?我回答了一个像10的数字

#!/bin/bash
echo -n "Please enter some input: "
read input

现在我喜欢执行10次命令,包括从001到010的数字 例如,我得到了正确的数字:

seq -f "%03g" 1 $input

但是如何使用这个数字执行now命令,如:

command anyname-001 -some -parameter
command anyname-002 -some -parameter
command anyname-003 -some -parameter

...

thanx求助

3 个答案:

答案 0 :(得分:1)

使用GNU bash:

for ((i=1;i<=$input;i++)); do
  printf -v number "%03d" "$i"
  echo command anyname-${number} -some -parameter
done

如果一切正常,请删除echo

答案 1 :(得分:1)

只需在范围内使用for循环。这应该可以解决问题。

MyProject[2141:497518] [LogMessageLogging] 6.1 <private>
Painter Z index: 1023 is too large (max 255)
Painter Z index: 1023 is too large (max 255)
Painter Z index: 1023 is too large (max 255)
Painter Z index: 1023 is too large (max 255)
Painter Z index: 1023 is too large (max 255)
Painter Z index: 1023 is too large (max 255)
ERROR /BuildRoot/Library/Caches/com.apple.xbs/Sources/VectorKit/VectorKit-1228.30.7.17.9/GeoGL/GeoGL/GLCoreContext.cpp 1763: InfoLog SolidRibbonShader:
ERROR /BuildRoot/Library/Caches/com.apple.xbs/Sources/VectorKit/VectorKit-1228.30.7.17.9/GeoGL/GeoGL/GLCoreContext.cpp 1764: WARNING: Output of vertex shader 'v_gradient' not read by fragment shader


Painter Z index: 1023 is too large (max 255)
Painter Z index: 1023 is too large (max 255)
Painter Z index: 1023 is too large (max 255)
Painter Z index: 1023 is too large (max 255)
Painter Z index: 1023 is too large (max 255)

答案 2 :(得分:1)

一行(好吧,正式两行):

#!/bin/bash
printf 'command anyname-%03d -some -parameter\n' $(seq $1)

将脚本调用为:

$ script 10
command anyname-001 -some -parameter
command anyname-002 -some -parameter
command anyname-003 -some -parameter
command anyname-004 -some -parameter
command anyname-005 -some -parameter
command anyname-006 -some -parameter
command anyname-007 -some -parameter
command anyname-008 -some -parameter
command anyname-009 -some -parameter
command anyname-010 -some -parameter