Page 1 of 1

BASH script doesn't follow the execution rules

Posted: Sun Jan 15, 2012 1:55 am
by windglider
I am new to Mint 12 (love the distro) but not new to Debian linux

I am trying to get a simple script to run on my new Mint 12 installation. The typical "Hello world" script doesn't quite work:

#!/bin/bash
# My first script
echo "Hello World!"


This will execute in the given script directory (such as local "bin") if I type

./my_script

under terminal
but not if I type:

my_script

even though chmod 755 has been done to the file, and even though .profiles says that "bin" is a valid directory for BASH script to execute.

So, I am rather stumped at the moment. I am just trying to do "normal" things here. I want to execute any script where the script file is placed in the user's private bin directory. Normally this simply runs.

Any ideas? This is the kind of thing that once you know the answer, you go "Aha!"... so simple to fix.

Thanks... -B

Re: BASH script doesn't follow the execution rules

Posted: Sun Jan 15, 2012 2:24 am
by Garvan
An idea ...
In Mint 12 there is no bin directory in home, so I assume you added it. Unless you reboot, this directory will not be in the path. If you are using a virtual machine, you may not have rebooted yet?

Garvan

Re: BASH script doesn't follow the execution rules

Posted: Sun Jan 15, 2012 4:47 am
by xenopeek
From the top of the default .profile:
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
I.e., it is only executed when you login through a shell (from the console, or through SSH), but not if you open a new terminal on your graphical environment or if you start a new bash instance...

To do that, you have to put the following in your ~/.bashrc, which is executed each time you open a new terminal on your graphical environment or if you start a new bash instance--though this doesn't get run for login shells!

Code: Select all

#!/bin/bash
PATH="$PATH:$HOME/bin
export PATH
If you also want this path set from login shells, add the following to you ~/.bash_profile:

Code: Select all

#!/bin/bash
if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi
All the intricacies of the different .profile, .bashrc and .bash_profile files are explained here: http://stefaanlippens.net/bashrc_and_others

Re: BASH script doesn't follow the execution rules

Posted: Sun Jan 15, 2012 5:51 am
by Garvan
xenopeek wrote:From the top of the default .profile:
<snip>
I.e., it is only executed when you login through a shell (from the console, or through SSH), but not if you open a new terminal on your graphical environment or if you start a new bash instance...
</snip>
I think Vincent (and Stefaan on the linked page) may be misunderstanding what login through a shell means. I know from experience that with my default Mint 11 setup, the file ~/.profile is run once every lonin. The files ~/.bash_profile or ~/.bash_login do not exist on my system.

An easy way to test is to put an echo in the file ~/.profile.

Code: Select all

echo debug > $HOME/debug.txt
and then check the date and time of the file debug.txt created in your home directory.

You could also check the contents of your path statement in a terminal.

Code: Select all

echo $PATH 
Garvan