简体   繁体   English

从bash脚本运行带有文件路径args的ruby脚本

[英]running ruby script with file pather args from a bash script

I am using a bash script to launch a ruby script. 我正在使用bash脚本启动ruby脚本。 this works very well if you are in the sir of the script but if you are not in that dir the script acts funny because it is not on the file path. 如果您位于脚本的目录中,则此方法效果很好,但如果您不在该目录中,则该脚本的行为很有趣,因为它不在文件路径中。

#!/bin/sh
jruby -Ilib script.rb $@

I added this to the script. 我将此添加到脚本中。

mypath=`realpath $0`

cd `dirname $mypath`/..

It makes the script run from any place I want. 它可以使脚本从我想要的任何位置运行。 the problem is that its arguments do not get their relative path names changed. 问题在于其参数未更改其相对路径名。

I am using trollop for my input parsing. 我正在使用trollop进行输入解析。

My question is: How can I change the input of the path in the input so it will be the correct relative path when I push the args to the ruby script. 我的问题是:如何在输入中更改路径的输入,以便在将args推送到ruby脚本时它将是正确的相对路径。 I would be happy with a ruby fix to this problem. 我很乐意用红宝石解决这个问题。

This uses bash specifically. 这专门使用bash。 It assembles an array of the arguments, calling realpath for each. 它组装一个参数数组,为每个参数调用realpath

#!/bin/bash
mypath=$(realpath $0)
args=()
for arg; do
  args+=( "$(realpath "$arg")" )
done
cd "$(dirname "$mypath")"/..

jruby -Ilib script.rb "${args[@]}"

I've liberally sprinkled double quotes throughout the script to handle files and paths that contain spaces. 我在整个脚本中使用了双引号,以处理包含空格的文件和路径。

I ended up doing it this way, 我最终还是这样

I found using environmental variables worked the best 我发现使用环境变量效果最好

#!/bin/bash

if [ "$EN_VARE"="" ]
then
 EN_VARE="."
fi

jruby -I$EN_VARE/lib $EN_VARE/lib/main_file.rb $@

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

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