如果我有这样的BASH脚本:
#!/bin/bash
f() {
# echo function name, "f" in this case
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?这可以用在诸如的帮助消息中
printf "Usage: %s: blah blah blah \n" $(basename $0) >&2;
Run Code Online (Sandbox Code Playgroud)
只有在这种情况下我想要的不是$0,这是脚本的文件名.
为什么以下代码会抛出异常?请注意,该文件是一个/proc/pid/stat文件,因此它可能会受到内核的干扰.
// Checked that file does exist
try {
std::ifstream file(path.c_str());
// Shouldn't even be necessary because it's the default but it doesn't
// make any difference.
file.exceptions(std::ifstream::goodbit);
// Read the stream into many fields
// !!!! The exception was thrown here.
file >> _ >> comm >> state >> ppid >> pgrp >> session >> tty_nr
/* >> ... omitted */;
file.close();
} catch (const std::ifstream::failure& e) {
std::cout << "Exception!!!! " << e.what();
}
Run Code Online (Sandbox Code Playgroud)
例外是"读取文件的basic_filebuf :: …