Using rvm within a bash script on CentOS 6.2

07
2014-07
  • Robert Horn

    I'm trying to write a script that does goes through all the steps for installing and setting up a graylog2 server and web interface on CentOS 6.2. Here is the section that I'm having trouble with:

    curl -L get.rvm.io | bash -s stable
    # Load RVM into a shell session *as a function*
    if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
    
      # First try to load from a user install
      source "$HOME/.rvm/scripts/rvm"
    
    elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
    
      # Then try to load from a root install
      source "/usr/local/rvm/scripts/rvm"
    
    else
    
      printf "ERROR: An RVM installation was not found.\n"
    
    fi
    
    rvm install 1.9.3 --create
    rvm use 1.9.3 --default
    

    So when it tries to load the rvm function into the shell as a function, it won't work, causing it to not install ruby. I've tried other methods of loading the function into the shell, as follows:

    Manually sourcing the scripts file from a root install

    source /usr/local/rvm/scripts/rvm
    

    Manually adding the PATH environment variable to include rvm:

    PATH=$PATH:/usr/local/rvm/bin
    export PATH
    

    Manually calling the path to the rvm command

    /usr/local/rvm/bin/rvm install 1.9.3 --create
    

    Sourcing /etc/profile.d/rvm.sh

    source /etc/profile.d/rvm.sh
    

    Note that if I do close out of the shell and log back in, rvm loads just fine, but I need to be able to do it all from within this script without having to log out and log back in. I'm also doing this from as root. You can look at the script I've written so far in it's entirety here.

    Any help is appreciated.

    EDIT

    After adding set -x to the script, here is the output when it tries to load the function:

    + [[ -s /root/.rvm/scripts/rvm ]]
    + [[ -s /usr/local/rvm/scripts/rvm ]]
    + source /usr/local/rvm/scripts/rvm
    ++ [[ :braceexpand:hashall:interactive-comments:posix:xtrace: =~ :posix: ]]
    ++ return 0
    + rvm install 1.9.3 --create
    ./rvm_test.sh: line 27: rvm: command not found
    + rvm use 1.9.3 --default
    ./rvm_test.sh: line 28: rvm: command not found
    

    EDIT 2

    I looked into the /usr/local/rvm/scripts/rvm file and found this at the very beginning:

    # Do not allow sourcing RVM in `sh` - it's not supported
    # return 0 to exit from sourcing this script without breaking sh
    [[ ":$SHELLOPTS:" =~ ":posix:" ]] && return 0 || true
    

    So I believe this is causing the my problem but I'm not sure. I'm not experienced in bash scripting :\ So the problem I'm guessing is that my shell isn't supported(?) can anybody clarify?

  • Answers
  • Robert Horn

    Answering my own question haha

    Just switching the first line

    #!/bin/sh
    

    to

    #!/bin/bash
    

    in my script fixes the problem I was having. I figured out that the script that was trying to be sourced doesn't support sh.


  • Related Question

    ruby - rvm doesnt work when specifiying the path...?
  • jtzero

    when typing in the path to the rvm it doesnt work

    $which rvm 
    /usr/local/bin/rvm
    $ruby -v
    ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]
    $/usr/local/bin/rvm use 1.8.7
    Using /usr/local/rvm/gems/ruby-1.8.7-p302
    $ruby -v
    ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]
    #now w/o the path
    $rvm use 1.8.7
    Using /usr/local/rvm/gems/ruby-1.8.7-p302
    $ruby -v 
    ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-linux]
    
    $whereis rvm
    rvm: /usr/local/bin/rvm /usr/local/lib/rvm /usr/local/rvm
    
    ....
    /etc/bash.bashrc 
    ....
    if [[ -n "$PS1" ]]; then
    ...
    [[ -s "/usr/local/rvm/scripts/rvm" ]] && . "/usr/local/rvm/scripts/rvm"
    

    anyone seen this?

    UPDATE:

    $type -a rvm
    rvm is a function
    rvm () 
    { 
        local result;
        __rvm_setup;
        case $- in 
            *i*)
                rvm_interactive_flag=1
            ;;
            *)
                rvm_interactive_flag=0
            ;;
        esac;
        export rvm_interactive_flag;
        if [[ -z "${ZSH_VERSION:-""}" ]]; then
            trap '\rm -rf "${rvm_tmp_path:-"$rvm_path/tmp"}/$$" >/dev/null 2>&1' 0 1 2 3 15;
        fi;
        disk_version="$(awk '/:/{printf $NF"."}' "$rvm_path/lib/VERSION.yml")";
        disk_version="${disk_version/%.}";
        if [[ $# -gt 0 ]]; then
            next_token="$1";
            shift;
        else
            next_token="";
        fi;
        if [[ -n "${rvm_version:-""}" && "${rvm_version:-""}" != "${disk_version:-""}" && "reload" != "${next_token:-""}" ]]; then
            printf "\nA RVM version ${disk_version} is installed yet ${rvm_version} is loaded.\n Please do one of the following:\n  * 'rvm reload'\n  * open a new shell\n  * source your shell init scripts";
            return 1;
        fi;
        __rvm_cleanse_variables;
        __rvm_load_rvmrc;
        __rvm_initialize;
        __rvm_parse_args "$@";
        if [[ $? -gt 0 ]]; then
            result=$?;
            return $result;
        fi;
        rvm_action="${rvm_action:-usage}";
        export GEM_HOME GEM_PATH rvm_action rvm_archflags rvm_bin_flag rvm_bin_path rvm_clang_flag rvm_configure_flags rvm_debug_flag rvm_delete_flag rvm_docs_type rvm_dump_environment_flag rvm_error_message rvm_file_name rvm_gemdir_flag rvm_gemset_name rvm_head_flag rvm_install_arguments rvm_install_on_use_flag rvm_interactive_flag rvm_llvm_flag rvm_loaded_flag rvm_make_flags rvm_niceness rvm_only_path_flag rvm_parse_break rvm_patch_names rvm_patch_original_pwd rvm_pretty_print_flag rvm_prior_cc rvm_proxy rvm_quiet_flag rvm_ree_options rvm_reload_flag rvm_remove_flag rvm_ruby_alias rvm_ruby_aliases rvm_ruby_args rvm_ruby_file rvm_ruby_gem_home rvm_ruby_interpreter rvm_ruby_load_path rvm_ruby_make rvm_ruby_make_install rvm_ruby_patch_level rvm_ruby_repo_url rvm_ruby_repo_branch rvm_ruby_require rvm_ruby_string rvm_ruby_strings rvm_ruby_version rvm_script_name rvm_sdk rvm_silent_flag rvm_system_flag rvm_token rvm_trace_flag rvm_use_flag rvm_user_flag rvm_verbose_flag rvm_wrapper_name rvm_version rvm_static_flag;
        case "$rvm_action" in 
            use)
                __rvm_use
            ;;
            srcdir)
                __rvm_source_dir
            ;;
            strings)
                __rvm_strings
            ;;
            version)
                __rvm_version
            ;;
            reset)
                __rvm_reset
            ;;
            update)
                __rvm_update
            ;;
            reboot)
                __rvm_reboot
            ;;
            usage)
                __rvm_usage
            ;;
            benchmark)
                __rvm_benchmark
            ;;
            inspect)
                __rvm_inspect
            ;;
            implode | seppuku)
                __rvm_implode
            ;;
            list)
                "$rvm_path/scripts"/list $rvm_ruby_args
            ;;
            debug)
                "$rvm_path/scripts/info" '' debug
            ;;
            help)
                "$rvm_path/scripts/help" $rvm_ruby_args
            ;;
            env)
                "$rvm_path/scripts/env" "$rvm_ruby_string"
            ;;
            info)
                if [[ $# -gt 0 ]]; then
                    next_token="$1";
                    shift;
                else
                    next_token="";
                fi;
                if [[ "$next_token" = "info" ]]; then
                    shift;
                fi;
                "$rvm_path/scripts/info" $rvm_ruby_args
            ;;
            answer)
                __rvm_Answer_to_the_Ultimate_Question_of_Life_the_Universe_and_Everything;
                result=42
            ;;
            question)
                __rvm_ultimate_question;
                result=42
            ;;
            wrapper)
                "$rvm_path/scripts/wrapper" "$rvm_ruby_string" "$rvm_wrapper_name" $rvm_ruby_args;
                unset rvm_wrapper_name
            ;;
            gemdir | gemhome | gempath)
                "$rvm_path/scripts/gemsets" "$rvm_action"
            ;;
            ruby | gem | rake | exec)
                old_rvm_ruby_string=${rvm_ruby_string:-""};
                unset rvm_ruby_string;
                export rvm_ruby_strings;
                "$rvm_path/scripts/set" "$rvm_action" $rvm_ruby_args;
                result=$?;
                [[ -n "$old_rvm_ruby_string" ]] && rvm_ruby_string=$old_rvm_ruby_string;
                unset old_rvm_ruby_string
            ;;
            cleanup | tools | snapshot | disk-usage | repair | alias | docs | rubygems | migrate | upgrade)
                __rvm_run_script "$rvm_action"
            ;;
            rvmrc)
                eval "__rvm_rvmrc_tools $rvm_ruby_args"
            ;;
            gemset)
                if [[ ${rvm_use_flag:-0} -eq 1 ]]; then
                    __rvm_gemset_select && __rvm_gemset_use;
                else
                    export rvm_ruby_strings;
                    "$rvm_path/scripts/gemsets" $rvm_ruby_args;
                    result=$?;
                    rvm_ruby_strings="";
                    if [[ ${rvm_delete_flag:-0} -eq 1 ]]; then
                        gem_prefix="$(echo "${GEM_HOME:-""}" | \sed 's/'${rvm_gemset_separator:-"@"}'.*$//')";
                        if [[ "${GEM_HOME:-""}" = "${gem_prefix}${rvm_gemset_separator:-"@"}${rvm_gemset_name}" ]]; then
                            rvm_ruby_gem_home="$gem_prefix";
                            GEM_HOME="$rvm_ruby_gem_home";
                            GEM_PATH="$rvm_ruby_gem_home:$rvm_ruby_gem_home${rvm_gemset_separator:-"@"}global";
                            export rvm_ruby_gem_home GEM_HOME GEM_PATH;
                        fi;
                        unset gem_prefix;
                    fi;
                fi
            ;;
            monitor)
                export rvm_ruby_strings rvm_ruby_string;
                "$rvm_path/scripts/monitor"
            ;;
            notes)
                "$rvm_path/scripts/notes"
            ;;
            get)
                "$rvm_path/scripts/get" $rvm_ruby_args
            ;;
            reload)
                rvm_reload_flag=1
            ;;
            tests | specs)
                rvm_action="rake";
                __rvm_do
            ;;
            package)
                "$rvm_path/scripts/package" $rvm_ruby_args
            ;;
            fetch | install | uninstall | remove)
                if [[ -n "$rvm_ruby_strings" ]]; then
                    "$rvm_path/scripts"/manage "$rvm_action" "$rvm_ruby_strings";
                else
                    "$rvm_path/scripts"/manage "$rvm_action";
                fi
            ;;
            error)
                false
            ;;
            *)
                if [[ -n "${rvm_action:-""}" ]]; then
                    "$rvm_path/scripts/log" "fail" "unknown action '$rvm_action'";
                else
                    __rvm_usage;
                fi;
                false
            ;;
        esac;
        result=${result:-$?};
        if [[ ${rvm_reload_flag:-0} -eq 1 ]]; then
            rvm_loaded_flag=0;
            source "$rvm_path/scripts/rvm";
            __rvm_project_rvmrc;
        fi;
        if [[ ${rvm_trace_flag:-0} -eq 1 ]]; then
            rvm_trace_flag=0;
            set +o verbose;
            set +o noclobber;
            set +o nounset;
            set +o xtrace;
            if [[ -z "${ZSH_VERSION:-""}" ]]; then
                set +o errtrace;
            fi;
        fi;
        __rvm_teardown;
        return ${result:-0}
    }
    rvm is /usr/local/bin/rvm
    rvm is /usr/local/bin/rvm
    

  • Related Answers
  • jtzero

    the rvm has to be sourced ... thats why it didnt work