When I install chruby, I have to edit its /etc/profile.d/chruby.sh and remove the comment delimiters from each line. After the edit, its content is the following.
When I restart my computer, that file does not seem to have effect: chruby is not recognized as command.
I then tried to copy the /usr/share/chruby/chruby.sh file to /etc/profile.d/chruby.sh, thinking that the first line could cause the script to terminate prematurely. This did not work either.
chruby is recognized as command only when I add the source /usr/share/chruby/chruby.sh line to the .bashrc file.
Is there any reason that would cause this code not to work?
CHRUBY_VERSION="0.3.9"
RUBIES=()
for dir in "$PREFIX/opt/rubies" "$HOME/.rubies"; do
[[ -d "$dir" && -n "$(ls -A "$dir")" ]] && RUBIES+=("$dir"/*)
done
unset dir
function chruby_reset()
{
[[ -z "$RUBY_ROOT" ]] && return
PATH=":$PATH:"; PATH="${PATH//:$RUBY_ROOT\/bin:/:}"
if (( $UID != 0 )); then
[[ -n "$GEM_HOME" ]] && PATH="${PATH//:$GEM_HOME\/bin:/:}"
[[ -n "$GEM_ROOT" ]] && PATH="${PATH//:$GEM_ROOT\/bin:/:}"
GEM_PATH=":$GEM_PATH:"
[[ -n "$GEM_HOME" ]] && GEM_PATH="${GEM_PATH//:$GEM_HOME:/:}"
[[ -n "$GEM_ROOT" ]] && GEM_PATH="${GEM_PATH//:$GEM_ROOT:/:}"
GEM_PATH="${GEM_PATH#:}"; GEM_PATH="${GEM_PATH%:}"
unset GEM_ROOT GEM_HOME
[[ -z "$GEM_PATH" ]] && unset GEM_PATH
fi
PATH="${PATH#:}"; PATH="${PATH%:}"
unset RUBY_ROOT RUBY_ENGINE RUBY_VERSION RUBYOPT
hash -r
}
function chruby_use()
{
if [[ ! -x "$1/bin/ruby" ]]; then
echo "chruby: $1/bin/ruby not executable" >&2
return 1
fi
[[ -n "$RUBY_ROOT" ]] && chruby_reset
export RUBY_ROOT="$1"
export RUBYOPT="$2"
export PATH="$RUBY_ROOT/bin:$PATH"
eval "$("$RUBY_ROOT/bin/ruby" - <<EOF
puts "export RUBY_ENGINE=#{defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'};"
puts "export RUBY_VERSION=#{RUBY_VERSION};"
begin; require 'rubygems'; puts "export GEM_ROOT=#{Gem.default_dir.inspect};"; rescue LoadError; end
EOF
)"
if (( $UID != 0 )); then
export GEM_HOME="$HOME/.gem/$RUBY_ENGINE/$RUBY_VERSION"
export GEM_PATH="$GEM_HOME${GEM_ROOT:+:$GEM_ROOT}${GEM_PATH:+:$GEM_PATH}"
export PATH="$GEM_HOME/bin${GEM_ROOT:+:$GEM_ROOT/bin}:$PATH"
fi
}
function chruby()
{
case "$1" in
-h|--help)
echo "usage: chruby [RUBY|VERSION|system] [RUBYOPT...]"
;;
-V|--version)
echo "chruby: $CHRUBY_VERSION"
;;
"")
local dir star
for dir in "${RUBIES[@]}"; do
dir="${dir%%/}"
if [[ "$dir" == "$RUBY_ROOT" ]]; then star="*"
else star=" "
fi
echo " $star ${dir##*/}"
done
;;
system) chruby_reset ;;
*)
local dir match
for dir in "${RUBIES[@]}"; do
dir="${dir%%/}"
case "${dir##*/}" in
"$1") match="$dir" && break ;;
*"$1"*) match="$dir" ;;
esac
done
if [[ -z "$match" ]]; then
echo "chruby: unknown Ruby: $1" >&2
return 1
fi
shift
chruby_use "$match" "$*"
;;
esac
}
I mean, are there restrictions on scripts placed in /etc/profile.d that would explain why that script does not work as expected?
I noticed that even adding source /usr/share/chruby/chruby.sh to the .bashrc file does not work; I have to execute it directly on the terminal window I open. It seems I should copy all the lines from the /usr/share/chruby/chruby.sh file to the .bashrc file.
I agree: It is much better to use a different installer/manager.
yay -S chruby does not install any /usr/local/share/chruby/chruby.sh file. I only find a /usr/share/chruby/chruby.sh file the instructions given after installing says to use.
Please do one of the following or something similar depending of your preferred shell and setup:
For further support/information, please see the project’s homepage!
For your convenience there’s a file at /etc/profile.d/chruby.sh which can used to enable chruby systemwide. You just need to uncomment the lines in the file.
It was a reply to your previous post. I just meant that either the GitHub page is not updated, or it does not apply to EndeavourOS / Arch Linux.
I apologize: My posts did not mean to say you were wrong or to push you to find another solution. I only reported what I found on my computer after running yay -S chruby.
I deleted the /etc/profile.d/chruby.sh file with sudo rm /etc/profile.d/chruby.sh, I added the source /usr/share/chruby/chruby.sh line to the .bashrc file. I rebooted and now chruby works.
I was doing the mistake to verify with which chruby if all worked correctly, as that command returns which: no chruby in (/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl). That is correct: chruby is a function, as type chruby shows.
That is what the output of yay -S chruby suggested. Since that required to just edit a file, and since I was convinced that putting a file in the /etc/profile.d/ directory is better than adding new lines to the .bashrc file, I first tried that.
Then I tried a different way, as I wanted to create a script to install chruby on other computers.