#include <iostream>
using std::cout;
using std::cin;
int main() {
//SUB-ROUTINE: print directory paths with index, using "complex" algorithm
// (so an external command rather than a bash function)
// (this sub-routine gives choose-able options to a user)
// (please note this sub-routine includes `cout`)
unsigned index;
cout << "Input index: ";
cin >> index;
switch (index) {
case 0:
cout << "/home/user/foo"; break;
case 1:
cout << "/home/user/bar"; break;
default:
cout << "/home/user";
}
}
cd $(a.out)
因为很多原因。一个原因是
a.out
具有比结果目录路径更多的输出。
bash
)? Linux特定的方式是可以的。当然,尽管将目录路径输出到文件(在ramdisk中)并从bash读取它可以实现我想要的,但我认为这不是一个明智的方法。
Changing the directory of the shell through a C++ program
补充:
如果我重写C++程序
#include <iostream>
using std::cout;
using std::cin;
int main(int argc, char **argv) {
if (argc == 1) {
//print directory paths with index
} else {
unsigned index = atoi(argv[1]);
switch (index) {
case 0:
cout << "/home/user/foo"; break;
case 1:
cout << "/home/user/bar"; break;
default:
cout << "/home/user";
}
}
}
,我就可以写了
./a.out
reply -p "Input index: "
cd $(a.out $REPLY)