Fedora Linux Support Community & Resources Center
  #1  
Old 13th November 2007, 08:59 AM
konjo's Avatar
konjo Offline
Registered User
 
Join Date: Nov 2007
Posts: 25
simple bash script

i have a following script :
Code:
PRROOT=$PWD
export PRROOT
and when i run it, and run echo $PRROOT nothing appears, i know i ran the script 'cause i debugged it. and if i put echo $PRROOT as the last line of the script it prints out the $PWD.

also when i execute the above commands in a terminal it works. why wouldn't it work in a script.....
Reply With Quote
  #2  
Old 13th November 2007, 12:44 PM
markkuk Offline
Registered User
 
Join Date: Apr 2005
Location: Finland
Posts: 5,076
A script is run in a new shell that exits when the script finishes. The variable PRROOT is destroyed when the sub-shell exits. It's only visible in the script and in programs started from the script.
Reply With Quote
  #3  
Old 13th November 2007, 01:48 PM
mwette Offline
Registered User
 
Join Date: Nov 2005
Location: Los Angeles area
Posts: 887
you need to source the file:
Code:
$ . myscript
or
$ source myscript
(Notice the dot in the first comand.)
Reply With Quote
  #4  
Old 13th November 2007, 07:07 PM
konjo's Avatar
konjo Offline
Registered User
 
Join Date: Nov 2007
Posts: 25
i didn't knew about the source command, interesting....
but what's up with the ". script" syntax ?
Reply With Quote
  #5  
Old 13th November 2007, 07:51 PM
scottro's Avatar
scottro Offline
Retired Community Manager -- Banned from Texas by popular demand.
 
Join Date: Sep 2007
Location: NYC
Posts: 8,142
A script runs in its own separate shell. So, if you just run the script, it runs in its own little shell and closes--if you ever played with DOS commands and had a bat file, it's the same sort of thing--you'd click on the .bat file, the black dos window opens does whatever and immediately closes.
Doing the . command (dot, space and then the command) tells it to run the command in the current shell rather than opening its own little shell and doing it.

If this isn't clear, let me know and I'll try to explain it less awkwardly.
Reply With Quote
  #6  
Old 13th November 2007, 11:10 PM
konjo's Avatar
konjo Offline
Registered User
 
Join Date: Nov 2007
Posts: 25
it's clear thanks for the explanation.

i have another question if you guys don't mind...
there was a ".profile" file in my home folder when i had SuSe installed, and everything from there was executed upon booting.
is there a similar file in Fedora? or an alternative solution...
Reply With Quote
  #7  
Old 14th November 2007, 02:19 AM
scottro's Avatar
scottro Offline
Retired Community Manager -- Banned from Texas by popular demand.
 
Join Date: Sep 2007
Location: NYC
Posts: 8,142
There is .bash_profile and also .bashrc. It works this way. (This is true for most shells.)

When you first log in, it looks at the .profile, .bash_profile, or whatever. For instance, at least some versions of the korn shell will look at profile and ignore the rc file unless you tell it not to (in the .profile).

Now the rc script is usually examined with each instances of the shell, that is when you're not logging in. (Sheesh, my explanations are obscure tonight. Well, if you're taking your nick from the Japanese word, you'll be brave enough to bear with me. Although, in that case, it should be konjou, sounding like a slightly longer o at the end.)

Ok, you log in. That is, you boot up the machine, or you start a new X session, or whatever--you type in your user name and password. That's when profile, .bash_profile or whatever kicks in.
Now, in that session, you open another shell. For example, you log into gnome by typing your user name and password, and then, while in gnome, you open a terminal. The terminal won't bother with your profile, it will just look at the rc file, which, in most Linux default installations, is .bashrc.

If you look at most (including Fedora's, I think--I'm on BSD right now so can't check, but can give you a similar example) it will often say something like
if [ -f ~/.bashrc ]; then source ~/.bashrc

This means that if there is a .bashrc file in your home directory, read it and use what might be in there. In many cases, that might be the entire .profile or .bash_profile line. So, this means you can put all your environment variables, any aliases and anything else in your .bashrc.

For example, in FreeBSD, which I'm using to write this, the default shell is the somewhat simple original bourne shell. I have a .profile, which contains the line

ENV=$HOME/.shrc; export ENV

This means, look at the .shrc for various environment variables, such as which editor I want to use, what is my path, and everything else.

So, practically speaking, you can usually put these things in either your .bash_profile or .bashrc.

If you look at the man page for bash, and do a search within the page for INVOCATION, it gives additional explanation of what files bash looks for when starting up and when being used interactively, e.g., in a shell script.
Reply With Quote
  #8  
Old 14th November 2007, 02:33 AM
mwette Offline
Registered User
 
Join Date: Nov 2005
Location: Los Angeles area
Posts: 887
Quote:
Originally Posted by konjo
i didn't knew about the source command, interesting....
but what's up with the ". script" syntax ?
I think this is historical. The first unix shell was the Bourne shell
and this used the "." syntax and "source" was added later. (I don't
even know if "source" is part of the Bourne shell.)
Reply With Quote
  #9  
Old 14th November 2007, 03:08 AM
scottro's Avatar
scottro Offline
Retired Community Manager -- Banned from Texas by popular demand.
 
Join Date: Sep 2007
Location: NYC
Posts: 8,142
The source command isn't recognized by sh. (Remember, on Fedora and many other versions of Linux, sh is simply a link to bash.)

It's considered best practice, I believe, to make scripts as portable as possible. For example, some bash scripts, to see if the user is root will make use of the $UID variable, which gives the UID number, which is 0 for root. However, if you're using plain old sh (on a non-Linux system, where it's not linked to bash) that won't work. So, it's good to get in the habit of using the universal $(id -u) rather than $UID.
Reply With Quote
  #10  
Old 14th November 2007, 09:17 AM
konjo's Avatar
konjo Offline
Registered User
 
Join Date: Nov 2007
Posts: 25
well thanks everyone for the info.
Reply With Quote
Reply

Tags
bash, script, simple

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert bash script to perl script homey Programming & Packaging 1 2nd September 2006 04:24 AM
Simple bash wh0rd Programming & Packaging 8 18th May 2006 06:19 PM
simple bash script need doboy408 Using Fedora 2 23rd March 2006 07:23 AM


Current GMT-time: 11:41 (Tuesday, 21-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat