简体   繁体   English

在文件夹中执行随机脚本

[英]Execute a random script in a folder

I need to run a random script in a folder.我需要在一个文件夹中运行一个随机脚本。 I can use 'ls -1 /home/sepinto/EML-Samples/scripts/regular |我可以使用 'ls -1 /home/sepinto/EML-Samples/scripts/regular | sort -R |排序-R | head -1' to get a random file name but how can I execute it? head -1' 获取随机文件名,但我该如何执行它?

Thanks谢谢

I can use 'ls -1 /home/sepinto/EML-Samples/scripts/regular |我可以使用 'ls -1 /home/sepinto/EML-Samples/scripts/regular | sort -R |排序-R | head -1' to get a random file name but how can I execute it? head -1' 获取随机文件名,但我该如何执行它?

It depends on the shell you are using.这取决于您使用的 shell。 I will assume your shell is bash or sh and the scripts are already executable.我假设您的 shell 是 bash 或 sh 并且脚本已经可执行。

Then all you need to do is enclose your command line above in backticks .然后你需要做的就是用反引号将上面的命令行括起来。 Everything you type between backticks is evaluated (executed) by the shell and then replaced by the command's output.您在反引号之间键入的所有内容都由 shell 评估(执行),然后由命令的 output 替换。

`ls -1 /home/sepinto/EML-Samples/scripts/regular | sort -R | head -1`

Edit:编辑:
Turns out that the path is stripped if ls -1 is piped into another command.事实证明,如果 ls -1 通过管道传输到另一个命令中,路径将被删除。
A solution is to store the path in a variable so you can use it twice:一种解决方案是将路径存储在变量中,以便您可以使用它两次:

d="/home/sepinto/EML-Samples/scripts/regular"; $d/`ls -1 $d/ | sort -R | head -1`

First, change your ls command to list the files with complete path, then just execute them either directly with backticks or storing them in a variable.首先,更改 ls 命令以列出具有完整路径的文件,然后直接使用反引号执行它们或将它们存储在变量中。

SCRIPTNAME=`ls -1 /home/sepinto/EML-Samples/scripts/regular/*.sh | sort -R | head -1`
echo "Executing $SCRIPTNAME"
"$SCRIPTNAME"

The above assumes that all your files have a.sh ending, change the ls argument if this is not the case.上面假设所有文件都以 a.sh 结尾,如果不是这种情况,请更改ls参数。

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

相关问题 如何运行脚本以执行到特定文件夹的节点 - How to run a script to execute to a node a specific folder 从其文件夹位置而不是调用方文件夹位置执行shell脚本? - Execute shell script from its folder location and not caller folder location? Linux 自定义脚本 - 如何从特定文件夹执行 bash 脚本 - Custom Script for Linux - how to execute bash script from particular folder 从Windows资源管理器中的共享文件夹执行bash脚本 - execute bash script from shared folder in windows explorer 如何查看文件夹中的新文件,然后将新文件作为 $1 执行脚本 - how to watch a folder for a new file and then execute a script with the new file as $1 Linux:每天午夜执行脚本,并删除除最近20个文件夹外的所有文件 - Linux: Execute script every day at midnight and remove all files in folder except latest 20 如何使用 shell 脚本执行命令并将文件存储在文件夹中? - How can I execute a command and store the files in a folder using a shell script? 如何编写脚本以从父目录转到最深的文件夹目录以执行命令? - How to script to go to the deepest folder directory from parent directory to execute a command? Yocto:在我的源存储库环境中执行 shell 脚本(在下载文件夹中?) - Yocto:Execute shell script inside my source repository environment (inside downloads folder ?) 循环执行命令的脚本 - Script to execute a command in a loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM