Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the content of a function in a string using bash?

I have already searched about this particular problem, but couldn't find anything helpful.

Let's assume I have following functions defined in my ~/.bashrc (Note: this is pseudo-code!):

ANDROID_PLATFORM_ROOT="/home/simao/xos/src/"

function getPlatformPath() {
  echo "$ANDROID_PLATFORM_ROOT"
}

function addCaf() {
  # Me doing stuff
  echo "blah/$(getPlatformPath)"
}

function addAosp() {
  # Me doing stuff
  echo "aosp/$(getPlatformPath)"
}

function addXos() {
  # Me doing stuff
  echo "xos/$(getPlatformPath)"
}

function addAllAll() {
  cd $(gettop)
  # repo forall -c "addCaf; addAosp; addXos" # Does not work!
  repo forall -c # Here is where I need all those commands
}

My problem:

I need to get the functions addCaf, addAosp and addXos in one single line.

Like you can run following in bash (pseudo code):

dothis; dothat; doanotherthing; trythis && succeedsdothis || nosuccessdothis; blah

I would like to run all commands inside the three functions addCaf, addAosp and addXos in just one line.

Any help is appreciated.

What I already tried:

repo forall -c "bash -c \"source ~/.bashrc; addAllAll\""

But that didn't work as well.

Edit:

To clarify what I mean.

I want something like that as a result:

repo forall -c 'function getPlatformPath() { echo "$ANDROID_PLATFORM_ROOT"; }; ANDROID_PLATFORM_ROOT="/home/simao/xos/src/"; echo "blah/$(getPlatformPath)"; echo "aosp/$(getPlatformPath)"; echo "xos/$(getPlatformPath)"'

But I don't want to write that manually. Instead, I want to get those lines from the functions that already exist.

like image 809
xdevs23 Avatar asked Oct 28 '25 09:10

xdevs23


1 Answers

You can use type and then parse its output to do whatever you want to do with the code lines.

$ foo() {
> echo foo
> }

$ type foo
foo is a function
foo () 
{ 
    echo foo
}

Perhaps this example makes things more clear:

#!/bin/bash

foo() {
    echo "foo"
}

bar() {
    echo "bar"
}

export IFS=$'\n'

for f in foo bar; do
    for i in $(type $f | head -n-1 | tail -n+4); do
        eval $i
    done
done

exit 0

This is how it looks:

$ ./funcs.sh 
foo
bar

What the script is doing is first loop over all the functions you have (in this case only foo and bar). For each function, it loops over the code of that function (skipping the useless lines from type's output) and it executes them. So at the end it's the same as having this code...

echo "foo"
echo "bar"

...which are exactly the code lines inside the functions, and you are executing them one after the other.

Note that you could also build a string variable containing all the code lines separated by ; if instead of running eval on every line you do something like this:

code_lines=

for f in foo bar; do
        for i in $(type $f | head -n-1 | tail -n+4); do
                if [ -z $code_lines ]; then
                        code_lines="$i"
                else
                        code_lines="${code_lines}; $i"
                fi  
        done
done

eval $code_lines
like image 101
Vincent Olivert Riera Avatar answered Oct 30 '25 02:10

Vincent Olivert Riera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!