Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Traversing the BashLex AST Node. #29

Open
shivamgupta01 opened this issue Apr 30, 2018 · 5 comments
Open

Traversing the BashLex AST Node. #29

shivamgupta01 opened this issue Apr 30, 2018 · 5 comments

Comments

@shivamgupta01
Copy link

Hey,

I am trying to traverse through the bashlex.ast.node to find something specific, such as if the bash command is writing something to temp or deleting any file.

Till now I was trying manual check
if (tree[0].tree[i].word) == 'rm' :
return command
But I assume its not the right way to traverse the Bashlex AST tree, what if I need to find the files with are writing to Temp directory.

Can you shed some light on how I can efficiently traverse through the AST and fulfill the above requirement.

@idank
Copy link
Owner

idank commented Apr 30, 2018

I think you want to look for a CommandNode and see if its first WordNode is 'rm'. Then traverse the subsequent WordNodes (which are the arguments to rm) and do the rest of the checks. Does that answer your question?

@shivamgupta01
Copy link
Author

shivamgupta01 commented Apr 30, 2018

Sounds Good to me, can you also explain to check if the file is updating something in /temp?

For Example:

import bashlex
parts = parser.parse("echo "hello world" >/tmp/foo")
print(parts[0])
'''
CommandNode(parts=[WordNode(parts=[] pos=(0, 4) word='echo'), WordNode(parts=[] pos=(5, 18) word='hello world'), RedirectNode(heredoc=None input=None output=WordNode(parts=[] pos=(20, 28) word='/tmp/foo') pos=(19, 28) type='>')] pos=(0, 28))
'''
if parts[0].parts[2] == 'RedirectNode':
print("DoSomething")
This Code is not working.

I want to check if a particular Node is a redirect node or any other particular type of node., I can Manually write up an split function for that, but I am assuming that there is better a way to access this data.

@dAnjou
Copy link

dAnjou commented Jan 23, 2020

Late to the party, I just discovered this library the other day. So, first of all, great work, also on explainshell!

I'd like to go back to the traversal question. I know that I can implement it myself because everything is basically just lists but is there maybe code already that makes it more convenient, like for @shivamgupta01's use case when I only want to hook into visited redirect nodes?

@idank
Copy link
Owner

idank commented Jan 23, 2020

I think the nodevisitor should get you there. See https://github.com/idank/bashlex/blob/master/examples/commandsubstitution-remover.py for a similar program that utilizes it to visit command substitutions only.

@BlankCanvasStudio
Copy link
Contributor

Hey, just wanted to shamelessly plug my package here. I wrote a ton of code that easily allows for bash AST manipulation in this way if anyone is interested in the future:
https://github.com/BlankCanvasStudio/bashparser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants