分割字符串仅获得前5个字符 [英] Split the string to get only the first 5 characters

查看:81
本文介绍了分割字符串仅获得前5个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想去那个地方 /var/log/src/ap-kernelmodule-10.001-100

但是看起来我的代码必须处理 ap-kernelmodule-10.002-100,ap-kernelmodule-10.003-101等. 我想用分割字符串到达​​该位置并执行我的命令. 例如:/var/log/src/ap-kernelmodule-10 ./

But looks like my code has to deal with ap-kernelmodule-10.002-100, ap-kernelmodule-10.003-101 etc. I would like to reach the location with a split string and execute my command. eg : /var/log/src/ap-kernelmodule-10./

这个用于Linux机器的ruby脚本,所以我使用了Mixlib :: ShellOut.

This ruby script for a Linux machine, so I used Mixlib:: ShellOut.

begin  
cmd = Mixlib::ShellOut.new("command run here" , :cwd => 
'/var/cache/acpchef/src/ap-kernelmodule-10xxx')
 cmd.run_command
end

推荐答案

您不能同时处于多个cwd中.要为每个与模式匹配的目录运行命令,可以使用Dir#glob:

You can't be in multiple cwds simultaneously. To run the command for each directory that matches the pattern, you can use Dir#glob:

Dir.glob('/var/cache/acpchef/src/ap-kernelmodule-10*').each do |cwd|
  Mixlib::ShellOut.new("command run here", cwd: cwd).run_command
end

这篇关于分割字符串仅获得前5个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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