LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH Confusion......Arrrrg (https://www.linuxquestions.org/questions/programming-9/bash-confusion-arrrrg-83036/)

l0f33t 08-18-2003 03:24 AM

BASH Confusion......Arrrrg
 
Hello all,

Running Linux Kernel: 2.4.19-16mdk
Bash version: 2.05b.0

I've been doing some online tutorials on BASH shell scripting.

I created a script called "foobar" in my home directory. I gave the script (chmod 755 foobar) permissions. Couldn't figure out how to run the script by just using the name.. foobar.

The script runs fine using "bash foobar" or "./foobar" or "sh foobar" or specifying the full complete path. /home/jbanks/foobar and of course putting the file in the /bin directory allows me to run the script by name "foobar"

Buttttt...I wanted to have the ability to exe the script from within my home directory by script name.

On the tutorial they said to just create a /bin directory inside my home directory and move the script into that directory. Done.

Logout then log back in. This is where I'm a little confused. It works....now but when I look at echo $PATH I now have 2 same path statements.. I didn't have to do anything to the .bash_profile either.

This was my path before creating the /bin directory:
[jbanks@localhost jbanks]$ echo $PATH
/usr/X11R6/bin:/usr/local/bin:/bin:/usr/bin

AFTER

[jbanks@localhost jbanks]$ echo $PATH
/bin:/usr/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/local/bin:/usr/bin:/usr/games:/home/jbanks/bin:/home/jbanks/bin

Why is /home/jbanks/bin listed twice?

When I login as root a notice the same type of similarity:
[root@localhost jbanks]# echo $PATH
/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin

Thanks for your help.
Heres a copy of my .bash_profile and .bashrc. Let me know if you need any futher info?

.BASH_PROFILE:
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
unset USERNAME

.BASHRC:
# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi


Thanks, :scratch:

hfawzy 08-18-2003 03:50 AM

Hi,
Quote:


.BASH_PROFILE:
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
unset USERNAME

.BASHRC
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
unset USERNAME
Did you posted your .bash_profile twice?
If no, delete the second part of this file and it will look like that :
Code:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
unset USERNAME

Hope that helps...

l0f33t 08-18-2003 03:59 AM

Woops...

I posted that incorrectly here. I've edit'ed my initial post.

Any clues?

Thanks,

jschiwal 08-21-2003 02:00 AM

Firstly, I would recommend you NOT make the change. The reason the current directory isn't in the path by default, is the risk someone could place a destructive script in a directory using the name of a command like ls. But all you need to do is add "./" in your path variable.

PATH=$PATH:$HOME/bin:./

I must confess to typing in PATH=$PATH:./ during a single session. But I would change any shell startup scripts do make the change permanent.

whansard 08-21-2003 02:23 AM

source ~/foobar

foobar doesn't need to be 755 for that either.


sorry. i just reskimmed your post, and i don't think
thats what you wanted.

you could also
ln -s /path/foobar /usr/local/bin/

l0f33t 08-21-2003 03:19 AM

Thanks for the help everyone.

jschiwal,

Thanks for the Security tip.
You said:
But all you need to do is add "./" in your path variable PATH=$PATH:$HOME/bin:./

Is this done to the .bash_profile or the .bashrc file?
Do I just append :./ to the exsisting Path variable specified in the file/files above?

Why is ./ more secure than having /home/jbanks/bin?

whansard,

What is the difference between "sourcing" a file and "executing" file? They seem the same to me.. :D

Thanks,

whansard 08-21-2003 03:59 AM

i don't really know.
source is a bash builtin. you can run bash source with
it without making the file executable.

l0f33t 08-21-2003 04:05 PM

Thanks,

I guess I'll put mv all my scripts in /home/jbanks/bin to the root /bin file for security reasons and remove the /home/jbanks/bin directory so that path isn't excessible..

JBanks

jschiwal 08-21-2003 04:48 PM

By your response, I think I could have explained things better.

You question was about why you had to precede a command with ./ to get it to run.

Adding ./ to your path variable is the way to include the current directory ($PWD ) in the path.

If for example, you are in a subdirectory, and write a quick shell script to use, having ./ in your path allows you to type in the script without preceding the command with ./ or having to copy the script to ~/bin/.

I didn't want to imply that this change would be secure.

If you do make the change, I would recommend that you enter it into the current terminal you are using only rather than change one of the startup scripts. If you are working in a subdirectory you just made, or the ls listing doesn't show any files added by a hacker, then it should be safe and the path variable change only effects the terminal you are current using. When you exit, your change will be lost.

One other thing you might want to consider. Suppose you add a disk drive, and you are the owner, or group member. If you know that this drive will only contain your data files, and never programs or scripts, consider including the nosuid,noguid and noexec option to the mount command.

Sometimes, a problem can occur due to a typo or other mistake and not to a malicious hacker or network worm. Taking precautions may also help limit damage made accidentally.

jschiwal 08-21-2003 05:04 PM

When I reread the thread, I realized that the original question wasn't answered. I'm not a home right now, so I can't refer to the bash documentation. But I would recommend that you consider that there are different bash startup files.

One of them is used once when you login as a user. This is the one you want to use to make your $PATH variable adjustment.

Another one is used every time you start a new shell. I think that the $PATH adjustment is being run more then once, because it's in the wrong startup file, or is being executed more than once.

Look in MAN BASH for details on the startup scripts. And use the one that is used once when you log in.

l0f33t 08-21-2003 05:12 PM

Thanks,
I'll take a look at "man bash" then.

Thanks jschiwal,


All times are GMT -5. The time now is 05:41 PM.