3 回答

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊
您可以使用該typeset命令通過(guò)來(lái)使功能在遠(yuǎn)程計(jì)算機(jī)上可用ssh。有多個(gè)選項(xiàng),具體取決于您要如何運(yùn)行遠(yuǎn)程腳本。
#!/bin/bash
# Define your function
myfn () { ls -l; }
要在遠(yuǎn)程主機(jī)上使用該功能:
typeset -f myfn | ssh user@host "$(cat); myfn"
typeset -f myfn | ssh user@host2 "$(cat); myfn"
更好的是,為什么還要麻煩管道:
ssh user@host "$(typeset -f myfn); myfn"
或者,您可以使用HEREDOC:
ssh user@host << EOF
$(typeset -f myfn)
myfn
EOF
如果要發(fā)送腳本中定義的所有函數(shù),而不僅僅是發(fā)送myfn,請(qǐng)typeset -f像這樣使用:
ssh user@host "$(typeset -f); myfn"
說(shuō)明
typeset -f myfn將顯示的定義myfn。
cat將以文本形式接收該函數(shù)的定義,$()并將在當(dāng)前的shell中執(zhí)行它,該shell將成為遠(yuǎn)程shell中的已定義函數(shù)。最后,該功能可以執(zhí)行。
最后的代碼將在ssh執(zhí)行之前將函數(shù)的定義內(nèi)聯(lián)。

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊
我個(gè)人不知道您問(wèn)題的正確答案,但是我有很多安裝腳本只是使用ssh復(fù)制自身。
讓命令復(fù)制文件,加載文件功能,運(yùn)行文件功能,然后刪除文件。
ssh user@host "scp user@otherhost:/myFile ; . myFile ; f ; rm Myfile"

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊
另一種方式:
#!/bin/bash
# Definition of the function
foo () { ls -l; }
# Use the function locally
foo
# Execution of the function on the remote machine.
ssh user@host "$(declare -f foo);foo"
declare -f foo 打印功能的定義
添加回答
舉報(bào)