Bash for 循环 - 在 n 之后命名(这是用户的输入)

标签 bash

<分区>

我正在使用 for i in {1..n} 循环遍历命令,并希望输出文件具有 n 扩展名。

例如:

  for i in {1..2}
  do cat FILE > ${i}_output
  done

但是 n 是用户的输入:

  echo 'Please enter n'
  read number
  for i in {1.."$number"}
  do 
    commands > ${i}_output
  done

循环遍历 n 次 - 这工作正常,但我的输出看起来像这样 {1..n}_output

如何在这样的循环中命名我的文件?

编辑

也试过这个

  for i in {1.."$number"} 
  do
    k=`echo ${n} | tr -d '}' | cut -d "." -f 3`
    commands > ${k}_output
  done

但它不起作用。

最佳答案

使用 "C-style" for-loop :

echo 'Please enter n'
read number
for ((i = 1; i <= number; i++))
do 
    commands > ${i}_output
done

请注意,for 循环 header 中的 $ 不需要位于 numberi 之前,但是double-parentheses是必需的。

关于Bash for 循环 - 在 n 之后命名(这是用户的输入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14870406/

相关文章:

linux - scp:如何发现复制完成

php - 从 TTY 到 PHP 的 AWK 管道输出

bash - 从 Shell 脚本将日期传递给 C 程序

arrays - Bash 引用数组扩展

linux - 如何使用 shell 在 linux 中逐一打印具有完整信息的所有用户列表

linux - 如何使用 bash 确定声卡类型?

linux - 在 Bash 中显示调用堆栈

bash - 如何使用 Dockerfile 设置 $PS1?

bash - 将每个整数转换为简单的 ASCII 图形

linux - 命令 "+0800"中的 "stat"是什么意思?