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

307 views Asked by At

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

There are 1 answers

1
Ray On BEST ANSWER

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
}