Linux Help
guides forums blogs
Home Desktops Distributions ISO Images Logos Newbies Reviews Software Support & Resources Linuxhelp Wiki

Welcome Guest ( Log In | Register )



Advanced DNS Management
New ZoneEdit. New Managment.

FREE DNS Is Back

Sign Up Now
 
Reply to this topicStart new topic
> Bash script (question)
kimball
post Feb 13 2006, 02:11 PM
Post #1


Whats this Lie-nix Thing?
*

Group: Members
Posts: 3
Joined: 13-February 06
Member No.: 6,064



Hello, I have some experience with shell scripting but am stumped on this one thing. How to declare variables in a separate file and have them called in a script. So for example, a user can edit the variables, but is unable to see the script they are being used in. Any help appreciated.
Go to the top of the page
 
+Quote Post
Hemant
post Feb 14 2006, 10:59 AM
Post #2


RMS is my Hero
******

Group: Support Specialist
Posts: 782
Joined: 6-October 02
From: Trichy,INDIA
Member No.: 29



There is a simple thing... whenever you execute a shell script, then that shell script is executed in a new shell and after your shell script terminates that shell is also terminated... and that is the reason why, variables declared in a particular shell script can't be accessed like that.

But there is a way to get around this problem. For example
CODE
#!/bin/bash
export rocks="Metallica"
export sucks="Windows"


if you execute this shell script, like this
CODE
$ chmod +x test.sh
$ ./test.sh
$ echo $rocks
 <<you will get an empty line>>


However if you do something like this
CODE
$ . ./test.sh
$ echo $rocks
Metallica
$ echo $sucks
Windows


So when you prefix . , then the shell script will be executed in the current shell. So you should make sure that you execute both you shell scripts by using ". ./<name>"


--------------------
Hemant Kumar
user posted image
hemant@linuxhelp.net
Go to the top of the page
 
+Quote Post
kimball
post Feb 15 2006, 03:36 PM
Post #3


Whats this Lie-nix Thing?
*

Group: Members
Posts: 3
Joined: 13-February 06
Member No.: 6,064



Neat, so many ways to do it. I came up with my own way by looking at some other sh scripts out there.
A command called include.
So...
File in /home/user is name config
CODE
#########
# Variables #
#########

var1="text"


Then you have a script in /bin owned by root called script1
This is inside the script
CODE
###################
# Load vars using include  #
###################

. $HOME/config
# or (don't use comment on second one)
# . ~/config
# test the var
echo "Your variable is $var1"


Now test it out by going in terminal:
CODE
[root@linux] $ cd /bin
[root@linux] $ chmod 755 (give access to everyone to read and execute)
[root@linux] $ su username
-----------you should see the following output if it was successful
[user@linux] $ script1
Your variable is text


An example of an include can be seen in your home directory in the file .bash_profile (maybe different for others)
There the script looks for a file using the if [-f . ~/filename ]; then command to load variables and aliases.
Hope this can be of help to others as it was to me and thanks for the quick reply.
Go to the top of the page
 
+Quote Post
Hemant
post Feb 16 2006, 05:27 AM
Post #4


RMS is my Hero
******

Group: Support Specialist
Posts: 782
Joined: 6-October 02
From: Trichy,INDIA
Member No.: 29



Cool man.... thanx for the extra bit of knowledge...


--------------------
Hemant Kumar
user posted image
hemant@linuxhelp.net
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 12th December 2017 - 08:45 PM