如何简化我的 bash 代码?

发布于 2024-12-10 07:59:06 字数 508 浏览 0 评论 0原文

我在 bash 中编写了以下代码,该代码倾向于打印 200x13 随机数的矩阵(我们有 200 行,每行打印 13 个数字)。我希望这段代码更加灵活,以便可以更改打印矩阵的大小(行数和每行数),而无需重新编写代码,

for (( k = 0; k < 200; k++  ))
do

    echo $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 ))

done

感谢任何帮助。

I've write the following code in bash, this code tends to print a matrix of 200x13 random numbers (we have 200 line printed with 13 numbers per line). I want this code to be more flexible such that one can change size of the matrix printed (number of line and number of number per line) without re-write the code

for (( k = 0; k < 200; k++  ))
do

    echo $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 )) $(( $RANDOM - 16384 ))

done

thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睡美人的小仙女 2024-12-17 07:59:06

首先,您想要将列的打印更改为另一个 for 循环:

for (( k = 0; k < 200; k++  ))
do
 for (( j = 0; j < 3; j++))
 do
    echo -n $(( $RANDOM - 16384 ))" "  # print your random number

 done

 echo     # print newline 
done

然后,您可以参数化您的 k max 和 j max:

rows=200   # set the number of rows
cols=3    # set the number of columns

for (( k = 0; k < $rows; k++  ))
do
   for (( j = 0; j < $cols; j++))
   do
       echo -n $(( $RANDOM - 16384 ))" "   

   done

   echo
done

First, you want to change the printing of the columns to another for-loop:

for (( k = 0; k < 200; k++  ))
do
 for (( j = 0; j < 3; j++))
 do
    echo -n $(( $RANDOM - 16384 ))" "  # print your random number

 done

 echo     # print newline 
done

Then, you can parametrize your k max and j max:

rows=200   # set the number of rows
cols=3    # set the number of columns

for (( k = 0; k < $rows; k++  ))
do
   for (( j = 0; j < $cols; j++))
   do
       echo -n $(( $RANDOM - 16384 ))" "   

   done

   echo
done
风向决定发型 2024-12-17 07:59:06

将其放入 shell 函数 并使用 printf 而不是 echo

printMatrix() {
  cols=${1:-13}
  rows=${2:-200}
  for (( i=0; i<$rows; i++ ))
  do
    for (( j=0; j<$cols; j++ ))
      printf "%13d" $(( $RANDOM - 16384))
    done
    echo
  done
}

printMatrix # will print 200x13 matrix
printMatrix 5 10  # with print 10x5 matrix

由于这是在 shell 函数中,因此您可以获取函数定义并直接从 shell 调用它。如果该函数位于名为 shfuncs 的文件中,那么您可以获取它并按如下方式运行它:

bash$ source shfuncs
bash$ printMatrix 3 2
11118        31           -9753        
-13700       -12008       -1779 
bash$

Put it in a shell function and use printf instead of echo.

printMatrix() {
  cols=${1:-13}
  rows=${2:-200}
  for (( i=0; i<$rows; i++ ))
  do
    for (( j=0; j<$cols; j++ ))
      printf "%13d" $(( $RANDOM - 16384))
    done
    echo
  done
}

printMatrix # will print 200x13 matrix
printMatrix 5 10  # with print 10x5 matrix

Since this is in a shell function, you can source the function definition and call it directly from the shell. If the function is in a file named shfuncs, then you can source it and run it like follows:

bash$ source shfuncs
bash$ printMatrix 3 2
11118        31           -9753        
-13700       -12008       -1779 
bash$
尤怨 2024-12-17 07:59:06

要简单地做到这一点,只需添加一个嵌套循环来重复列即可。

另外,在 bash 中,您可以使用大括号扩展执行 for 循环...

for a in {1..200}
do # Rows...
    for b in {1..13}
    do # Columns...
        echo -n "$(( $RANDOM - 16384 )) " 
        # -n suppresses newlines
    done
    echo 
    # do newline at the end of the row.
done

如果脚本将变得更加复杂。我建议将脚本抽象为函数。

function printRandomNumber() { 
   seed={1:-16384}
   echo -n "$(( $RANDOM - $seed ))" 
}

function printRandomMatrix() {
    rows={1:-200} # default 200 if not set.
    cols={2:-13} # default 13 if not set.

    for a in {1..$rows}
    do
        for b in {1..$cols}
        do  
            printRandomNumber
        done
        echo 
    done
}

然后用这种方式调用它,

printRandomMatrix 
# print the default row/column matrix.

printRandomMatrix 10 10
# print a matrix of 10 by 10.

您可以更轻松地维护脚本并适应更改,例如,如果您需要使用 printf 格式化输出,如 D.Shawley 的答案所示。当然,您可能需要各种可能的视觉格式(左/右列对齐等),这些可以通过各种技巧来实现... 有关 shell 文本格式的更多信息

To do this simply, just add a nested loop to repeat the columns.

Also in bash you can do a for loop with brace expansion ...

for a in {1..200}
do # Rows...
    for b in {1..13}
    do # Columns...
        echo -n "$(( $RANDOM - 16384 )) " 
        # -n suppresses newlines
    done
    echo 
    # do newline at the end of the row.
done

If the script was going to become more complicated. I'd suggest abstracting the script into functions.

function printRandomNumber() { 
   seed={1:-16384}
   echo -n "$(( $RANDOM - $seed ))" 
}

function printRandomMatrix() {
    rows={1:-200} # default 200 if not set.
    cols={2:-13} # default 13 if not set.

    for a in {1..$rows}
    do
        for b in {1..$cols}
        do  
            printRandomNumber
        done
        echo 
    done
}

Then call it with

printRandomMatrix 
# print the default row/column matrix.

printRandomMatrix 10 10
# print a matrix of 10 by 10.

This way you can more easily maintain the script and adapt to changes, e.g. if you needed to format the output using printf as shown in D.Shawley's answer. Of course you may want a variety of possible visual formatting (left/right column alignment etc.) these can be achieved with a variety of tricks... More on shell text formatting

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文