如何使用Shell脚本获取通过率

时间:2019-10-04 22:39:58

标签: shell unix

我有以下脚本,该脚本试图通过shell脚本获取通过率。

脚本:

  n= "$pass"/"$total";
  "$n" * = 100;
  echo "$n"

输出:

  

/tmp/jenkins3601870177535319162.sh:第45行:20/25 * =:没有此类文件或   目录20/25

我不确定以上计算是否正确。但是我只有变量$ pass具有通过测试用例数,而$ total变量具有通过测试用例数。只是想使用shell获得通过的测试用例的百分比。

1 个答案:

答案 0 :(得分:0)

您有两个主要问题:(1)'='符号的任何一侧都不能有空格; (2)Shell使用 integer 数学,因此20/25将等于0

POSIX算术运算符为((...)),其中表达式位于((...))之内。另外,在((...))中,不需要通过在变量名前面加上'$'来解除引用。

要将表达式的结果分配给变量,请在运算符前加上'$',例如n=$((pass/total))

要解决shell使用 integer 数学的事实,可以在除以pass之前将100乘以#!/bin/sh pass=20 total=25 n=$(((pass * 100)/total)) printf "pass percentage: %d\n" "$n" ,至少得到一个整数百分比。例如:

$ sh percent.sh
pass percentage: 80

如果运行脚本,您将得到:

80%

20/25let mongoose = require('mongoose') const server = 'ds221609.mlab.com:21609' const database = 'rest-api-workshop' const user = 'username' const password = 'password' mongoose.connect(`mongodb://${user}:${password}@${server}/${database}`) let CustomerSchema = new mongoose.Schema({ name: String, email: { type: String, requried: true, unique: true } }) module.exports = mongoose.model('Customer', CustomerSchema)的百分比结果。仔细研究一下,如果您还有其他问题,请告诉我。