繁体   English   中英

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

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

我想编写一个shell脚本,该脚本应从此处的文档中调用一个函数,如下所示:

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

cat <<:EOD:
funexit 
:EOD:

请给我一个好的解决方案!

如果我对您的理解正确,那么您需要这样,

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

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

你是这个意思吗

#!/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

输出:

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

暂无
暂无

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

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