【问题标题】:bash aliases not recognized by a bash function: sunspot_rails, jruby, rspecbash 函数无法识别的 bash 别名:sunspot_rails、jruby、rspec
【发布时间】:2011-12-07 16:32:29
【问题描述】:

下面的别名用于在后台运行太阳黑子 以下用于查找和杀死这些实例的别名有效 太阳黑子端口的 ENV 变量可访问 但, 运行 sunspot、处理命令和杀死 sunspot 的函数只有在我在函数之外获取 .bashrc 后才能工作。

$user_id 在此之前设置 sunspot_ports() 在首次登录时被调用并正确打印 rebash 是 source ~.bashrc 的别名

我也有开发和生产的别名 - 这只是代表代码。

sunspot_ports ()
{  
  #alias sunspot_run_test to the user's port
  sunspot_test_port=$(($user_id +5300))
  echo "Your sunspot test port: $sunspot_test_port"
  alias sunspot_run_test="RAILS_ENV=test sunspot-solr run -p${sunspot_test_port} &"
  alias sunspot_kill_test="fuser -n tcp ${sunspot_test_port} -k"

  export sunspot_production_port sunspot_development_port sunspot_test_port
}

solr_test()
{
  #only makes the aliases be recognized when it is outside the function
  #rebash
  #aliases not recognized without a rebash prior to the function 
  sunspot_run_test
  #commands not recognized even with rebash
  #"RAILS_ENV=test sunspot-solr run -p${sunspot_test_port} &"
  sleep 10;
  "$@";
  sunspot_kill_test;
  #commands not recognized even with rebash
  #"fuser -n tcp ${sunspot_test_port} -k"
}

我尝试在函数中获取 .bashrc,用扩展命令替换别名,并将函数放在 sunspot_ports() 中的每个组合中。当我登录时,太阳黑子端口打印正确,所以我知道这段代码会运行。

另外,我需要在 .bashrc 中将其作为一个函数,而不是在我的 jruby 代码中的某个位置,因为 jvm 不允许分叉(否则我只会在我的规范测试中使用 sunspot-solr start 和 sunspot-solr end )

【问题讨论】:

    标签: jrubyonrails sunspot-rails bash bash-function


    【解决方案1】:

    bash 将仅在函数调用的初始来源时已定义别名的情况下解析别名。但是,在您的情况下,别名是在函数 (sunspot_ports) 中定义的,并且在获取 solr_test 时该函数尚未运行。

    你有几个选择:

    1. 在定义solr_test之前调用sunspot_ports
    2. 用函数替换别名,例如,
    sunspot_kill_test() { 用户 -n tcp ${sunspot_test_port} -k }

    【讨论】:

    • 这很有意义(更重要的是,有效)。非常感谢。我让它暂时使用同事推荐的 hack $(sunspot_run_test) & .... $(sunspot_kill_test);在 shell 中运行它,但它让我发疯,不知道为什么资源 bash 以前有效。
    猜你喜欢
    • 2010-10-05
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2016-03-04
    • 2022-11-22
    • 1970-01-01
    相关资源
    最近更新 更多