简体   繁体   English

Bash-查找脚本并使用选项执行

[英]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: 为了更具体一点,我在Kali Linux中,我像下面这样运行locate命令:

locate pattern_create 找到pattern_create

which returns: 返回:

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

So I thought about piping this into xargs to run the script like so: 因此,我想到了将其传送到xargs来像这样运行脚本的方法:

locate pattern_create | 找到pattern_create | xargs ruby Xargs红宝石

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 红宝石/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 pattern_create = $(找到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. 我希望通过xargs甚至可能的话甚至更干净的方式来解决这个问题。 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. 我知道可以使用find -exec轻松完成此操作,但是在我不在脚本存储位置的情况下,这将不起作用。

Any help would be awesome, I appreciate everyone's time. 任何帮助都会很棒,我感谢大家的时间。 Thank you. 谢谢。

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. 但是请注意,如果locate返回了多行,那么这可能无法满足您的要求。

This is a dangerous thing to do as you do not know what locate will return and you could end up executing arbitrary scripts. 这是一件危险的事情,因为您不知道locate将返回什么,并且您可能最终执行任意脚本。 I suggest that you use an intermediate script which will protect against the unexpected, such as finding no scripts or finding more than one. 我建议您使用一个中间脚本来防止意外情况,例如找不到脚本或找到多个脚本。

#! /bin/sh
#
if [ $# -eq 0 ]
then
    echo >&2 "Usage: $0 script arguments"
    exit -1
fi
script=$(locate $1)
numfound=$(locate $1 | wc -l)
shift
if [ $numfound -eq 1 ]
then
    # Only run the script if exactly one match is found
    ruby $script $*
elif [ $numfound -eq 0 ]
then
    echo "No matching scripts found" >&2
    exit -1
else
    echo "Too many scripts found - $script" >&2
    exit -1
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM