Source functions from variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Source functions from variable
# 8  
Old 04-29-2017
Quote:
Originally Posted by SkySmart
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
We all know that different shells behave differently. (And we know that you haven't told us what shell or shells you're using for this problem.)

We all know that we can do some things in shell code a few different ways (or a few thousand different ways) depending on what we are trying to do. (And we know that you haven't told us what you are trying to do other than define functions that have been read from a file into a variable.)

The only thing I see here that is impossible is for us to accurately guess at why you think you have to use this method to define functions and what you are going to do with those functions after you define them.

If you would share with us what you are trying to do instead of telling us how you want to implement a small piece of what you want to do, we might be able to help you find a solution to your problem.

P.S. Did you know that some shells can export functions in addition to exporting variables?
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 04-29-2017
Hi.

Possibly:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate source function from variable.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C

pl " Remove variable v1 and function x1:"
unset v1
unset -f x1
set | grep v1
set | grep x1

pl " Create variable:"
v1="
x1() { printf ' Hello, world from x1.\n' ; }
"

pl " Variable v1 = $v1"

pl " Results, try to execute x1, expect failure:"
x1

pl " Source, then execute x1"
source <( printf "%s" "$v1")
x1

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
bash GNU bash 4.3.30

-----
 Remove variable v1 and function x1:
_i=' Remove variable v1 and function x1:'
_i=' Remove variable v1 and function x1:'

-----
 Create variable:

-----
 Variable v1 = 
x1() { printf ' Hello, world from x1.\n' ; }


-----
 Results, try to execute x1, expect failure:
./s1: line 29: x1: command not found

-----
 Source, then execute x1
 Hello, world from x1.

( With an assist from bash - source /dev/stdin doesn't work as expected - Unix & Linux Stack Exchange )

Best wishes ... cheers, drl
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