solo2500
Posts: 123
Joined: Sat Jul 09, 2016 12:38 am

Moving variable from file to program?

Thu Nov 03, 2016 3:28 pm

I have a program written in python that passes a value to a variable in a file. I would like to pass that variable to another python program (of my own). My question is... How do I make the variable available to my python program and how do I print (echo) it? Below is what I invision.. but of course it doesn't work! ( I am a TOTAL novice!!!)
Thanks for any help!!!

file1.tmpl........................ to...................myprogram.sh
$current.outTemp ..................................#!/bin/bash
................................................................echo $current.outTemp
I'm a total novice, non-programer (...basically a hack.)

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Moving variable from file to program?

Thu Nov 03, 2016 8:32 pm

Simple files do not have variables, just content that is written to them.

You can write the content of a variable that is part of a Python script into a file where it will be held permanently.

You can subsequently read the contents of that file with another script and retrieve your content into a variable in the second script.

If this is not working for you it would be a good idea to post the contents of your scripts here, enclosed in [ Code ] markers by using the 5th button at the top of the message edit screen.

solo2500
Posts: 123
Joined: Sat Jul 09, 2016 12:38 am

Re: Moving variable from file to program?

Thu Nov 03, 2016 8:57 pm

Simple files do not have variables, just content that is written to them.
Yes... that's what I was trying to illustrate (admittedly very poorly!!!)
The file file1.tmpl contains $current.outTemp
The file myprogram.sh is a simple shell script to read and print the value of $current.outTemp.

myprogram.sh

Code: Select all

#!/bin/bash

echo $current.outTemp
 
I'm guessing at a minimum there needs to be a link (symbolic?) between the two files?
I'm a total novice, non-programer (...basically a hack.)

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Moving variable from file to program?

Thu Nov 03, 2016 9:15 pm

It might help if you showed the actual content of the file. I'm going to assume it is something like

Code: Select all

21.5
and you want to read the contents of that file into a variable in the script.

Code: Select all

#!/bin/bash
current_outTemp="$(cat file1.tmpl)"
echo $current_outTemp
A dot isn't valid in bash variable names, so I've used an underscore.

Return to “Python”