perplexity-cli
🧠 A simple command-line client for the Perplexity API. Ask questions and receive answers directly from the terminal! 🚀🚀🚀
A collection of Bash scripts for creating safe and useful command line programs.
git clone https://github.com/xwmx/bash-boilerplate.gitxwmx/bash-boilerplateA collection of Bash starter scripts for easily creating safe and useful command line programs.
Bash Boilerplate is great for making standalone, portable, single-file command line programs as well as shell initialization functions. For a framework approach that's useful for task and build files, try Bask, a pure Bash mini-framework for command-centric Bash scripts.
A simple bash script with some basic strictness checks and help features. Useful for simple programs that don't have many features and don't take options other than help.
-h or --help.A simple bash script with some basic strictness checks, option parsing, help features, easy debug printing. Useful for regular scripts.
-h or --help,debug printing with --debug flag,_exit_1 and _warn functions for error messages,An example of a bash program with subcommands. This contains lots of features and should be usable for creating bash programs that do multiple related tasks.
-h or --help,debug printing with --debug flag,_exit_1 and _warn functions for error messages,Shell function examples and boilerplate. The functions in this file are
intended to be included in the interactive shell, which can be done by
defining them in a shell init file like ~/.bashrc.
Helper functions. These functions are primarily intended to be used within scripts, but can be adapted for use as shell functions.
Use it. It's super useful.
ShellCheck is a static analysis and linting tool for sh/bash scripts. It's mainly focused on handling typical beginner and intermediate level syntax errors and pitfalls where the shell just gives a cryptic error message or strange behavior, but it also reports on a few more advanced issues where corner cases can cause delayed failures.
It can be used with Vim via Syntastic, Emacs via Flycheck, Sublime Text 3 via SublimeLinter, VS Code via vscode-shellcheck, and Atom via linter, atom-lint, or linter-shellcheck.
These boilerplate scripts use some common settings for enforcing strictness in Bash scripts, thereby preventing some errors.
For some additional background, see Aaron Maxwell's "Unofficial Bash Strict Mode" (redsymbol.net) post.
Add this to the top of every script (note: an extended version of this is already included in the boilerplate scripts):
# Bash 'Strict Mode' # http://redsymbol.net/articles/unofficial-bash-strict-mode # https://github.com/xwmx/bash-boilerplate#bash-strict-mode set -o nounset set -o errexit set -o pipefail IFS=$'\n\t'
set -o nounset / set -uTreat unset variables and parameters other than the special parameters @ or
* as an error when performing parameter expansion. An 'unbound variable'
error message will be written to the standard error, and a non-interactive
shell will exit.
Short form:
set -u
Long form:
set -o nounset
Parameter expansion can be used to test for unset variables when using set -o nounset.
http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
The two approaches that are probably the most appropriate are:
${parameter:-word}
If parameter is unset or null, the expansion of word is substituted.
Otherwise, the value of parameter is substituted. In other words, "word"
acts as a default value when the value of "$parameter" is blank. If "word"
is not present, then the default is blank (essentially an empty string).
${parameter:?word}
If parameter is null or unset, the expansion of word (or a message to that
effect if word is not present) is written to the standard error and the
shell, if it is not interactive, exits. Otherwise, the value of parameter
is substituted.
Arrays:
${some_array[@]:-} # blank default value
${some_array[*]:-} # blank default value
${some_array[0]:-} # blank default value
${some_array[0]:-default_value} # default value: the string 'default_value'
Positional variables:
${1:-alternative} # default value: the string 'alternative'
${2:-} # blank default value
With an error message:
${1:?'error message'} # exit with 'error message' if variable is unbound
set -o errexit / set -eExit immediately if a pipeline returns non-zero.
Short form:
set -e
Long form:
set -o errexit
set -o errexit with read -rd ''set -o errexit is super useful for avoiding scary errors, but there are some
things to watch out for. When using read -rd '' with a heredoc, the
exit status is non-zero, even though there isn't an error, and this
setting then causes the script to exit. read -rd '' is equivalent
to read -d $'\0', which means read until it finds a NUL byte, but
it reaches the end of the heredoc without finding one and exits with
a 1 status. Therefore, when reading from heredocs with set -e,
there are three potential solutions:
Solution 1. set +e / set -e again:
set +e read -rd '' variable <<HEREDOC Example text. HEREDOC set -e
Solution 2. <<HEREDOC || true:
read -rd '' variable <<HEREDOC || true Example text. HEREDOC
Solution 3. Don't use set -e or set -o errexit at all.
More information:
'builtin "read -d" behaves differently after "set -e"' (lists.gnu.org)
set -o pipefailReturn value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully.
Long form (no short form available):
set -o pipefail
$IFSSet IFS to just newline and tab.
IFS=$'\n\t'
For some background, see Filenames and Pathnames in Shell: How to do it Correctly (dwheeler.com)
Explicitness and clarity are generally preferable, especially since bash can be difficult to read. This leads to noisier, longer code, but should be easier to maintain. As a result, some general design preferences:
local, such as those defined outside of a function or
automatically through a for loop, prefix with double underscores.${NAME} instead
of $NAME. Braces are only required for variable references in some cases,
but the cognitive overhead involved in keeping track of which cases require
braces can be reduced by simply always using them.printf over echo. For more information, see:
http://unix.stackexchange.com/a/65819$_explicit_variable_name over names like $var.#!/usr/bin/env bash shebang in order to run the preferred
Bash version rather than hard-coding a bash executable path.Scripts based on this project.
Copyright (c) 2015 William Melody • hi@williammelody.com
more like this
🧠 A simple command-line client for the Perplexity API. Ask questions and receive answers directly from the terminal! 🚀🚀🚀
search projects, people, and tags