Skip to content

Latest commit

 

History

History

stdin

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

STDIN Example

Demonstrates how to read from STDIN.

This example was generated with:

$ bashly init --minimal
# ... now edit bashly.yml to match ...
$ bashly generate
# ... now edit *.sh to match ...
$ bashly generate

bashly.yml

name: cli
help: STDIN Example
version: 0.1.0

args:
- name: path
  
  # Set the default path value to -, which is the standard operator for stdin.
  default: "-"
  help: Path to file (reads from stdin if empty)

flags:
- long: --file
  short: -f
  arg: path

  # This is also supported in flags
  # Note that in both cases, your script needs to handle the value '-' as there
  # is no special treatment in bashly, other than allowing '-' as argument.
  default: "-"
  help: Another path to file

src/root_command.sh

inspect_args

# Since cat knows how to handle '-' as a value, it will work with both a file
# path and '-' argument.
cat "${args[path]}"

# To read the file yourself, use something like this
#
# if [[ "${args[path]}" == "-" ]]; then
#   file=$(</dev/stdin)
# else
#   file=$(<"${args[path]}")
# fi

Output

$ ./cli some-file

args:
- ${args[--file]} = -
- ${args[path]} = some-file
some file with some content

$ cat some-file | ./cli

args:
- ${args[--file]} = -
- ${args[path]} = -
some file with some content

$ cat some-file | ./cli -

args:
- ${args[--file]} = -
- ${args[path]} = -
some file with some content