我希望能够编写一个程序,当你输入命令时,它会做一些事情,比如计算你使用的次数cd
。与此类似的东西:
[ : ~ $] cd public_html
Congratulations! You've earned the Badge 'cd master!'. Level up!
到目前为止,我所有的 C++ 文件包括:
#include <iostream>
int main(int argc, char* argv[]) {
int counter = 0;
for (int i = 1; i < argc; i++) {
std::cout << argv[i] << std::endl;
if (argv[i] == "cd")
std::cout << "Badge earned 'cd master!' +5120 experience points" << std::endl;
}
return 0;
}
因为它反映了一种尝试的解决方案,涉及:
#!/bin/sh
bash | tee ./main
和
bind 'RETURN: "echo $(!!) | tee ~/.main \n"'
我决定一起去
export PROMPT_COMMAND='history | tail -n1'
但这意味着必须解析输出。
完成此任务的最简单方法是什么?
编辑
这是我设法制作的:
#!/bin/sh
export COUNTER=0
export MAXWIDTH=10
export TNL=1000
update_prompt() {
export PS1="> "
}
cd() {
COUNTER=$(($COUNTER + 25));
echo +25;
builtin cd $@;
}
help() {
echo "custom commands are: score";
}
score() {
echo $COUNTER"/"$TNL
BAR=$(yes "#" | head -n 10 | tr -d '\n')
OVERLAY=$(yes "%" | head -n 10 | tr -d '\n')
WIDTH=$(echo "$COUNTER*$MAXWIDTH/$TNL" | bc)
FIRST=${BAR:0:WIDTH}
SECOND=${OVERLAY:0:$((MAXWIDTH-WIDTH))}
echo "["$FIRST$SECOND"]"
}
exit() {
echo "Bye bye";
builtin exit $@;
}
export -f update_prompt
export -f cd # export the function
export -f help
export -f score
export -f exit
bash # run subshell with the exported functions
update_prompt