开发者

cat a file inside a shell function

开发者 https://www.devze.com 2023-02-19 03:44 出处:网络
I am wondering how I can implement something like the following: test(){ cat>file<<\'EOF\' abc EOF

I am wondering how I can implement something like the following:

test(){
  cat>file<<'EOF'
    abc
  EOF
开发者_StackOverflow}

Many thanks.


Qiang:

Remove the spaces in front of EOF (so it's on a line by itself and not indented).


From bash(1):

If the redirection operator is <<-, then all leading tab
characters are stripped from input lines and the line
containing delimiter.  This allows here-documents within
shell scripts to be indented in a natural fashion.

It says tab, and in my testing, tab works, but spaces do not:

#!/bin/bash

cat>file <<-END
    hello
    world
    hello
    END

echo done

(All those indents are tabs; the funny thing about the four-leading-spaces markup for code means only four spaces show up in the rendered text, too.)


Your code should work just fine, is there anything specific you are looking for?

#!/bin/sh

input() {
  cat > file <<EOF
input
line
another line
EOF
}

input

EDIT: Changed function input to input()

0

精彩评论

暂无评论...
验证码 换一张
取 消