LinuxQuestions.org
Help answer threads with 0 replies.
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 12-19-2011, 02:47 PM   #1
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Rep: Reputation: Disabled
Reading in variables from another file


Ive just started scripting a bit and am having troubles trying to read in variables from my script from another file.

File 1 (var.s)
Code:
#!/bin/bash
par1=55
par2=75
par3=95
File 2 (tst.s)
Code:
#!/bin/bash
var.s
echo "par1 is $par1"
echo "par2 is $par2"
echo "par3 is $par3"

I thought all I would have to do was call on var.s in the script file for this to work, but it is just outputting:
par1 is
par2 is
par3 is


I was planning on doing this for more complicated stuff but I just wanted to get a basic case to work first.


Thanks
 
Old 12-19-2011, 03:09 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!

Almost: you need to "source" the other file, not execute it.
Code:
#!/bin/bash
. var.s
echo "par1 is $par1"
echo "par2 is $par2"
echo "par3 is $par3"

Cheers,
Tink
 
Old 12-19-2011, 03:29 PM   #3
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
wow, thanks
 
Old 12-19-2011, 04:33 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Welcome.
 
Old 12-19-2011, 04:55 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
In case you're not clear about it, sourcing a file means inserting its contents into that point in the current script, so be sure that the file only contains lines you want the script to execute, in proper shell syntax.

Also, you don't need the #!/bin/bash line at the top unless you also intend for it to be a stand-alone script. That line is only necessary to tell the system what interpreter to use when you execute the file directly. When read in any other way by the shell it's treated as a simple comment, since it starts with #.
 
1 members found this post helpful.
  


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
tcl foreach loop not reading variables Stuart07 Programming 1 03-11-2011 03:45 PM
Reading variables from a text file in C++ Mork2k4 Programming 7 09-09-2009 10:54 AM
Python Reading and Declaring Variables from a Text File dudeman41465 Programming 7 01-31-2009 05:54 PM
reading variables from other file RudraB Programming 1 03-28-2008 07:06 AM
BASH script Ė reading and writing variables to a separate file morrolan Programming 10 09-20-2005 08:45 AM


All times are GMT -5. The time now is 11:11 AM.

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