0

I have a pre-existing csh script on a Linux machine i'd like to update but i'm currently struggling to read two values from a text file which were previously hard coded in the script. The text file is called config.txt and has two lines with the following data:

Config=338893279
Profile=Profile1

In the csh script, i'm creating a $Config and $Profile variables and would like to assign the data from the text file to these variables.

I've been looking at examples using awk but i'm not sure it's the right tool for this as I have different types of data on each line.

I'd really appreciate it if someone please point me in the right direction on how you would get the data into the variables. TIA

Barmar
  • 741,623
  • 53
  • 500
  • 612
user2908926
  • 195
  • 1
  • 2
  • 10
  • Why did you use the `bash` tag when the question is about `csh`? – Barmar Oct 30 '19 at 17:25
  • What part of this are you having trouble with? Reading the lines into variables, splitting them at the `=`? – Barmar Oct 30 '19 at 17:27
  • See https://stackoverflow.com/questions/7735160/how-do-i-split-a-string-in-csh for how to split the string. – Barmar Oct 30 '19 at 17:27
  • It's been more than a decade, but I think `csh` also has a `source` function. You'll need to change the format so it has sthe correct syntax for an assignment statement, `set var = value` and `setenv var value` for local vs exported vars (I'm pretty sure). So `source $pathTo/config.txt` may do it. Sorry don't have a `csh` handy to test. Good luck. – shellter Oct 30 '19 at 20:27

1 Answers1

0

Thanks for the help Barmar & Shelter, In the end I've changed the names of the data in the text file to

Loaded_Config = 338893279
Loaded_Profile = Profile1

And used the following code

#!/bin/csh
set Logfile="/usr/bin/MyAppConfig.txt"
set Loaded_Config=`awk -F"Loaded_Config = " '{print $2}' $Logfile`
set Loaded_Profile=`awk -F"Loaded_Profile = " '{print $2}' $Logfile`
user2908926
  • 195
  • 1
  • 2
  • 10