Pipe output to bash function

前端 未结 7 1055
故里飘歌
故里飘歌 2020-11-29 21:11

I have as simple function in a bash script and I would like to pipe stdout to it as an input.

jc_hms(){
  printf \"$1\"
}

I\'d like to use

7条回答
  •  暖寄归人
    2020-11-29 21:32

    1) I know this is a pretty old post

    2) I like most of the answers here

    However, I found this post because I needed to something similar. While everyone agrees stdin is what needs to be used, what the answers here are missing is the actual usage of the /dev/stdin file.

    Using the read builtin forces this function to be used with piped input, so it can no longer be used in a typical way. I think utilizing /dev/stdin is a superior way of solving this problem, so I wanted to add my 2 cents for completeness.

    My solution:

    jc_hms() { 
      declare -i i=${1:-$(

    In action:

    user@hostname:pwd$ jc_hms 7800
    02:10:00
    user@hostname:pwd$ echo 7800 | jc_hms 
    02:10:00
    

    I hope this may help someone.

    Happy hacking!

提交回复
热议问题