Results 1 to 7 of 7
Hi Guys,
I want to edit the environment with a bash script, a part of my script is showed in the post. Its not everything, but the rest is working ...
- 02-25-2011 #1Just Joined!
- Join Date
- Dec 2010
- Posts
- 16
Bash Script : source: not found
Hi Guys,
I want to edit the environment with a bash script, a part of my script is showed in the post. Its not everything, but the rest is working fine. At the begin of my script I've the follow line
First I tried with this solution.Code:#!/bin/bash set -e
Error, source not foundCode:sudo chmod 777 /etc sudo rm /etc/environment echo 'PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/ruby/bin"' > */etc/environment sudo chmod 755 /etc source /etc/environment
The second solution was this:
This one did't give an error, but if i try this:Code:sudo chmod 777 /etc sudo rm /etc/environment echo 'PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/ruby/bin"' > */etc/environment sudo chmod 755 /etc . /etc/environment
got an error, so the evironment did'nt worked.Code:ruby -v
If i use both commands
Code:source /etc/environment
in the terminal, without a bash script, it works, so if i doCode:. /etc/environment
it works and showed the right version.Code:ruby -v
My question is, how can I solve this with a bash script. Please help me
- 02-26-2011 #2Linux Newbie
- Join Date
- Nov 2008
- Location
- Tokyo, Japan
- Posts
- 243
I think your problem is the asterisk in this line of code:
Have you triedCode:echo 'PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/ruby/bin"' > */etc/environment
If the error message isCode:ls -l /etc/environment
then the file was never created it before you can use the source command on it.Code:ls: cannot access /etc/enviromnent: No such file or directory
- 02-28-2011 #3Just Joined!
- Join Date
- Dec 2010
- Posts
- 16
Thanks for reaction, but the asterisk was not the problem. I'll try to explain it again and I hope you all will understand it better.
First I did it manually, with the command line.
change this line:Code:$ sudo nano /etc/environment
into this line:Code:PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
after that I type the next two commandsCode:PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/ruby/bin"
The last command gave some output.Code:$ source /etc/environment $ ruby -v
The case is, it must be automatic, because it is a part of an instalation (for Ruby). So I make a bash script.
(must do this for the permission when I want to edit a file.Code:sudo chmod 777 /etc
The last line of the script is:Code:sudo rm /etc/environment echo 'PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/ruby/bin"' > /etc/environment sudo chmod 755 /etc
this one gave the error: Source not foundCode:source /etc/environment
change it in
it gave no errors, but if I enter in command line (after that the script is done)Code:. /etc/environment
got the error that ruby wasn't foundCode:$ ruby -v
(after that I try to type)
And it work.Code:$ source /etc/environment $ ruby -v
So the problem is with the 'source' command in the bash script (or the . command in the bash script, because outside the bashscript it work fine)
- 02-28-2011 #4Linux Newbie
- Join Date
- Nov 2008
- Location
- Tokyo, Japan
- Posts
- 243
In my opinion, it would be better to simply create your own "/usr/bin/ruby" script which contains this code:
The "export" directive is most important here: it will make sure that ruby, and any applications executed by "ruby" can inherit your PATH environment variable from bash. If you do not use "export" then PATH will revert back to the default value for sub-processes like "ruby". The "exec" directive will remove the "/usr/bin/ruby" bash script and bash interpreter program from memory and replace it with the "ruby -v" command interpreter. The "$@" dumps all command line arguments given to the bash script into the "ruby" command line arguments.Code:#!/bin/bash # "/usr/bin/ruby" export PATH="$PATH:/usr/local/ruby/bin" exec ruby -v $@
If you absolutely must modify "/etc/environment", I offer some friendly criticism: it is NOT secure coding practice to chmod "/etc" -- even if it is for just a very brief moment in a script.
You can try executing the script file in debugging mode with "set -x". I would suggest you try this code:The important thing is to use bash debugging mode. If you do this, it will tell you which command you execute and whether or not an "if" statement evaluates to true.Code:#!/bin/bash # "my-start-ruby.sh" # 1. Turn on BASH's debugging mode set -x # 2. Copy all lines of "/etc/environment", except for lines # that contain the substring "PATH=", into a temporary file. grep -v 'PATH=' /etc/environment >./temporary.sh # 3. Append your new path variable to the temporary script. echo "PATH=$PATH:/usr/local/ruby/bin" >>temporary.sh # 4. Overwrite /etc/environment with the temporary file sudo cp -f ./temporary.sh /etc/environment && rm ./temporary.sh # 5. Check if the "/etc/environment" still exists and # source-execute. if [ -f "/etc/environment" ] then source /etc/environment export PATH ruby -v $@ else echo "File still not found." ls -l /etc/environment fi # 6. Turn off debugging so if you execute this script using # the "source" directive, bash will stop printing debugging # output after the script completes. set +x
Observe the debug output when you execute ". my-start-ruby.sh". You should see some line of output that looks like thisCode:++ '[' -f "/etc/environment" ']' ++ ruby -v
Last edited by ramin.honary; 02-28-2011 at 12:40 PM. Reason: corrected grammatical error
- 03-01-2011 #5Just Joined!
- Join Date
- Dec 2010
- Posts
- 16
Hi,
I tried your code:
My script looks like this:
Code:#!/bin/sh set -e ############ SOME OTHER FUNTIONS ############### f_RUBY(){ echo '####################' echo '### INSTALL RUBY ###' echo '####################' sleep 1 sudo apt-get install gcc g++ build-essential libssl-dev libreadline5-dev zlib1g-dev linux-headers-generic -y wget ftp://ftp.ruby-lang.org//pub/ruby/1.9/ruby-1.9.2-p0.tar.gz tar -xvzf ruby-1.9.2-p0.tar.gz cd ruby-1.9.2-p0/ ./configure --prefix=/usr/local/ruby make && sudo make install set -x grep -v 'PATH=' /etc/environment >./temporary.sh echo "PATH=$PATH:/usr/local/ruby/bin" >>temporary.sh sudo cp -f ./temporary.sh /etc/environment && rm ./temporary.sh if [ -f "/etc/environment" ] then source /etc/environment export PATH ruby -v $@ else echo "File still not found." ls -l /etc/environment fi set +x #echo '####################' #echo '### INSTALL GEMS ###' #echo '####################' #sleep 1 #sudo ln -s /usr/local/ruby/bin/ruby /usr/local/bin/ruby #sudo ln -s /usr/local/ruby/bin/gem /usr/bin/gem #sudo gem install tzinfo builder memcache-client rack rack-test erubis mail text-format #bundler thor i18n capistrano capistrano capistrano-ext #sudo gem install rack-mount --version=0.4.0 #sudo gem install rails --version 3.0.0 #echo '#####################' #echo '### INSTALL MYSQL ###' #echo '#####################' #sleep 1 #sudo apt-get install libmysql-ruby libmysqlclient-dev -y #sudo gem install mysql -- --with-mysql-config=/usr/bin/mysql_config echo '###############################' echo '### INSTALL RUBY SUCCESFULL ###' echo '###############################' sleep 1 } ########### SOME OTHER CODE (FOR MENU) ############
OUTPUT:
Offcourse all tar, configure, make lines, but the important line for you is:
Code:+ grep -v PATH= /etc/environment
Last edited by MarkRaats; 03-01-2011 at 07:59 AM.
- 03-02-2011 #6Linux Newbie
- Join Date
- Nov 2008
- Location
- Tokyo, Japan
- Posts
- 243
Did it work then?
The lines that begin with "++" are bash debugging output. So you should observe all lines that start with "++".
There should be more debug output than just the result of "grep". Of course I don't need to see the result of "tar" or "make", but what you should look carefully at is the lines of output after:This will tell you whether or not the file exists.Code:++ '[ -f "/etc/environment" ']'
You can save the debug output of your script by redirecting "stderr" to a file, then grep the file for lines starting with "++":Code:./my-script.sh 2>debug grep -e '^[+]\+' debug >debug.out && rm debug
- 03-02-2011 #7Just Joined!
- Join Date
- Dec 2010
- Posts
- 16
That was the only output expecting the lines that are from the make/configure commands.
So there wasn't a line with
Code:++ '[ -f "/etc/environment" ']'


Reply With Quote
