简体   繁体   中英

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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