Exporting Values of Variables to Another File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exporting Values of Variables to Another File
# 1  
Old 03-28-2012
Exporting Values of Variables to Another File

Hello,

I'm very new to Linux, and I have a question, I'm hoping you could help me with. Smilie

I have created a file called subject, contains this code:

Code:
#!/bin/bash

read -p "Student Name: " NAME
read -p "Student ID: " ID
read -p "Address: " ADDRESS

I'm to create another file called data in the same directory and that file is supposed to import the variables from the subject and print their values.

Code:
#!/bin/bash

export NAME
export ID
export ADDRESS
./subject
echo "Name: $NAME"
echo "ID: $ID"
echo "Address: $ADDRESS"

However the above code doesn't import any value from the subject file, and just prints blank.

Thank you in advance Smilie
# 2  
Old 03-28-2012
Is this homework?
# 3  
Old 03-28-2012
Quote:
Originally Posted by Corona688
Is this homework?
Nope, self learning. Smilie
# 4  
Old 03-28-2012
The 'data' file would just be
Code:
My name
My ID
My Address

and you'd use it like
Code:
./myscript < data

# 5  
Old 03-28-2012
Thank you for you reply, but what I mean is how can I make the data use the values of the variables in subject and print them on the screen? Am I using the export incorrectly or is the problem somewhere else?
# 6  
Old 03-28-2012
Code:
./subject < data

By redirecting data into ./subject with <, you are replacing it's standard input. read will read from the file, not the keyboard.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 03-28-2012
You need to run the script called subject in a slightly different way such that it executes in the same shell as the script called data. It needs to be run as . ./subject (dot-space-dot-slash-scriptname).

Code:
#!/bin/bash

export NAME
export ID
export ADDRESS
. ./subject
echo "Name: $NAME"
echo "ID: $ID"
echo "Address: $ADDRESS"

This is the same way that your login profile works.
Btw. Calling a script data is very confusing and has made your earlier posts ambiguous.
This User Gave Thanks to methyl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

2. Shell Programming and Scripting

How to fetch values from a line in a file to variables in UNIX?

Hi, I need to assign values from a lines in a file into variables in unix, i am using Korn shell. I tried the below script from posts but i am unable to fetch every value in a variable. #! /usr/bin/ksh #for file in test.txt; do IFS=$'\|' I=1 while read -a val do echo... (15 Replies)
Discussion started by: karthikram
15 Replies

3. Shell Programming and Scripting

Help in assigning values to variables from the file

Hi! This might be a simple thing, but I'm struggling to assign values to variables from the file. I've the following values stored in the file.. It consists of only two rows.. 10 20 I want to assign the first row value to variable "n1" and the second row value to variable "n2".. That is ... (3 Replies)
Discussion started by: abk07
3 Replies

4. Shell Programming and Scripting

Parse config file and store the values in variables

Hi, I have a config file that has blank, commented lines. I need to escape commented lines, blank lines, parse the remaining lines and store them in variables or array. the config file contains the following lines. # config file # Define Oracle User ORA_USER=abcde ORA_PASS=xyzabc... (8 Replies)
Discussion started by: Lakshmi Chowdam
8 Replies

5. Shell Programming and Scripting

Assign values to variables of a file

Hi, I have a file like the following... CUST= DIR= NULIST= name=philps_123 How can i add values to each of these unassigned variables using a shell script? say for eg: i have values for CUST as onida, dir as /dir/onida, NULIST as /tmp/onida_files. How can i add these values to... (11 Replies)
Discussion started by: Tuxidow
11 Replies

6. Shell Programming and Scripting

Read variables and their values from file

Hi, I want to read the variables and the values from the txt file and compare these values with the ones computed by script. for ex: say var.txt contains the variable names and their values: one 1 two 2 three 3 The value of variables "one" "two" and "three" will be computed in the script... (3 Replies)
Discussion started by: bhushana
3 Replies

7. Shell Programming and Scripting

Exporting variables from subshell to parent shell

Hi, I was trying to do something where I would be able to export one local variable in a telnet subshell to its parent shell. I found something like this over here, but couldnt exactly understand it :(. I am referring to this part actually: #! /usr/bin/ksh exec 4>&1 tail -5 >&4 |& exec... (4 Replies)
Discussion started by: King Nothing
4 Replies

8. Shell Programming and Scripting

Assign Values to variables from a text file

The text file has one single row and looks like this Q1 P1 2006 I have to pick up this values from a shell script into three different variables, say quarter, period and year from the above text file. Some one know's how to do this? I went through 'sed', dint really know how to... (3 Replies)
Discussion started by: sarsani
3 Replies

9. Shell Programming and Scripting

[BASH] mapping of values from file line into variables

Hello, I've been struggling with this for some time but can't find a way to do it and I haven't found any other similar thread. I'd like to get the 'fields' in a line from a file into variables in just one command. The file contains data with the next structure:... (4 Replies)
Discussion started by: semaler
4 Replies

10. Shell Programming and Scripting

exporting variables (shell scripting)

Hi, i've got an executable shell script, called mysc.sh with this line only: export DATASIZE=0 i run it from my console (./mysc.sh), and after that in the console i run: echo $DATASIZE and nothing prints what could be the problem??? thanks!! (2 Replies)
Discussion started by: kfad
2 Replies
Login or Register to Ask a Question