简体   繁体   English

我们可以从shell脚本从此处文档调用函数吗

[英]From shell script can we invoke function from here document

I would like to write a shell script that should call a function from here document as below: 我想编写一个shell脚本,该脚本应从此处的文档中调用一个函数,如下所示:

#!/bin/bash
funexit 
funexit  ()
{
  exit 1
}

cat <<:EOD:
funexit 
:EOD:

Please suggest a good solution to me! 请给我一个好的解决方案!

If I understood you correctly, then you need like this, 如果我对您的理解正确,那么您需要这样,

funexit()
{
  echo "calling funexit"
  exit 1
}

cat <<:EOD:
$(funexit)
:EOD:

Do you mean like this? 你是这个意思吗

#!/bin/bash
funexit()        # Declare function 
{
  echo $1        # echo parameter
  cat            # cat STDIN
  exit 1         # exit (badly)
}

funexit 3 <<EOD  # call function with parameter 3
hello            # pass "hello" to its STDIN
EOD

Output: 输出:

./go
3
hello
~: echo $?       # Check exit status is 1
1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM