LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-03-2009, 12:54 AM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 36
Script Execution Help Needed.


Hi guys i have a script (takeconsole) as following
I have moved it to /usr/local/bin to make the execution of script simpler.
below are the content of the file.

When i run the file by typing "takeconsole" .it always ask for entering the IP address but when i run it by using ". takeconsole". it ask for entering the IP address only at first occurrence.


How can establish 2 scenario by just typing "takeconsole" and not ". takeconsole".

if [ -z "$DISPLAY" ]
then
echo "Please Enter IP address of your machine"
read ipadd
export DISPLAY="$ipadd:0.0"
else
echo display is already set
fi
 
Old 11-03-2009, 01:01 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,295

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
When you run a script as a cmd eg 'takeconsole', it creates a new sub-shell, does what you want, then exits. Note that when it exits, any env vars created/set will disappear.
OTOH, if you use '. takeconsole' (equiv to 'source takeconsole'), then it becomes part of your current env and env vars set will not disappear.
However, obviously, in any env, there can only be one value for a given variable.

HTH
 
Old 11-03-2009, 02:53 AM   #3
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by chrism01 View Post
When you run a script as a cmd eg 'takeconsole', it creates a new sub-shell, does what you want, then exits. Note that when it exits, any env vars created/set will disappear.
OTOH, if you use '. takeconsole' (equiv to 'source takeconsole'), then it becomes part of your current env and env vars set will not disappear.
However, obviously, in any env, there can only be one value for a given variable.

HTH
Thanks for your explanation but if at all i need to run the script not using ". takeconsole" how would i go about it.

I just want the execution to be simpler like other scripts just type the script name and the script should get executed.
 
Old 11-03-2009, 03:31 AM   #4
AwesomeMachine
Senior Member
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora
Posts: 1,927

Rep: Reputation: 280Reputation: 280Reputation: 280
If the program is in /usr/local/bin, you do:

check that /usr/local/bin is in PATH:

echo $PATH

if /usr/local/bin is there stop. Otherwise add /usr/local/bin to PATH:

export PATH='/bin:/usr/bin:/usr/local/bin'

What's probably happening is you copied the program to /usr/local/bin, and the program you are running is the copy in /home/user. If you remove the copy in /home/user, and /usr/local/bin is in PATH, it should work. The "." just means: "the working directory". "./" means "this directory". ".." means "the parent directory". But you shouldn't need any of those for a directory in your PATH.
 
Old 11-03-2009, 04:45 AM   #5
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by AwesomeMachine View Post
If the program is in /usr/local/bin, you do:

check that /usr/local/bin is in PATH:

echo $PATH

if /usr/local/bin is there stop. Otherwise add /usr/local/bin to PATH:

export PATH='/bin:/usr/bin:/usr/local/bin'

What's probably happening is you copied the program to /usr/local/bin, and the program you are running is the copy in /home/user. If you remove the copy in /home/user, and /usr/local/bin is in PATH, it should work. The "." just means: "the working directory". "./" means "this directory". ".." means "the parent directory". But you shouldn't need any of those for a directory in your PATH.
that is not the case my /usr/local/bin is in path and i can run that program from anywhere using the name itself .the only problem is when i run the program by using its name it only export the value of DISPLAY in a scope of the script and it is not exported globally.
To export it globally i need to add . while running the script.
 
Old 11-03-2009, 06:47 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,295

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
Exactly; in shell you can only create or export a shell env var at the current level (X=y) or export it down the process tree (export X=y). You cannot export it up the process tree (or across to other processes not in the current tree).
 
Old 11-03-2009, 09:59 PM   #7
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by chrism01 View Post
Exactly; in shell you can only create or export a shell env var at the current level (X=y) or export it down the process tree (export X=y). You cannot export it up the process tree (or across to other processes not in the current tree).
I have created a function in .profile file with that code.

User Home .profile:
function takeconsole {
if [ -z "$DISPLAY" ]
then
echo "Please Enter IP address of your machine"
read ipadd
export DISPLAY="$ipadd:0.0"
else
echo display is already set
fi
}

And then:

$ takeconsole
 
Old 11-04-2009, 01:31 AM   #8
indiajoe
Member
 
Registered: Jan 2009
Location: India
Distribution: Porteus atma
Posts: 84

Rep: Reputation: 21
define alias

Hi,
You can define alias to make typing simple in your .profile
Code:
alias takeconsole=". /usr/local/bin/takeconsole"
You need not define the script as a funtion in .profile, putting the above line in .profile will do.
Cheers
-indiajoe

Last edited by indiajoe; 11-04-2009 at 01:33 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script execution yakoub Linux - Software 3 11-12-2006 01:26 AM
Script execution salmanucit Linux - Software 1 12-08-2005 08:51 AM
script execution on halt linuxhippy Slackware 4 04-01-2005 07:09 PM
script execution error linux_lover2005 Programming 3 12-22-2004 06:46 AM
script execution Johng Programming 2 08-21-2004 04:58 PM


All times are GMT -5. The time now is 01:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration