What's wrong with my bash array? - bash

Can anyone tell me why this bash script works if I cut and paste it to the terminal but throws "server_prep.sh: 7: Syntax error: "(" unexpected" when launched using $ sudo sh server_prep.sh ?
#!/bin/sh
#Packages
apt-get -y install ssh libsqlite3-dev ruby-full mercurial
#Gems
required_gems = ( rake rails sqlite3-ruby )
#Set up directories
[ ! -d /var/www ] && mkdir /var/www
[ ! -d /var/www/apps ] && mkdir /var/www/apps
#install gems manually
if ! which gem >/dev/null; then
wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz
tar xvfz rubygems-1.3.5.tgz
ruby rubygems-1.3.5/setup.rb
ln -s /usr/bin/gem1.8 /usr/bin/gem
gem update --system
#Tidy Up
rm -rf rubygems-1.3.5.tgz rubygems-1.3.5
fi
#Install required gems
for required_gem in "${required_gems[#]}"
do
if ! gem list | grep $required_gem >/dev/null; then
gem install $required_gems
fi
done
Thanks in advance!

Are you on ubuntu?
Then you should change the #!-line at the top to read #!/bin/bash because /bin/sh is a very limited shell.
This would explain why works in the terminal (where the shell is bash) but not as a shell script (which is run by /bin/sh).
They changed this a couple of releases ago for performance reasons - most people don't need full bash functionality for shell script, and this limited shell is much faster at startup.
Edit: I just noticed that you don't even have to use an array since you convert it to a space separated string in the for loop anyway. Just remove the parenthesis in the assignment and put quotes around it instead (and also remove the spaces around the equal sign, as hacker suggested)

Try
required_gems=( rake rails sqlite3-ruby )
instead (note the lack of spaces around '=').

Related

rvm: command not found MAC OX

Really, I don't know what happened. Excuse me if this question is so NOOB, but I can't find the solution for this problem.
-bash: rvm: command not found
I tried this
curl -L https://get.rvm.io | bash -s -- --version latest
but still nothing I need to see the ruby version for use the simplecov because it not works with older version from 1.9
It might because the terminal not having rvm shell configuration loaded.
Try following from your terminal:
$ source ~/.rvm/scripts/rvm
then
$ type rvm | head -n 1
If the output is:
rvm is a function
You may need to add "source ~/.rvm/scripts/rvm" to your ~/.bash_profile file
you need to read all the texts that are displayed when you install RVM:
rm -rf ~/.rvm
curl -L https://get.rvm.io | bash -s stable
after you run sudo curl -L https://get.rvm.io | bash -s stable --ruby
you need to close the terminal ,then open again!
This worked for me:
rm -rf ~/.rvm
curl -L https://get.rvm.io | bash -s stable
source /etc/profile worked for me.
For a long-term solution, you should add this to your ~/.profile file:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
to simply load rvm into a single terminal, use
source "$HOME/.rvm/scripts/rvm"
supposedly this call is more cross-platform:
. "$HOME/.rvm/scripts/rvm"
As suggested by #mpapis, Do this first
$ rm -rf ~/.rvm
$ curl -L https://get.rvm.io | bash -s stable
Then, as suggested by #peterpengnz, do this and you should be fine with RVM cmd issues
$ source ~/.rvm/scripts/rvm
Close and restart terminal after installing RVM — gets me EVERY TIME.
To start using RVM, you'll need to enter source /Users/yourusername/.rvm/scripts/rvm into your terminal (of course, insert your real username in place of yourusername).
Close and reopen your terminal. Sometimes, the changes made during the installation of rvm are not immediately picked up by your terminal, so reopening it should resolve the problem.

Auto-install packages from inside makefile

Goal: when the user types 'make packages', automatically search for the package libx11-dev (required for my program to compile) and, if not found, install it. Here's a stripped-down version of my makefile:
PACKAGES = $(shell if [ -z $(dpkg -l | grep libx11-dev) ]; then sudo apt-get install libx11-dev; fi)
[other definitions and targets]
packages: $(PACKAGES)
When I type 'make packages', I'm prompted for the super-user password. If entered correctly, it then hangs indefinitely.
Is what I'm trying to do even possible from within the makefile? If so, how?
Thanks so much.
The problem is that the shell function acts like backticks in the shell: it takes the output to stdout and returns it as the value of the function. So, apt-get is not hanging, it's waiting for you to enter a response to some question. But you cannot see the question because make has taken the output.
The way you're doing this is not going to work. Why are you using shell instead of just writing it as a rule?
packages:
[ -z `dpkg -l | grep libx11-dev` ] && sudo apt-get install libx11-dev
.PHONY: packages
I figured out a better way, which avoids the problem of having unexpected arguments to the if statement:
if ! dpkg -l | grep libx11-dev -c >>/dev/null; then sudo apt-get install libx11-dev; fi
The -c flag on grep makes it return the number of lines in dpkg -l which contain the string libx11-dev, which will either be 0 (if uninstalled) or 1 (if installed), allowing
dpkg -l | grep libx11-dev -c
to be treated like an ordinary boolean variable.

RVM installation fails

I followed the instructions from the RVM homepage (https://rvm.beginrescueend.com/rvm/install/)
I typed the following:
sudo bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )
the script executed and didn't seem to give any errors.
the same without sudo gave an error:
Warning: Failed to create the file
Warning: /usr/share/ruby-rvm/archives/wayneeseguin-rvm-stable.tgz: Permission
Warning: denied
0 792k 0 3908 0 0 2257 0 0:05:59 0:00:01 0:05:58 2257
curl: (23) Failed writing body (0 != 3908)
Could not download 'https://github.com/wayneeseguin/rvm/tarball/stable'.
curl returned status '23'.
I also put this in my ~/.bashrc
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
what I get is:
~$ type rvm | head -1
bash: type: rvm: not found
~$ source "/usr/local/rvm/scripts/rvm"
bash: /usr/local/rvm/scripts/rvm: No such file or directory
~$ source "$HOME/.rvm/scripts/rvm"
bash: /home/anonym/.rvm/scripts/rvm: No such file or directory
How can this be fixed?
PS
I'm using Ubuntu 11.10
Sudo problems. This worked for me to install rvm. Just do:
curl -L https://get.rvm.io | sudo bash -s stable --ruby
nano ~/.bashrc
at the bottom of the file add these line
unset rvm_path
unset GEM_HOME
Then run
curl -L https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc
rvm install 2.1.3
rvm use 2.1.3 --default
ruby -v
Can you try this:
$ curl -s raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer | bash -s stable
or
$ curl -s raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer | sudo bash -s stable
depending on your permissions. That should help. It helped me! :)
I'm sure there's a more elegant way to fix this, but I ran into the same issue and was kinda in a hurry, so I went for a quick and dirty workaround:
$ curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer > foo.sh
$ chmod 755 foo.sh
Then edit line 162, and add -k to the curl command:
$ vim foo.sh
162 if curl -Lk https://github.com/${_repo}/rvm/tarball/${_branch} -o ${rvm_archives_path}/${_repo}-rvm-${_branch}.tgz
Then run the script:
$ ./foo.sh --branch stable
Like I said, not ideal, but it got me where I needed to be quickly.
If anyone has this problem in the future in ubuntu I was getting this error because of an old package that still had config stuff hanging around.
Try running
sudo apt-get --purge remove ruby-rvm
That should take care of the permission error and let you install RVM as a normal user under $HOME/.rvm
Put this in your .bashrc instead (without echo)
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
And restart your terminal.
You might have RVM installed under the wrong user. RVM won't let you install if it's installed under another user.
You can uninstall it from the other user with rvm implode.
Log over to the other user and RVM will install correctly!
https://rvm.io/rvm/security#ipv6-issues
You can forbid gpg's internal dirmngr from using IPv6 by add the following line to ~/.gnupg/dirmngr.conf:

installing rvm on lion

How to install RVM on lion, I am trying this command in terminal
curl -s https://rvm.beginrescueend.com/install/rvm
After this command executed it gives me a lot of text in terminal, the text is ending with this statement
chmod +x ./scripts/install
# Now we run the RVM installer.
./scripts/install ${flags[*]} --prefix "$rvm_prefix" --path "$rvm_path"
After it finish executing I tried rvm command but its giving me error command not found
Are you running only curl -s https://rvm.beginrescueend.com/install/rvm in terminal? This only displays the content of the script.
If you want to install it you need to run something like this(assuming you are using bash):
bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
Copied from:
http://ruby.about.com/od/rubyversionmanager/ss/Installing-Ruby-On-Snow-Leopard-With-Rvm_6.htm
Now that the RVM scripts are installed, you need to load them every
time your start a new Bash session. This is achieved by adding a line
to the end of your ~/.bash_profile file. You can add this line with
your favorite text editor, or by using the cat command. Just hit
Ctrl-D after you've typed the line and it will write that line to the
file and return you to the bash prompt.
$ cat >>~/.bash_profile
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

How do you remove the documentation installed by gem install?

I know it's possible to install a gem without the documentation, but unfortunately, I didn't for the first three months I used ruby. In that time, I managed to install a significant amount of gems, but not once since I started using ruby have I used the documentation on my computer. I always look to docs on the internet.
What is the best way to safely remove the documentation from my computer? Also, is there a way to configure ruby to not install documentation by default?
run this command:
rm -r "$(gem env gemdir)"/doc/*
on windows if you use cygwin
Gem documentation is installed in [RUBY]/lib/ruby/gems/doc, where [RUBY] is the directory into which you installed Ruby (C:/ruby on my machine).
I don't see why you shouldn't just delete the folders representing the gems for which don't don't need documentation: you can always regenerate it.
I understand there is a .gemrc file that may be used.
install: --no-rdoc --no-ri
update: --no-rdoc --no-ri
puts the two lines in it.
I believe you put the .gemrc file in your $Home directory.
Copy & paste any of these into a Bourne-compatible shell:
Remove existing docs for locally-installed gems, current-user only
(exec 1>&2; DIR="$(gem env gemdir)"; \
DOCDIR="$DIR"/doc; \
if [ -d "$DOCDIR" ]; then \
echo "Contents of '$DOCDIR': "; ls "$DOCDIR"/; echo ''; \
read -p "Do you really want to remove contents of RubyGems doc dir '$DOCDIR' ? [Yn] " ANS; \
if [ "$ANS" = y ] || [ "$ANS" = Y ]; then \
rm -rf "$DOCDIR"/*; \
fi; \
fi)
Remove docs for all globally installed system gems
(exec 1>&2; for DIR in $(gem env path | tr ':' '\n'); do \
DOCDIR="$DIR"/doc; \
if [ -d "$DOCDIR" ]; then \
echo "Contents of '$DOCDIR': "; ls "$DOCDIR"/; echo ''; \
echo '!! Possibly removes system-provided gem docs, review carefully before continuing ("n" if unsure or Ctrl-C to abort completely) !!'; \
read -p "Do you really want to remove RubyGems doc dir '$DOCDIR' ? [Yn] " ANS; \
if [ "$ANS" = y ] || [ "$ANS" = Y ]; then \
sudo rm -rf "$DOCDIR"/*; \
fi; \
fi; \
done)
Prevent current user from generating gems docs from now onwards
with modern RubyGems:
(X='gem: --no-document'; \
touch ~/.gemrc && \
grep -q "^$X$" ~/.gemrc || echo "$X" >> ~/.gemrc)
with old RubyGems: X='gem: --no-ri --no-rdoc' ...
I can't answer the first part of your problem (I have the same issue myself) but I have managed to not install documentation by default.
You can specify some default command line options in the gem config file, you can specify not to generate documentation (something like --no-rdoc and --no-ri).
Sam
This command worked for me in removing documentation installed by install.
rm -r "$(gem env gemdir)"/doc/*
Here's another way to go about it;
To locate the folder where gems are installed on your system, simply run the following on terminal or shell
gem env home
This will display an output like
/home/username/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0
You can then locate the docs folder and delete all the documentations that you find there for each gem in a ruby version
I also want to add this too to the answers provided,
if want to prevent gems from generating documentation during installation, then turn off local documentation generation by creating a file called ~/.gemrc which contains a configuration setting to turn off this feature:
Simply run this code in your terminal or shell
printf "install: --no-rdoc --no-ri\nupdate: --no-rdoc --no-ri\n" >> ~/.gemrc
OR
echo "gem: --no-document" > ~/.gemrc
This will prevent gems from generating documentation during installation, saving you the stress of deleting/removing gem documentations on your system later.
That's all
I hope this helps.

Resources