小编Dav*_*ley的帖子

无法在PowerShell中找到类型[System.Web.HttpUtility]

我正在尝试使用PowerShell获取Microsoft Translator应用程序的访问令牌,但由于该错误,该过程中的某些命令失败:

Unable to find type [System.Web.HttpUtility]
Run Code Online (Sandbox Code Playgroud)

首先我输入了代码,但是如果我将代码直接从MSDN页面复制粘贴到PowerShell ISE中(并替换缺少的值),则显示相同的错误:

# ...
$ClientID = '<Your Value Here From Registered Application>'
$client_Secret = ‘<Your Registered Application client_secret>'

# If ClientId or Client_Secret has special characters, UrlEncode before sending request
$clientIDEncoded = [System.Web.HttpUtility]::UrlEncode($ClientID)
$client_SecretEncoded = [System.Web.HttpUtility]::UrlEncode($client_Secret)
# ...
Run Code Online (Sandbox Code Playgroud)

我是PowerShell的新手(通常使用Linux进行开发)但我的猜测是,这应该是开箱即用的,而不必安装其他工具; 如果没有,我在哪里可以找到它们?

powershell azure microsoft-translator

16
推荐指数
1
解决办法
2万
查看次数

在后台从 Bash 脚本启动进程,然后将其置于前台

以下是我拥有的一些代码的简化版本:

#!/bin/bash

myfile=file.txt
interactive_command > $myfile &
pid=$!

# Use tail to wait for the file to be populated
while read -r line; do
  first_output_line=$line
  break # we only need the first line
done < <(tail -f $file)
rm $file

# do stuff with $first_output_line and $pid
# ...
# bring `interactive_command` to foreground?
Run Code Online (Sandbox Code Playgroud)

我想在将interactive_command第一行输出存储到变量后将其带到前台,以便用户可以通过调用此脚本与其进行交互。

但是,似乎 usingfg %1在脚本的上下文中不起作用,并且我无法fg与 PID 一起使用。有没有办法做到这一点?

(另外,是否有更优雅的方式来捕获第一行输出,而不写入临时文件?)

bash job-control

5
推荐指数
1
解决办法
1416
查看次数

如何使用 SeedableRng 生成随机数?

我有以下 Rust 代码,其最终目标是在给定公钥的情况下生成确定性用户名:

use openssl::pkey::{PKey, Private};
use rand::{Rng, SeedableRng};
use std::convert::TryInto;

pub struct MyRng([u8; 32]);

impl SeedableRng for MyRng {
    type Seed = [u8; 32];

    fn from_seed(seed: Self::Seed) -> MyRng {
        MyRng(seed)
    }
}

fn generate_username(keypair: &PKey<Private>) {
    let public_bytes: Vec<u8> = keypair.public_key_to_der().unwrap();
    let public_bytes: [u8; 32] = public_bytes.try_into().unwrap();
    let seeded_rng: MyRng = SeedableRng::from_seed(public_bytes);

    let num = rand::thread_rng().gen_range(0..32);
}
Run Code Online (Sandbox Code Playgroud)

使用rand::thread_rng()创建 aThreadRng允许我创建一个没有种子的随机数。如果我将最后一行更改为,seeded_rng.gen_range(0..32)那么我会被告知未找到方法,并且特征RngRngCore未实现。然而,我不确定如何去实现这些特征。

如何使用 的种子生成随机数[u8; 32]?另外,我的代码是否有任何可以改进的地方?(例如,我有必要创建自己的结构吗?)

random rust

2
推荐指数
1
解决办法
2544
查看次数