审计bash RPG的bash命令 [英] Audit bash commands for bash RPG

查看:183
本文介绍了审计bash RPG的bash命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想能够编写一个程序,当你输入命令时,它会做一些事情,例如你使用 cd 的次数。类似这样的:

  [:〜$] cd public_html 
恭喜!你已经获得了徽章的高手!升级!

我所有的C ++文件到目前为止是:

  #include< iostream> 

int main(int argc,char * argv []){
int counter = 0;
for(int i = 1; i std :: cout< argv [i]<< std :: endl;
if(argv [i] ==cd)
std :: cout<< 徽章赚cd高手!+ 5120经验值< std :: endl;
}
return 0;
}

由于它反映了一个尝试的解决方案:

 #!/ bin / sh 
bash | tee ./main

  bind'RETURN:echo $(!!)| tee〜/ .main \\\
'

我决定使用

  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(){
echocustom commands are:score;
}
score(){
echo $ COUNTER/$ TNL
BAR = $(yes#| head -n 10 | tr -d'\\\
')
OVERLAY = $(yes%| head -n 10 | tr -d'\\\
')
WIDTH = $(echo$ COUNTER * $ MAXWIDTH / $ TNL| bc )
FIRST = $ {BAR:0:WIDTH}
SECOND = $ {OVERLAY:0:$((MAXWIDTH-WIDTH))}
echo[$ FIRST $ SECOND]
}
exit(){
echoBye bye;
builtin exit $ @;
}
export -f update_prompt
export -f cd#export函数
export -f help
export -f score
export -f exit

bash#run带有导出函数的subshel​​l
update_prompt


解决方案

一个简单的解决方案是在shell中覆盖你的shell的 cd 命令。例如,在Bash或ZSH中:

  cd(){
echoCongratulations
builtin cd $ @;
}

(例如用于 autoenv 。)



您可以对所有其他命令执行相同操作。你也可以从那里调用你的C ++代码。



如果你想把它放入脚本,例如。 name it learn-bash.sh

  ..} 
export -f cd#导出函数

bash#运行带有导出函数的subshel​​l






另一个解决方案,你有更多的力量,但是更多的涉及:获取Bash的源代码(它的C)它(由C或C ++)。然后你可以基本上做任何你想要的。你有一切直接在那里,即解析的命令等。


I want to be able to write a program that when you type in commands, it will do things like count the amount of times you've used cd. Something similar to this:

[ : ~ $] cd public_html
Congratulations! You've earned the Badge 'cd master!'. Level up!

All my C++ file consists of so far is:

#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;
}

As it reflects one attempted solution involving:

#!/bin/sh
bash | tee ./main

and

bind 'RETURN: "echo $(!!) | tee ~/.main \n"'

I've decided to go with

export PROMPT_COMMAND='history | tail -n1'

But that would mean having to parse the output.

What's the easiest way of accomplishing this?

edit

Here's what I've managed to produce:

#!/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

解决方案

An easy solution is to overwrite the cd command of your shell inside the shell itself. For example, in Bash or ZSH:

cd() {
  echo "Congratulations";
  builtin cd $@;
}

(This is for example used in projects like autoenv.)

You can do the same for all other commands. You can also call your C++ code from there.

If you want to put that into a script, e.g. name it learn-bash.sh:

cd() { ... }
export -f cd # export the function

bash # run subshell with the exported functions


Another solution, where you have much more power, but which is way more involved: take the source code of Bash (it's C) and extend it (by C or C++). Then you can basically do whatever you want. And you have everything directly there, i.e. the parsed commands, etc.

这篇关于审计bash RPG的bash命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆