Read variables from a text file for use in csh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read variables from a text file for use in csh script
# 1  
Old 06-11-2016
Read variables from a text file for use in csh script

Hello,

I have a text file (say, declarevars.txt) that contains multiple lines that are essentially meant to be variable declarations:

Code:
set arr1 = (var1a var1b var1c)
set arr2 = (var2a var2b var2c)
.
.
.

I want to be able to read this text file within a csh (sorry) script and have that script treat these lines as if they are part of the script. That is, after reading the text file, if I write

Code:
echo $arr1

within the script, it would print out
Code:
var1a var1b var1c

My reason for doing this instead of setting the variables within the script itself is more for neatness and convenience. I would be using a lot of different scripts doing different things but each would be using these same variables and I didn't want the first 50 lines of each script to always be variable declarations.

Based on what I've found from forums, I have tried the following

Code:
`cat declarevars.txt'

and
Code:

foreach line ("`cat declarevars.txt`")
end

However, none of these seem to work. I am hoping that someone can help me with this question. Thank you very much!

Last edited by Scrutinizer; 06-11-2016 at 06:05 AM.. Reason: Bold tags -> Code tags
# 2  
Old 06-11-2016
If it is in csh syntax you can include it with the source command
Code:
# for safety turn wildcard globbing off
set noglob
source declarevars.txt
unset noglob

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 06-11-2016
Thank you very much, MadeInGermany! It works now. Smilie
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

Shell script to read a file and store in variables

I have a input file like this. Sample.txt 30 | TXDatacenter | TXBackupDC 10 | UKDatacenter | UKBackupDC 0 | NLDatacenter | NLBackupDC ...... ...... ...... I need to get these values in different variables like this. Load1=30 PriCenter1=TXDatacenter... (5 Replies)
Discussion started by: Visha
5 Replies

3. UNIX for Dummies Questions & Answers

Pass variables from a text file to a shell script

Hi, I have a text file as follows: a.txt ------ STEPS=3 STEP_DURATION=100 INTERVAL=60 I want to use these values in a shell script. How to go about this? (3 Replies)
Discussion started by: akarnya
3 Replies

4. Shell Programming and Scripting

Read record from the text file & assign those values to variables in the script

For eg: I have sample.txt file with 4 rows of record like: user1|password1 user2|password2 user3|password3 user4|password4 The username and password is sepsrated by '|' I want to get the 1st row value from the file and assign it to two different variables(username and password) in my... (1 Reply)
Discussion started by: priya001
1 Replies

5. Shell Programming and Scripting

Environment Variables in text file and read command

I cannot get the following substitution ($ORACLE_SID) to work: The variable ORACLE_SID is set to wardin my environment. It has been exported. I have a text file called test.dat: /u07/oradata/${ORACLE_SID}/extab/finmart/summit/ps_voucher_line_crnt_ex.dbf... (2 Replies)
Discussion started by: bradyd
2 Replies

6. Shell Programming and Scripting

Script that takes in variables, and outputs to another text or script file

Ok, I sort of need to create a command files that will be ftped to another server to run. I have some input variable that will need to be read, and then transformed into another script file. Here are some examples. Server 1: outputCmd.sh passing in ./outputCmd.sh nh8oaxt Release_4_0... (1 Reply)
Discussion started by: orozcom
1 Replies

7. Shell Programming and Scripting

csh script for deleting extra spaces in text file

I am new to scripting and I needed to know if there would be an easy way to delete extra spaces in a text file. I have a file with three rows with 22 numbers each, but there is extra spaces between the numbers when it gets output by this program AFNI that I am using. What script would help delete... (2 Replies)
Discussion started by: hertingm
2 Replies

8. Shell Programming and Scripting

Read variables contain spaces from text file

Dears, I developed a shell script to read varibales from text file as the following: cat /dev/null > /rename-OUT.txt while read line do set -- `echo $line` snmpset -c dslmibs $1 sysName.0 octetstring $2 after=$(snmpget -c dslmibs $1 sysName.0 | cut -d: -f3) echo "$1,$2,$after" >>... (1 Reply)
Discussion started by: ahmed.zaher
1 Replies

9. Shell Programming and Scripting

how can i read text file and assign its values to variables using shell

Hello, I have a cat.dat file, i would like shell to read each 3 lines and set this 3 lines to 3 different variables. my cat.dat is: 11 12 +380486461001 12 13 +380486461002 13 14 +380486461003 i want shell to make a loop and assign 1st line to student_id, 2nd line to... (4 Replies)
Discussion started by: rosalinda
4 Replies

10. Shell Programming and Scripting

Help required with a Csh script to read data from a file

Dears, This is what i want.. I need to read a comma separated text file whose name is config.txt. whose content is like ; bscnara,btserrr bscsana,btssanacity ..... i need to read the first string and second string and use it to execute a another shell script. This is the logic. ... (1 Reply)
Discussion started by: fizzme
1 Replies
Login or Register to Ask a Question