BASH script doesn't follow the execution rules

Forum rules
Before you post please read how to get help
windglider
Level 1
Level 1
Posts: 1
Joined: Sun Jan 15, 2012 1:44 am

BASH script doesn't follow the execution rules

Postby windglider » Sun Jan 15, 2012 1:55 am

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

User avatar
Garvan
Level 4
Level 4
Posts: 296
Joined: Sun May 29, 2011 3:26 am
Location: Lao PDR
Contact:

Re: BASH script doesn't follow the execution rules

Postby Garvan » Sun Jan 15, 2012 2:24 am

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
Notebook: HP 14 Notebook PC 14-r003TX, i5-4210U @ 1.7 GHz, 8GB RAM, Linux Mint 17.2 (Dual boot with Win 10)
Notebook: DELL latitude E5520, i5-2520M @ 2.50 GHz, 4GB RAM, Linux Mint 17.2

User avatar
xenopeek
Level 23
Level 23
Posts: 19030
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: BASH script doesn't follow the execution rules

Postby xenopeek » Sun Jan 15, 2012 4:47 am

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
Image

User avatar
Garvan
Level 4
Level 4
Posts: 296
Joined: Sun May 29, 2011 3:26 am
Location: Lao PDR
Contact:

Re: BASH script doesn't follow the execution rules

Postby Garvan » Sun Jan 15, 2012 5:51 am

Vincent Vermeulen 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
Notebook: HP 14 Notebook PC 14-r003TX, i5-4210U @ 1.7 GHz, 8GB RAM, Linux Mint 17.2 (Dual boot with Win 10)
Notebook: DELL latitude E5520, i5-2520M @ 2.50 GHz, 4GB RAM, Linux Mint 17.2


Return to “Scripts & Bash”

Who is online

Users browsing this forum: cypher_bg and 3 guests