我正在寻找像这样的东西:
$ startmem
$ command1
$ command2
$ command3
$ endmem
后来,我想这样称呼它:
mem
,执行命令
要清除,请再次使用startmem
。有没有办法解决这个问题?
答案 0 :(得分:3)
你可以这样做:
#include <iostream>
#include <vector>
class B;
class C;
class A {
public:
virtual bool sm(A* a) = 0;
virtual bool sm(B* b) = 0;
virtual bool sm(C* c) = 0;
};
class B : public A {
public:
bool sm(A* a) {
return a->sm(this);
}
bool sm(B* b) {
std::cout << "In B doing B" << std::endl;
return true;
}
bool sm(C* c) {
std::cout << "In B doing C" << std::endl;
return false;
}
};
class C : public A {
public:
bool sm(A* a) {
return a->sm(this);
}
bool sm(B* b) {
std::cout << "In C doing B" << std::endl;
return true;
}
bool sm(C* c) {
std::cout << "In C doing C" << std::endl;
return false;
}
};
int main() {
std::vector<A*> objects;
objects.push_back(new B());
objects.push_back(new C());
objects[0]->sm(objects[0]);
objects[0]->sm(objects[1]);
objects[1]->sm(objects[0]);
objects[1]->sm(objects[1]);
std::cin.get();
return 0;
}
这将接受你的输入,但不包括&#34; endmem&#34;线,并生成一个&#34; mem&#34;功能
如果您希望它持久存在,那么您别无选择,只能将其写入磁盘
startmem () { eval "mem() { $(sed '/^endmem/Q'); }"; }
但是在这一点上,为什么不创建startmem () { sed '/^endmem/Q' > ~/.mem_commands; }
mem () { source ~/.mem_commands; }
,将~/bin/mem
添加到PATH,只需要在想要更改时编辑~/bin
文件。