1

下面用于在后台运行 sunspot 的别名 下面用于查找和终止这些实例的别名可以访问 sunspot 端口的 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 )

4

1 回答 1

1

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

你有几个选择:

  1. sunspot_ports在定义之前调用solr_test
  2. 用函数替换你的别名,例如,
sunspot_kill_test()
{
   用户 -n tcp ${sunspot_test_port} -k
}
于 2011-12-08T04:45:55.140 回答