我已经搜索过有关此特定问题的信息,但找不到任何有用的信息。如何使用bash获取字符串中函数的内容?
假设我已经按照我的~/.bashrc
定义函数(注:这是伪代码!):
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
}
我的问题:
我需要的功能addCaf
,addAosp
和addXos
在一行中。
喜欢,你可以(伪代码)以下bash中运行:
dothis; dothat; doanotherthing; trythis && succeedsdothis || nosuccessdothis; blah
我想运行三个功能addCaf
,在短短的一行addAosp
和addXos
内的所有命令。
任何帮助表示赞赏。
我已经尝试过:
repo forall -c "bash -c \"source ~/.bashrc; addAllAll\""
但是这并没有正常工作。
编辑:
为了澄清我的意思。
我想这样的事情的结果:
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)"'
但我不希望是手动编写。相反,我想从已经存在的函数中获取这些行。
不知道你在问什么,但如果你想在一行上创建一个函数,你可以使用'function f(){echo hi; }'。注意最后的';'。 –
那么,我想在一个字符串中的一行中获取函数的**内容**。 – xdevs23
为什么你需要这些功能的内容,而不是仅仅运行这些功能呢?因为你需要将参数中的调用嵌入'repo'(不管是什么)? '输入addXos'确实(大部分)你需要什么?或者实际上'声明-f addXos'? –