Skip to content
Advertisement

Bash – Locate Script and Execute with Options

I did a good bit of searching and testing on my own, but I can’t seem to find the best way to achieve this goal. I would like to have a bash one liner that will find a script on the machine, execute the script and have the ability to add switches or needed information in my case to execute the script successfully.

To get a little more specific, I am in Kali Linux and I run the locate command like so:

locate pattern_create

which returns:

/usr/share/metasploit-framework/tools/pattern_create.rb

So I thought about piping this into xargs to run the script like so:

locate pattern_create | xargs ruby

but of course I could not specify the options I need to that would run the script successfully, which would be:

ruby /usr/share/metasploit-framework/tools/pattern_create.rb 2700

I came up with a work around, but I feel that it’s somewhat sloppy and this could be done easier, and that’s where I hope I could get any input/feedback.

I found out I can run:

pattern_create=$(locate pattern_create) && ruby $pattern_create 2700

to get exactly what I need, but then I am dealing with environment variables which I would not want a bunch of when doing this often. I was hoping to figure this out with xargs or maybe and even cleaner way if possible. I know this can be done easily with find -exec, but that won’t work in my case where I don’t where the script is stored.

Any help would be awesome, I appreciate everyone’s time. Thank you.

Advertisement

Answer

You can do:

ruby $(locate pattern_create)

But be aware that if there are multiple lines returned by locate, then this may not do what you wanted.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement