Source functions from variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Source functions from variable
# 1  
Old 04-28-2017
Source functions from variable

I have a functions file containing functions:

file.functions:
Code:
myTest () {
echo "MYPWD=$(echo $(pwd))"
echo "MYLOGNAME=${LOGNAME}"
}
myCar () {
echo "MYDATE=$(date)"
echo "MYUPTIME=$(uptime)"
}

i know i can source this file in another script using something similar to this:

Code:
source /tmp/file.functions
or
. /tmp/file.functions

however, suppose I have the content of /tmp/file.functions in a variable, how can I source it?

i.e.

Code:
MyFunctions=$(cat /tmp/file.functions)

# 2  
Old 04-28-2017
Instead of MyFunctions=$(cat /tmp/file.functions), why not just use MyFunctions=/tmp/file.functions and then use:
Code:
source "$MyFunctions"

or
Code:
. "$MyFunctions"

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 04-29-2017
Quote:
Originally Posted by Don Cragun
Instead of MyFunctions=$(cat /tmp/file.functions), why not just use MyFunctions=/tmp/file.functions and then use:
Code:
source "$MyFunctions"

or
Code:
. "$MyFunctions"

ok, i provided the cat example to depict functions being in a variable.

in the case that am working on, the functions will be in a variable. i only catted them in the example to show precisely what i was aiming for.

i need to source a variable.

ok., let me put a different way.

Code:
MyFunctions='
myTest () {
echo "MYPWD=$(echo $(pwd))"
echo "MYLOGNAME=${LOGNAME}"
}
myCar () {
echo "MYDATE=$(date)"
echo "MYUPTIME=$(uptime)"
}
'

can i source this variable now?
# 4  
Old 04-29-2017
Hi,
this is just another way.

You only nee to add a semicolon at the end of each command.

Code:
myTest () { echo "MYPWD=$(echo $(pwd))"; echo "MYLOGNAME=${LOGNAME}"; } myCar () { echo "MYDATE=$(date)"; echo "MYUPTIME=$(uptime)"; }

Then:

Code:
MyFunctions=$(cat /tmp/file.functions)

eval $MyFunctions

typeset -f myTest

myTest () { echo "MYPWD=$(echo $(pwd))" echo "MYLOGNAME=${LOGNAME}" }

Greetings!

Last edited by AbelLuis; 04-29-2017 at 01:58 AM..
This User Gave Thanks to AbelLuis For This Post:
# 5  
Old 04-29-2017
Can I ask why you need to put functions into an "intermediate" container object? Doing what Don Cragun suggested is simpler and standard.

Your efforts will make code maintenance much harder. This may not apply to you, but don't confuse cool with good programming practice. The two are not always congruent.
# 6  
Old 04-29-2017
@jim mcnamara

I added that way because it was the original request.

Quote:
Originally Posted by SkySmart

however, suppose I have the content of /tmp/file.functions in a variable, how can I source it?

i.e.

Code:
MyFunctions=$(cat /tmp/file.functions)

Regards.
This User Gave Thanks to AbelLuis For This Post:
# 7  
Old 04-29-2017
i really need to be able to source functions however they are stored in a variable.

i understand it shouldnt be done this way. but i have my own need to do it this way.

doesnt look like its possible Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Source functions from variable

so i found a way to do this. #!/bin/bash MYF=$(/home/jason/myfunctions) source <(printf '%s\n' "${MYF}") echo "${var1}" /home/jason/myfunctions is a script that outputs information..i.e. functions...etc that needs to be sourced. I have no control over /home/jason/myfunctions. i can... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Local variable in functions (gawk)

Hi Everybody :) I need your help, because i know a local variable in a function for example k, it is different of other variable(with the same name k) this a global variable. Is that right? dgawk> run Starting program: 3238860128818202 3 4 7 11 12 13 17 22 23 32 35 37 41 48 49 55 63 ... (5 Replies)
Discussion started by: solaris21
5 Replies

3. Shell Programming and Scripting

Need help on Assigning a Array variable from Background Functions

I have a question on how can I assign a output of a function to a variable which is executed in background. Here is my example $ cat sample_program.sh #!/bin/ksh exec_func () { sleep 1 v=`expr $1 + 100` print $v } export OUT_ARR date for i in 1 2 do OUT_ARR=`exec_func $i` &... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

4. Shell Programming and Scripting

Share variable between functions

Hi, gtkdialog gui seperate buttons use seperate functions in their GUI. A variable (newFolderName) is being set in one function (funcA) and it needs to be called in another function (funcB). I thought using global variable (like in python) concept should work however it does not. ... (4 Replies)
Discussion started by: singhai.nish
4 Replies

5. UNIX for Dummies Questions & Answers

Overwrite a Source Script Variable Value

Hello All, How do i overwrite a sourced script variable value. Sourced Script: GEN_PARAM_LIST4=""$LOG_DIR"/dwh_GenerateXMLFile.lst" GEN_PARAM_LIST4_v2=""$LOG_DIR"/dwh_GenerateXMLFile.v2.lst" I am using below statement for replacing. Script2: &&... (1 Reply)
Discussion started by: Ariean
1 Replies

6. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

7. Shell Programming and Scripting

Source environment variable in script

Hi, I construct a bash script to finish some tasks. One of the task is to set up some environment variables. There is already one script file to complete the environment variables setting work. I am not able to know the detail of how to set these variables. So, I may just need to call this... (4 Replies)
Discussion started by: gofortime
4 Replies

8. UNIX for Dummies Questions & Answers

== vs -eq and functions

Hey I have a question.... what is the difference between using == vs -eq when testing in WHILE loops. I use the following test that only worked with == signs.... if why do i need == and not -eq? 2. I need to re-use some code in a couple places in this script. is functions my best... (5 Replies)
Discussion started by: danieldcc
5 Replies

9. Shell Programming and Scripting

Use of functions

Hi my shell is tcsh can I have functions in my shell scripting? Is the below shell script correct. Can I have two functions and call one of them as required. ---------- echo "functions" f1 f1 () { echo "hello" } f2 () (1 Reply)
Discussion started by: amitrajvarma
1 Replies
Login or Register to Ask a Question