Hacker News new | past | comments | ask | show | jobs | submit login

Combination of your improvements and the first commentor's in the link

- 'notes' by itself views the file (interactive shell only)

- 'notes' with args, the args are appended to file (original function)

- 'notes' with heredoc (or any piped data) allows for multiline notes

    notes() {
      if [ -n "$1" ]; then
        echo $@ >> "$HOME/notes.md"
      else
        # detect if we're in a tty or a pipe
        if [ -t 0 ]; then
          less "$HOME/notes.md"
        else
          cat - >> "$HOME/notes.md"
        fi
      fi
    }



    usage:
    $ notes 1
    $ notes 1 2 3
    $ notes <<EOF
    > this is a 
    > multiline note
    > EOF
    $ uptime | notes
    $ notes
    (opens less with the following content)
    1
    1 2 3
    this is a 
    multiline note
     23:22:06 up 3 days, 11:41,  1 user,  load average: 0.25, 0.15, 0.26



You should use dollar-star and printf to prevent echo treating some of the arguments as flags:

    printf '%s\n' "$*"
Which works while

    echo "$@"
might not work as expected if you did e.g.:

    notes -n is a bad flag to pass to some commands
The latest posix bans echo from taking any - options, so you may be safe with /bin/echo or if your shebang specifies "sh" but that hasn't percolated down everywhere yet.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: