shell-scripts: Use [[ instead of [ where possible

Replace all occurrences of [ with [[ in bash and zsh scripts and
configs.
Performance wise it makes sense to use the builtin instead of calling an
external command also when from a functionality stand point `test` would
suffice.
This commit is contained in:
2020-10-03 01:29:29 +02:00
parent 29d28a25ee
commit 4cb445c2b5
14 changed files with 64 additions and 64 deletions

View File

@@ -2,7 +2,7 @@
## Created: 2018-11-23
# Set terminals title if this is a scratchpad-terminal
[ -z "$SCRATCHPAD_TERMINAL" ] || printf "\x1b\x5d\x30\x3bscratchpad-terminal\x07"
[[ -z "$SCRATCHPAD_TERMINAL" ]] || printf "\x1b\x5d\x30\x3bscratchpad-terminal\x07"
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.config/zsh/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
@@ -16,7 +16,7 @@ ZSH_CONF="$ZDOTDIR/plugins"
# https://github.com/romkatv/dotfiles-public/blob/7e49fc4fb71d/.zshrc#L35
comp-conf() {
emulate -L zsh
[ ! "$1.zwc" -nt "$1" ] && [ -w "${1:h}" ] || return 0
[[ ! "$1.zwc" -nt "$1" && -w "${1:h}" ]] || return 0
zmodload -F zsh/files b:zf_mv b:zf_rm
local tmp="$1.tmp.$$.zwc"
{
@@ -29,7 +29,7 @@ comp-conf() {
# https://github.com/romkatv/dotfiles-public/blob/7e49fc4fb71d/.zshrc#L47
comp-source() {
emulate -L zsh
[ -e "$1" ] && comp-conf "$1" && source -- "$1"
[[ -e "$1" ]] && comp-conf "$1" && source -- "$1"
}
## set zshoptions
@@ -70,21 +70,21 @@ autoload -U select-word-style && select-word-style bash
## Setup the prompt
# use bright version of colors when printing bold
if command -v dircolors >/dev/null 2>&1; then
if [ -e "${XDG_CONFIG_HOME:-$HOME/.config}/dircolors/dircolors" ]; then
if [[ -e "${XDG_CONFIG_HOME:-$HOME/.config}/dircolors/dircolors" ]]; then
eval "$(dircolors -b "${XDG_CONFIG_HOME:-$HOME/.config}/dircolors/dircolors")"
else
eval "$(dircolors -b)"
fi
fi
if [ -e "$ZSH_CONF/powerlevel10k/powerlevel10k.zsh-theme" ]; then
if [[ -e "$ZSH_CONF/powerlevel10k/powerlevel10k.zsh-theme" ]]; then
comp-source "$ZSH_CONF/powerlevel10k/powerlevel10k.zsh-theme"
# To customize prompt, run `p10k configure` or edit $ZSH_CONF/p10k.zsh-theme.
comp-source "$ZSH_CONF/p10k.zsh-theme"
fi
## Setup zsh completion system
[ ! -d "$ZSH_CONF/completion" ] || fpath=("$ZSH_CONF/completion" $fpath)
[[ ! -d "$ZSH_CONF/completion" ]] || fpath=("$ZSH_CONF/completion" $fpath)
autoload -Uz compinit
compinit -d "${XDG_CACHE_HOME:-$HOME/.cache}/zsh/zcompdump-$ZSH_VERSION"
@@ -123,13 +123,13 @@ comp-source "$ZSH_CONF/fzf-tab/fzf-tab.plugin.zsh"
autoload edit-command-line; zle -N edit-command-line
! alias run-help >/dev/null 2>&1 || unalias run-help
autoload run-help run-help-git zmv
if [ -d "$ZDOTDIR/autoload" ]; then
if [[ -d "$ZDOTDIR/autoload" ]]; then
fpath=("$ZDOTDIR/autoload" $fpath)
autoload -Uz -- "" "${fpath[1]}"/[^_.]*(.xN:t)
fi
! command -v direnv >/dev/null 2>&1 || eval "$(direnv hook zsh)"
# stderred
if [ -e "$ZSH_CONF/stderred/build/libstderred.so" ]; then
if [[ -e "$ZSH_CONF/stderred/build/libstderred.so" ]]; then
export LD_PRELOAD="$ZSH_CONF/stderred/build/libstderred.so${LD_PRELOAD:+:$LD_PRELOAD}"
export STDERRED_ESC_CODE="$(tput bold && tput setaf 1)"
export STDERRED_BLACKLIST="^(git|curl|wget|swipl)$"
@@ -143,7 +143,7 @@ comp-source "$ZSH_CONF/completion.zsh"
# async_init
### syntax-highlight > keys
# syntax highlighting
if [ -e "$ZSH_CONF/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ]; then
if [[ -e "$ZSH_CONF/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ]]; then
comp-source "$ZSH_CONF/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
comp-source "$ZSH_CONF/zsh-syntax-highlighting.zsh-theme"
fi
@@ -164,7 +164,7 @@ elif command -v nano >/dev/null 2>&1; then
fi
# `sudo nano` won't work without this (?)
if [ "$TERM" = "xterm-kitty" ]; then
if [[ "$TERM" = "xterm-kitty" ]]; then
export TERM=xterm-256color
fi
@@ -198,5 +198,5 @@ zle_highlight=('paste:none')
## History
HISTSIZE=1000000
SAVEHIST=1000000
[ -d "${XDG_DATA_HOME:-$HOME/.local/share}/zsh" ] || mkdir -p "${XDG_DATA_HOME:-$HOME/.local/share}/zsh"
[[ -d "${XDG_DATA_HOME:-$HOME/.local/share}/zsh" ]] || mkdir -p "${XDG_DATA_HOME:-$HOME/.local/share}/zsh"
HISTFILE="${XDG_DATA_HOME:-$HOME/.local/share}/zsh/.zsh_history"

View File

@@ -12,7 +12,7 @@ if (( ! idx )); then
fi
local style="${@[$idx]#-style}" prefix=""
if [ -n "$style" ]; then
if [[ -n "$style" ]]; then
# Flag was given in form -style=<style>
style="${style#=}"
prefix="-style="
@@ -21,7 +21,7 @@ else
(( idx++ ))
style="${@[$idx]}"
fi
if [ ! -e "$style" ]; then
if [[ ! -e "$style" ]]; then
# Argument is not a file and thus probably a valid style string that can
# be passes to clang-format
command clang-format "$@"

View File

@@ -7,7 +7,7 @@
local toplevel="$(git rev-parse --show-toplevel)" || return
# Exit if no arguements were given
[ $# -gt 0 ] || return
[[ $# -gt 0 ]] || return
local separator=""
for arg in "$@"; do
@@ -16,11 +16,11 @@ for arg in "$@"; do
# argument relative from git toplevel
local arg_from_git="${${arg:A}##$toplevel/}"
# argument has to exist
[ -e "$arg" ] || continue
[[ -e "$arg" ]] || continue
# argument has to exist in repo
[ -e "$toplevel/$arg_from_git" ] || continue
[[ -e "$toplevel/$arg_from_git" ]] || continue
# has to be a submodule
[ -e "$toplevel/.git/modules/$arg_from_git" ] || continue
[[ -e "$toplevel/.git/modules/$arg_from_git" ]] || continue
git submodule deinit -f "$arg"
echo "command rm -rf \"$toplevel/.git/modules/$arg_from_git\""

View File

@@ -52,7 +52,7 @@ local -a fzf_args=(
# Execute git show on the commit as preview.
"--preview" "
out=\"\$(echo {} | sed -Ee \"$del_ansi\" -e \"$commit_hash\")\"
if [ \"\$out\" ]; then
if [[ \"\$out\" ]]; then
git show \"${(j:%n:)format}\" \"$date\" $colors \"\$out\" \
| diff-so-fancy
fi

View File

@@ -20,9 +20,9 @@ builtin local -a dirs files
# All other arguments are kept.
for arg in "$@"; do
shift
if [ -d "$arg" ]; then
if [[ -d "$arg" ]]; then
dirs+="$arg"
elif [ -e "$arg" ]; then
elif [[ -e "$arg" ]]; then
files+="$arg"
else
set -- "$@" "$arg"
@@ -36,7 +36,7 @@ fi
# Do not separate files and directories if -d flag was specified.
while getopts d flag 2>/dev/null; do
if [ "$flag" = "d" ]; then
if [[ "$flag" = "d" ]]; then
command ls "$@" -- "${files[@]}" "${dirs[@]}"
return
fi

View File

@@ -59,7 +59,7 @@
&& sudo apt upgrade -y \
&& sudo apt autoremove -y
[ ! -e /var/run/reboot-required ] \
[[ ! -e /var/run/reboot-required ]] \
|| printf "\n\nSystem restart required.\n"
'
alias pdf2t{e,}xt='pdftotext'

View File

@@ -25,10 +25,10 @@ function mkcd () {
# Create directory
mkdir "$@"
# shift arguments if mkdir options were used
while [ $# -gt 1 ]; do
while [[ $# -gt 1 ]]; do
shift
done
if [ -d "$1" ]; then
if [[ -d "$1" ]]; then
cd "$1"
pwd
fi
@@ -36,7 +36,7 @@ function mkcd () {
## Send a message over telegram by using the -e flag
function msg() {
if [ $# -ge 2 ]; then
if [[ $# -ge 2 ]]; then
telegram-cli -W -e "msg $*" | grep -E "${${*/ /.*}//_/ }"
# | grep -E "$(echo "$*" | sed 's/ /.*/; s/_/ /g')"
else
@@ -47,7 +47,7 @@ function msg() {
## Execute tg -e command but cuts of the uninteresting parts
function tg() {
tg="telegram-cli"
if [ "$1" = "-e" ]; then
if [[ "$1" = "-e" ]]; then
shift
$tg -N -W -e "$@" | tail -n +9 | head -n -2
else
@@ -67,7 +67,7 @@ function qr() {
## Edit config file
function conf() {
local CONF_EDITOR
if [ -n "$EDITOR" ]; then
if [[ -n "$EDITOR" ]]; then
CONF_EDITOR="$EDITOR"
elif command -v vim &>/dev/null; then
CONF_EDITOR=vim
@@ -88,7 +88,7 @@ function conf() {
shift $(( $OPTIND - 1 ))
# conf needs an argument
if [ $# -eq 0 ]; then
if [[ $# -eq 0 ]]; then
printf "\033[1;31mPlease specify a config.\n\033[0m" >&2
return 1
fi
@@ -100,7 +100,7 @@ function conf() {
fi
# If specific name is given, open file.
if [ $# -gt 1 ]; then
if [[ $# -gt 1 ]]; then
"$CONF_EDITOR" "$CONF_DIR/$2"
return
fi
@@ -121,10 +121,10 @@ function conf() {
# check if config file exists
for config in $CONF_PATTERNS; do
if [ -e "$CONF_DIR/$config" ]; then
if [[ -e "$CONF_DIR/$config" ]]; then
$CONF_EDITOR "$CONF_DIR/$config"
return 0
elif [ -e "$CONF_DIR/.$config" ]; then
elif [[ -e "$CONF_DIR/.$config" ]]; then
$CONF_EDITOR "$CONF_DIR/.$config"
return 0
fi
@@ -132,10 +132,10 @@ function conf() {
# if no config was found in a location other than HOME, look again in HOME.
# (For cases like default vim with ~/.vim/ and ~/.vimrc)
if [ "$CONF_DIR" != "$HOME" ];then
if [[ "$CONF_DIR" != "$HOME" ]];then
for config in $CONF_PATTERNS; do
# Only look for hidden files
if [ -e "$HOME/.$config" ]; then
if [[ -e "$HOME/.$config" ]]; then
$CONF_EDITOR "$HOME/.$config"
return 0
fi
@@ -149,7 +149,7 @@ function conf() {
## Change into config dir
function c() {
CONF_DIR="$(_get_config_dir $*)"
if [ $? -eq 0 ]; then
if [[ $? -eq 0 ]]; then
cd "$CONF_DIR"
else
printf "$CONF_DIR" >&2
@@ -158,14 +158,14 @@ function c() {
}
## Get config directory
function _get_config_dir() {
if [ $# -gt 1 ]; then
if [[ $# -gt 1 ]]; then
printf "\033[1;31mPlease specify one config.\n\033[0m" >&2
return 1
elif [ $# -eq 0 ]; then
elif [[ $# -eq 0 ]]; then
echo "${XDG_CONFIG_HOME:-$HOME/.config}"
elif [ -d "${XDG_CONFIG_HOME:-$HOME/.config}/$1" ]; then
elif [[ -d "${XDG_CONFIG_HOME:-$HOME/.config}/$1" ]]; then
echo "${XDG_CONFIG_HOME:-$HOME/.config}/$1"
elif [ -d "$HOME/.$1" ]; then
elif [[ -d "$HOME/.$1" ]]; then
echo "$HOME/.$1"
else
printf "\033[1;31mCould not find config home.\n\033[0m" >&2
@@ -235,20 +235,20 @@ function resolve() {
echo "$THIS_COMMAND$THIS_ARGUMENTS"
fi
THIS_COMMAND="$(which $THIS_COMMAND)"
if [ $? -ne 0 ]; then
if [[ $? -ne 0 ]]; then
echo "${THIS_COMMAND%% *} not found." >&2
return 1
fi
if (( $VERBOSE_MODE )); then
echo -n "$THIS_COMMAND"
NEXT_STEP="$(file -bh $THIS_COMMAND | cut -d' ' -f4-)"
if [ "${NEXT_STEP:0:1}" != '/' ]; then
if [[ "${NEXT_STEP:0:1}" != '/' ]]; then
NEXT_STEP="${THIS_COMMAND%/*}/$NEXT_STEP"
fi
while [[ "$(file -bh $THIS_COMMAND)" =~ "^symbolic link to" && "$NEXT_STEP" != "$THIS_COMMAND" ]]; do
THIS_COMMAND=$NEXT_STEP
NEXT_STEP="$(file -bh $THIS_COMMAND | cut -d' ' -f4-)"
if [ "${NEXT_STEP:0:1}" != '/' ]; then
if [[ "${NEXT_STEP:0:1}" != '/' ]]; then
NEXT_STEP="${THIS_COMMAND%/*}/$NEXT_STEP"
fi
@@ -268,7 +268,7 @@ function resolve() {
## Grep a keyword at the beginning of a line (ignoring whitespace) in a man page
function mangrep() {
if [ $# -lt 2 ]; then
if [[ $# -lt 2 ]]; then
printf "usage: mangrep <man page> <pattern> [<man flags>]\n" >&2
printf "example: mangrep bash \"(declare|typeset)\"\n" >&2
return 1
@@ -279,8 +279,8 @@ function mangrep() {
}
safe-remove() {
[ $# -gt 0 ] || return 1
[ -e "$1" ] || return 1
[[ $# -gt 0 ]] || return 1
[[ -e "$1" ]] || return 1
sync
if ! udisksctl unmount -b "$1"; then

View File

@@ -69,7 +69,7 @@ zle -N _expandDots
bindkey . _expandDots
function ls-on-enter {
if [ -z "$BUFFER" ]; then
if [[ -z "$BUFFER" ]]; then
BUFFER=ls
zle accept-line
else
@@ -85,7 +85,7 @@ ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(ls-on-enter)
# "Scroll" through history if buffer was empty but use it as query for fzf over
# command line history if not (similar to substring-search but with fzf)
function fzf-hist-up {
if [ -z "$BUFFER" ] || [ "$FZF_HIST_WENT_UP" -eq 1 ]; then
if [[ -z "$BUFFER" || "$FZF_HIST_WENT_UP" -eq 1 ]]; then
zle up-line-or-history
FZF_HIST_WENT_UP=1
else
@@ -95,7 +95,7 @@ function fzf-hist-up {
}
function fzf-hist-down {
zle down-line-or-history
[ -n "$BUFFER" ] || FZF_HIST_WENT_UP=
[[ -n "$BUFFER" ]] || FZF_HIST_WENT_UP=
}
zle -N fzf-hist-up
zle -N fzf-hist-down