我正在尝试编写一个bash脚本,它将获取在后台运行的命令的输出.不幸的是我不能让它工作,我分配输出的变量是空的 - 如果我用echo命令替换赋值,一切都按预期工作.
#!/bin/bash
function test {
echo "$1"
}
echo $(test "echo") &
wait
a=$(test "assignment") &
wait
echo $a
echo done
Run Code Online (Sandbox Code Playgroud)
此代码生成输出:
echo
done
Run Code Online (Sandbox Code Playgroud)
将作业更改为
a=`echo $(test "assignment") &`
Run Code Online (Sandbox Code Playgroud)
工作,但似乎应该有一个更好的方法来做到这一点.
如果一个类Foo有一个静态成员变量Bar,我希望Bar析构函数只能在析构函数的最后一个实例运行之后才能Foo运行.下面的代码片段不会发生这种情况(gcc 6.3,clang 3.8):
#include <memory>
#include <iostream>
class Foo;
static std::unique_ptr<Foo> foo;
struct Bar {
Bar() {
std::cout << "Bar()" << std::endl;
}
~Bar() {
std::cout << "~Bar()" << std::endl;
}
};
struct Foo {
Foo() {
std::cout << "Foo()" << std::endl;
}
~Foo() {
std::cout << "~Foo()" << std::endl;
}
static Bar bar;
};
Bar Foo::bar;
int main(int argc, char **argv) {
foo = std::make_unique<Foo>();
}
Run Code Online (Sandbox Code Playgroud)
输出:
Bar()
Foo()
~Bar()
~Foo() …Run Code Online (Sandbox Code Playgroud) 我正在尝试序列化一个对象(在这种情况下是一个简单的字符串),加密它,并将其写入文件.加密似乎有效,但解密总是失败.我试过四处寻找,但我似乎无法弄清楚我做错了什么..
// Create a new key to encrypt and decrypt the file
byte[] key = "password".getBytes();
// Get a cipher object in encrypt mode
Cipher cipher = null;
try {
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey desKey = skf.generateSecret(dks);
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, desKey);
} catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException ex) {
System.err.println("[CRITICAL] Incryption chiper error");
}
// Encrypt the file
try {
new ObjectOutputStream(new CipherOutputStream(new FileOutputStream("test"), cipher)).writeObject("test text");
} catch (IOException e) …Run Code Online (Sandbox Code Playgroud) 我需要通过递归给定对象的所有接口和超类.当父是另一个类时,这很好,但是当一个接口扩展另一个接口时,我得到一个NullPointerException.
这是一个例子:
public class Test {
private interface Foo {
}
private interface Bar extends Foo {
}
public static void main(String args[]) {
System.out.println(Bar.class.getSuperclass().getSimpleName());
}
}
Run Code Online (Sandbox Code Playgroud)
这会抛出NullPointerException而不是Foo按预期打印.有没有办法获得界面的超类?