Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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
    Code:
    #!/bin/bash
    set -e
    First I tried with this solution.
    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
    source /etc/environment
    Error, source not found

    The second solution was 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
    This one did't give an error, but if i try this:
    Code:
    ruby -v
    got an error, so the evironment did'nt worked.

    If i use both commands
    Code:
    source /etc/environment
    Code:
    . /etc/environment
    in the terminal, without a bash script, it works, so if i do
    Code:
    ruby -v
    it works and showed the right version.

    My question is, how can I solve this with a bash script. Please help me

  2. #2
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    I think your problem is the asterisk in this line of code:
    Code:
    echo 'PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/ruby/bin"' > */etc/environment
    Have you tried
    Code:
    ls -l /etc/environment
    If the error message is
    Code:
    ls: cannot access /etc/enviromnent: No such file or directory
    then the file was never created it before you can use the source command on it.

  3. #3
    Just 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.

    Code:
    $ sudo nano /etc/environment
    change this line:
    Code:
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
    into this line:
    Code:
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/ruby/bin"
    after that I type the next two commands
    Code:
    $ source /etc/environment
    $ ruby -v
    The last command gave some output.

    The case is, it must be automatic, because it is a part of an instalation (for Ruby). So I make a bash script.

    Code:
    sudo chmod 777 /etc
    (must do this for the permission when I want to edit a file.
    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
    The last line of the script is:
    Code:
    source /etc/environment
    this one gave the error: Source not found
    change it in
    Code:
    . /etc/environment
    it gave no errors, but if I enter in command line (after that the script is done)
    Code:
    $ ruby -v
    got the error that ruby wasn't found
    (after that I try to type)
    Code:
     $ source /etc/environment
    $ ruby -v
    And it work.

    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)

  4. #4
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    In my opinion, it would be better to simply create your own "/usr/bin/ruby" script which contains this code:
    Code:
    #!/bin/bash
    # "/usr/bin/ruby"
    
    export PATH="$PATH:/usr/local/ruby/bin"
    exec ruby -v $@
    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.

    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:
    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
    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.

    Observe the debug output when you execute ". my-start-ruby.sh". You should see some line of output that looks like this
    Code:
    ++ '[' -f "/etc/environment" ']'
    ++ ruby -v
    Last edited by ramin.honary; 02-28-2011 at 12:40 PM. Reason: corrected grammatical error

  5. #5
    Just 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.

  6. #6
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    Did it work then?
    Quote Originally Posted by MarkRaats View Post
    OUTPUT:
    Offcourse all tar, configure, make lines, but the important line for you is:
    Code:
    + grep -v PATH= /etc/environment
    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:
    Code:
    ++ '[ -f "/etc/environment" ']'
    This will tell you whether or not the file exists.

    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

  7. #7
    Just 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" ']'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •