小编Mal*_*ppa的帖子

如何在 bash 中有效地生成大的、均匀分布的随机整数?

我一直在想这将是获得最佳的方式很好在bash,即随机性,这将是一个过程,以获得之间的随机正整数MIN,并MAX使得

  1. 范围可以是任意大的(或者至少可以达到 2 32 -1);
  2. 值均匀分布(即没有偏差);
  3. 它是有效的。

在 bash 中获得随机性的一种有效方法是使用$RANDOM变量。但是,这仅对 0 到 2 15 -1之间的值进行采样,这对于所有用途来说可能不够大。人们通常使用模数将其置于他们想要的范围内,例如,

MIN=0
MAX=12345
rnd=$(( $RANDOM % ($MAX + 1 - $MIN) + $MIN ))
Run Code Online (Sandbox Code Playgroud)

此外,这会产生偏差,除非$MAX碰巧除以 2 15 -1=32767。例如,如果$MIN是 0 并且$MAX是 9,那么值 0 到 7 的可能性比值 8 和 9 稍大,$RANDOM永远不会是 32768 或 32769。随着范围的增加,这种偏差会变得更糟,例如,如果$MIN是 0 并且$MAX是9999,那么数字 0 到 2767 的概率为4 / 32767,而数字 2768 到 9999 的概率仅为 …

command-line bash shell-script

35
推荐指数
5
解决办法
2万
查看次数

如何在 FAT32 分区上手动设置脏位

出于某些调试目的,我希望能够手动FAT32 分区的脏位设置为 true。

我发现吨关于如何使用信息fsck.vfat,以去除如何将脏位,但没有设置它。

这是可能的,因为mount它。当安装了 FAT32 分区(dirty 为 false)时,mount将dirty 设置为 true(并umount再次将其设置为 false)。我正在寻找一种在不安装分区的情况下设置脏位的方法,即模拟它没有完全卸载。

partition mount fsck vfat

4
推荐指数
1
解决办法
6398
查看次数

Predicting the PID of previously started SSH command

This is the weirdest thing.

In a script, I start an SSH tunnel like so:

ssh -o StrictHostkeyChecking=no -fND 8080 foo@bar
Run Code Online (Sandbox Code Playgroud)

This starts an ssh instance that goes into the background, and script execution continues. Next, I save its PID (for killing it later) by using bash's $! variable. For this to work, I append & to the ssh command even though it already goes into the background by itself (otherwise $! doesn't contain anything). Thus, for example the …

bash ssh ssh-tunneling shell-script

4
推荐指数
1
解决办法
1404
查看次数

标签 统计

bash ×2

shell-script ×2

command-line ×1

fsck ×1

mount ×1

partition ×1

ssh ×1

ssh-tunneling ×1

vfat ×1