Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bash] Support program input with the -c flag #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions bash/bf.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,32 @@ die()
exit 1
}

if (( $# )); then
if [[ -e $1 ]]; then
program=$(< "$1")
# program comes from -c flag
program=''
while getopts 'c:' flag; do
case $flag in
c)
program=$OPTARG
;;
'?')
exit 1 # usage error occured, and bash printed a diagnostic
;;
esac
done

if test -z "$program"; then # corner case: -c '' gets here
if (( $# )); then
# program comes from filename argument
if [[ -e $1 ]]; then
program=$(< "$1")
else
die "file '$1' does not exist"
fi
else
die "file '$1' does not exist"
# program comes from stdin
mapfile -t
program=${MAPFILE[*]}
fi
else
mapfile -t
program=${MAPFILE[*]}
fi

program=${program//[^'><+-.,[]']}
Expand Down