bash aliases not recognized by a bash function: sunspot_rails, jruby, rspec

守給你的承諾、 提交于 2019-12-24 10:39:44

问题


Aliases below for running sunspot in the background work Aliases below for finding and killing those instances work ENV variables for the sunspot ports are accessible But, Functions for running sunspot, processing command, and killing sunspot only work after I source .bashrc outside of the function.

$user_id is set before this sunspot_ports() gets called and prints properly when first logging in rebash is an alias for source ~.bashrc

I have aliases for development and production as well - this is just representative code.

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"
}

I tried sourcing .bashrc inside the function, replacing the alias with the expanded command, and putting the function inside of sunspot_ports() in every combination. The sunspot port prints out correctly when I log in, so I know that this code gets run.

Also, I need to have this as a function in .bashrc instead of somewhere in my jruby code since the jvm doesn't allow forking (otherwise I would just use sunspot-solr start and sunspot-solr end in my spec tests)


回答1:


bash will only resolve the alias if it is already defined at the time that the function invoking is initially sourced. However, in your case, the alias is defined in a function (sunspot_ports), and that function has not been run by the time solr_test is sourced.

You have a couple of options:

  1. Invoke sunspot_ports before defining solr_test
  2. Replace your aliases with functions, e.g.,
sunspot_kill_test()
{
   user -n tcp ${sunspot_test_port} -k
}


来源:https://stackoverflow.com/questions/8418885/bash-aliases-not-recognized-by-a-bash-function-sunspot-rails-jruby-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!