我想在 shell 脚本中比较两个浮点数。以下代码不起作用:
#!/bin/bash
min=12.45
val=10.35
if (( $val < $min )) ; then
min=$val
fi
echo $min
Run Code Online (Sandbox Code Playgroud) 我想并行化for
以下代码的循环。这该怎么做?
#!/bin/bash
N=$1
n=$2
for (( i=1; i<=$N; i++ )); do
min=100000000000000 //set min to some garbage value
for (( j=1; j<=$n; j++ )); do
val=$(/path/to/a.out)
val2=`echo $val | bc`
if (( $val2 < $min )); then
min=$val2;
fi
done
arr=("${arr[@]}" "$min")
done
Run Code Online (Sandbox Code Playgroud) 我有一个文件 ( file_name
),它Result:
在一行的开头只包含一次出现的字符串。我想打印Result:
该行中字符串之后的所有字符,直到遇到空格。我应该使用哪个 shell 命令?
grep "Result: " file_name | tail -c +9
Run Code Online (Sandbox Code Playgroud)
不管用。
我想并行化一个 for 循环,其中循环中的迭代次数可能非常大,例如 10^6。所以,如果我可以创建线程而不是进程会更好。怎么做?代码如下
N=$1
for (( i=0; i < $N; i++ )); do
./random >> output /* in each iteration one random number is appended to
a file "output" */
done
Run Code Online (Sandbox Code Playgroud) 我对shell脚本不太熟悉。我想为以下伪代码编写一个shell脚本:
min=some garbage value
for(i=1 to N){ // N and n will be taken as input for the shell script.
for(j=1 to n){
val= './a.out' // call the executable a.out and take the output in val
if(val<min) // a.out is a random number generator script
min=val;
}
arr[k++]=min;
}
// Then I want to calculate the sum of the elements of the array 'arr'.
Run Code Online (Sandbox Code Playgroud)
如何通过 shell 脚本执行这些操作?
a.out
是这样的,它将产生一串数字作为输出。我必须将此字符串转换为变量val
。这该怎么做?例如,作为输出a.out
生成12.34
但是12.34
是字符数组而不是浮点数。我想将其转换为通过 shell 脚本浮动,但我无权更改a.out …