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

@@ -24,7 +24,7 @@ if ! pgrep -ax polybar >/dev/null 2>&1; then
primary="$(xrandr -q | grep primary | cut -d' ' -f1)" primary="$(xrandr -q | grep primary | cut -d' ' -f1)"
for m in $(polybar --list-monitors | cut -d':' -f1); do for m in $(polybar --list-monitors | cut -d':' -f1); do
export TRAY_POS=none export TRAY_POS=none
[ "$m" != "$primary" ] || export TRAY_POS=right [[ "$m" != "$primary" ]] || export TRAY_POS=right
export MONITOR="$m" export MONITOR="$m"
polybar --reload -c "$BASE_DIR/config" main & polybar --reload -c "$BASE_DIR/config" main &
done done

View File

@@ -17,13 +17,13 @@ declare -a ramp
ramp=(          ) ramp=(          )
# display in red when under $low and no charger is connected # display in red when under $low and no charger is connected
[ "$bat" -gt "$low" ] || [ "$ac" -eq 1 ] || color="$red" [[ "$bat" -gt "$low" || "$ac" -eq 1 ]] || color="$red"
# display in green when over $full and a charger is connected # display in green when over $full and a charger is connected
[ "$bat" -lt "$full" ] || [ "$ac" -eq 0 ] || color="$green" [[ "$bat" -lt "$full" || "$ac" -eq 0 ]] || color="$green"
let "icon_index = $bat / (${#ramp[@]} - 1)" let "icon_index = $bat / (${#ramp[@]} - 1)"
[ $icon_index -lt ${#ramp[@]} ] || icond_index=10 [[ $icon_index -lt ${#ramp[@]} ]] || icond_index=10
icon="${ramp[$icon_index]}" icon="${ramp[$icon_index]}"
[ "$ac" -eq 0 ] || charge="" [[ "$ac" -eq 0 ]] || charge=""
echo "${color}${icon}${charge} ${bat}%" echo "${color}${icon}${charge} ${bat}%"

View File

@@ -3,7 +3,7 @@
bluetooth_print() { bluetooth_print() {
bluetoothctl | while read -r; do bluetoothctl | while read -r; do
if [ "$(systemctl is-active "bluetooth.service")" = "active" ]; then if [[ "$(systemctl is-active "bluetooth.service")" = "active" ]]; then
devices_paired=$(echo paired-devices | bluetoothctl | sed -n '/paired-devices/,$p' | grep Device | cut -d ' ' -f 2) devices_paired=$(echo paired-devices | bluetoothctl | sed -n '/paired-devices/,$p' | grep Device | cut -d ' ' -f 2)
counter=0 counter=0
@@ -14,7 +14,7 @@ bluetooth_print() {
if echo "$device_info" | grep -q "Connected: yes"; then if echo "$device_info" | grep -q "Connected: yes"; then
device_alias=$(echo "$device_info" | grep "Alias" | cut -d ' ' -f 2-) device_alias=$(echo "$device_info" | grep "Alias" | cut -d ' ' -f 2-)
if [ $counter -gt 0 ]; then if [[ $counter -gt 0 ]]; then
printf ", %s" "$device_alias" printf ", %s" "$device_alias"
else else
printf ": %s" "$device_alias" printf ": %s" "$device_alias"

View File

@@ -27,7 +27,7 @@ function scheduled_suspend {
# make sure the input was a valid number # make sure the input was a valid number
# side effect: 0 minutes is not possible # side effect: 0 minutes is not possible
[ "$min" -ne 0 ] || exit 1 [[ "$min" -ne 0 ]] || exit 1
notify-send "suspend in" "$min minutes" notify-send "suspend in" "$min minutes"
sleep $((min*60)) && systemctl suspend sleep $((min*60)) && systemctl suspend
@@ -37,15 +37,15 @@ function scheduled_suspend {
# Note: bash does not keep the order of the keys thus they get sorted # Note: bash does not keep the order of the keys thus they get sorted
chosen="$(printf '%s\n' "${!entries[@]}" | sort | rofi "${rofi_args[@]}" "power: ")" chosen="$(printf '%s\n' "${!entries[@]}" | sort | rofi "${rofi_args[@]}" "power: ")"
# exit if nothing was selected # exit if nothing was selected
[ -n "$chosen" ] || exit 1 [[ -n "$chosen" ]] || exit 1
# handle scheduled suspend different # handle scheduled suspend different
[ "$chosen" != "suspend (scheduled)" ] || { ${entries[$chosen]}; exit $?; } [[ "$chosen" != "suspend (scheduled)" ]] || { ${entries[$chosen]}; exit $?; }
# Confirm choice # Confirm choice
yesNo="$(echo -e "yes\nno" | rofi "${rofi_args[@]}" "Are you sure you want to ${chosen}? ")" yesNo="$(echo -e "yes\nno" | rofi "${rofi_args[@]}" "Are you sure you want to ${chosen}? ")"
# Exit if "No" # Exit if "No"
[ "$yesNo" == "yes" ] || exit 1 [[ "$yesNo" == "yes" ]] || exit 1
# Execute # Execute
${entries[$chosen]} ${entries[$chosen]}

View File

@@ -2,7 +2,7 @@
## Created: 2018-11-23 ## Created: 2018-11-23
# Set terminals title if this is a scratchpad-terminal # 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. # 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] # 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 # https://github.com/romkatv/dotfiles-public/blob/7e49fc4fb71d/.zshrc#L35
comp-conf() { comp-conf() {
emulate -L zsh 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 zmodload -F zsh/files b:zf_mv b:zf_rm
local tmp="$1.tmp.$$.zwc" local tmp="$1.tmp.$$.zwc"
{ {
@@ -29,7 +29,7 @@ comp-conf() {
# https://github.com/romkatv/dotfiles-public/blob/7e49fc4fb71d/.zshrc#L47 # https://github.com/romkatv/dotfiles-public/blob/7e49fc4fb71d/.zshrc#L47
comp-source() { comp-source() {
emulate -L zsh emulate -L zsh
[ -e "$1" ] && comp-conf "$1" && source -- "$1" [[ -e "$1" ]] && comp-conf "$1" && source -- "$1"
} }
## set zshoptions ## set zshoptions
@@ -70,21 +70,21 @@ autoload -U select-word-style && select-word-style bash
## Setup the prompt ## Setup the prompt
# use bright version of colors when printing bold # use bright version of colors when printing bold
if command -v dircolors >/dev/null 2>&1; then 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")" eval "$(dircolors -b "${XDG_CONFIG_HOME:-$HOME/.config}/dircolors/dircolors")"
else else
eval "$(dircolors -b)" eval "$(dircolors -b)"
fi fi
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" comp-source "$ZSH_CONF/powerlevel10k/powerlevel10k.zsh-theme"
# To customize prompt, run `p10k configure` or edit $ZSH_CONF/p10k.zsh-theme. # To customize prompt, run `p10k configure` or edit $ZSH_CONF/p10k.zsh-theme.
comp-source "$ZSH_CONF/p10k.zsh-theme" comp-source "$ZSH_CONF/p10k.zsh-theme"
fi fi
## Setup zsh completion system ## 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 autoload -Uz compinit
compinit -d "${XDG_CACHE_HOME:-$HOME/.cache}/zsh/zcompdump-$ZSH_VERSION" 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 autoload edit-command-line; zle -N edit-command-line
! alias run-help >/dev/null 2>&1 || unalias run-help ! alias run-help >/dev/null 2>&1 || unalias run-help
autoload run-help run-help-git zmv autoload run-help run-help-git zmv
if [ -d "$ZDOTDIR/autoload" ]; then if [[ -d "$ZDOTDIR/autoload" ]]; then
fpath=("$ZDOTDIR/autoload" $fpath) fpath=("$ZDOTDIR/autoload" $fpath)
autoload -Uz -- "" "${fpath[1]}"/[^_.]*(.xN:t) autoload -Uz -- "" "${fpath[1]}"/[^_.]*(.xN:t)
fi fi
! command -v direnv >/dev/null 2>&1 || eval "$(direnv hook zsh)" ! command -v direnv >/dev/null 2>&1 || eval "$(direnv hook zsh)"
# stderred # 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 LD_PRELOAD="$ZSH_CONF/stderred/build/libstderred.so${LD_PRELOAD:+:$LD_PRELOAD}"
export STDERRED_ESC_CODE="$(tput bold && tput setaf 1)" export STDERRED_ESC_CODE="$(tput bold && tput setaf 1)"
export STDERRED_BLACKLIST="^(git|curl|wget|swipl)$" export STDERRED_BLACKLIST="^(git|curl|wget|swipl)$"
@@ -143,7 +143,7 @@ comp-source "$ZSH_CONF/completion.zsh"
# async_init # async_init
### syntax-highlight > keys ### syntax-highlight > keys
# syntax highlighting # 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-syntax-highlighting.zsh"
comp-source "$ZSH_CONF/zsh-syntax-highlighting.zsh-theme" comp-source "$ZSH_CONF/zsh-syntax-highlighting.zsh-theme"
fi fi
@@ -164,7 +164,7 @@ elif command -v nano >/dev/null 2>&1; then
fi fi
# `sudo nano` won't work without this (?) # `sudo nano` won't work without this (?)
if [ "$TERM" = "xterm-kitty" ]; then if [[ "$TERM" = "xterm-kitty" ]]; then
export TERM=xterm-256color export TERM=xterm-256color
fi fi
@@ -198,5 +198,5 @@ zle_highlight=('paste:none')
## History ## History
HISTSIZE=1000000 HISTSIZE=1000000
SAVEHIST=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" HISTFILE="${XDG_DATA_HOME:-$HOME/.local/share}/zsh/.zsh_history"

View File

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

View File

@@ -7,7 +7,7 @@
local toplevel="$(git rev-parse --show-toplevel)" || return local toplevel="$(git rev-parse --show-toplevel)" || return
# Exit if no arguements were given # Exit if no arguements were given
[ $# -gt 0 ] || return [[ $# -gt 0 ]] || return
local separator="" local separator=""
for arg in "$@"; do for arg in "$@"; do
@@ -16,11 +16,11 @@ for arg in "$@"; do
# argument relative from git toplevel # argument relative from git toplevel
local arg_from_git="${${arg:A}##$toplevel/}" local arg_from_git="${${arg:A}##$toplevel/}"
# argument has to exist # argument has to exist
[ -e "$arg" ] || continue [[ -e "$arg" ]] || continue
# argument has to exist in repo # argument has to exist in repo
[ -e "$toplevel/$arg_from_git" ] || continue [[ -e "$toplevel/$arg_from_git" ]] || continue
# has to be a submodule # 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" git submodule deinit -f "$arg"
echo "command rm -rf \"$toplevel/.git/modules/$arg_from_git\"" 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. # Execute git show on the commit as preview.
"--preview" " "--preview" "
out=\"\$(echo {} | sed -Ee \"$del_ansi\" -e \"$commit_hash\")\" out=\"\$(echo {} | sed -Ee \"$del_ansi\" -e \"$commit_hash\")\"
if [ \"\$out\" ]; then if [[ \"\$out\" ]]; then
git show \"${(j:%n:)format}\" \"$date\" $colors \"\$out\" \ git show \"${(j:%n:)format}\" \"$date\" $colors \"\$out\" \
| diff-so-fancy | diff-so-fancy
fi fi

View File

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

View File

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

View File

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

View File

@@ -69,7 +69,7 @@ zle -N _expandDots
bindkey . _expandDots bindkey . _expandDots
function ls-on-enter { function ls-on-enter {
if [ -z "$BUFFER" ]; then if [[ -z "$BUFFER" ]]; then
BUFFER=ls BUFFER=ls
zle accept-line zle accept-line
else 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 # "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) # command line history if not (similar to substring-search but with fzf)
function fzf-hist-up { 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 zle up-line-or-history
FZF_HIST_WENT_UP=1 FZF_HIST_WENT_UP=1
else else
@@ -95,7 +95,7 @@ function fzf-hist-up {
} }
function fzf-hist-down { function fzf-hist-down {
zle down-line-or-history 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-up
zle -N fzf-hist-down zle -N fzf-hist-down

View File

@@ -8,9 +8,9 @@
## format of zshs extended history. ## format of zshs extended history.
## An automatic backup is created before deleting that can be used for recovery. ## An automatic backup is created before deleting that can be used for recovery.
[ $# -eq 1 ] || { echo "Specify history file" >&2; exit 1; } [[ $# -eq 1 ]] || { echo "Specify history file" >&2; exit 1; }
[ -e "$1" ] || { echo "File does not exist" >&2; exit 1; } [[ -e "$1" ]] || { echo "File does not exist" >&2; exit 1; }
[ "$(stat -c '%a' "$1")" = "600" ] || { echo "File does not look like a history file" >&2; exit 1; } [[ "$(stat -c '%a' "$1")" = "600" ]] || { echo "File does not look like a history file" >&2; exit 1; }
# Sort the commands per number of occurrences # Sort the commands per number of occurrences
most_used="$(\ most_used="$(\
@@ -42,11 +42,11 @@ else
echo echo
fi fi
[ "${#commands}" -gt 0 ] || exit 0 [[ "${#commands}" -gt 0 ]] || exit 0
printf '%s\n' "${commands[@]}" | column -x printf '%s\n' "${commands[@]}" | column -x
echo "Please confirm the deletion of these commands in $1 ('yes')" echo "Please confirm the deletion of these commands in $1 ('yes')"
read yn read yn
[ "$yn" = "yes" ] || exit 1 [[ "$yn" = "yes" ]] || exit 1
tempd="$(mktemp -d)" tempd="$(mktemp -d)"
cp "$1" "$tempd/$(basename "$1")" cp "$1" "$tempd/$(basename "$1")"

View File

@@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
[ $# -eq 1 ] || { echo "Specify what to do" >&2; exit 1; } [[ $# -eq 1 ]] || { echo "Specify what to do" >&2; exit 1; }
case "$1" in case "$1" in
"--init") INIT=1;; "--init") INIT=1;;