BASH script doesn't follow the execution rules

Forum rules
Before you post please read this

BASH script doesn't follow the execution rules

Postby windglider on 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
windglider
Level 1
Level 1
 
Posts: 1
Joined: Sun Jan 15, 2012 1:44 am

Linux Mint is funded by ads and donations.
 

Re: BASH script doesn't follow the execution rules

Postby Garvan on 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
Desktop: Dell, Intel Core 2 Duo E7500 @ 2.93Ghz, 2GiB RAM, Linux Mint 13 Maya.
Net-book: SAMSUNG N148-Plus, 2x Intel Atom CPU N450 @1.66Ghz, 1GB RAM, lubuntu 12.04.
User avatar
Garvan
Level 4
Level 4
 
Posts: 273
Joined: Sun May 29, 2011 3:26 am
Location: Cambodia

Re: BASH script doesn't follow the execution rules

Postby xenopeek on 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
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14889
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: BASH script doesn't follow the execution rules

Postby Garvan on 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
Desktop: Dell, Intel Core 2 Duo E7500 @ 2.93Ghz, 2GiB RAM, Linux Mint 13 Maya.
Net-book: SAMSUNG N148-Plus, 2x Intel Atom CPU N450 @1.66Ghz, 1GB RAM, lubuntu 12.04.
User avatar
Garvan
Level 4
Level 4
 
Posts: 273
Joined: Sun May 29, 2011 3:26 am
Location: Cambodia


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 2 guests