execute bash function over ssh
#!/bin/bash
function show_uptime() {
uptime
}
PEM_FILE=some/path
ssh -i ${PEM_FILE} $1 "$(typeset -f show_uptime); show_uptime"
neovim view who changed expandtab
When pressing newline in a .go file, I got tabs instead of spaces (my default setting). This helped me track where the setting was changed:
:verb set expandtab?
bash read line handle case of no newline at end of file
Found the answer here:
while IFS= read -r LINE || [[ -n "$LINE" ]]; do
echo "$LINE"
done
bash overridable variables in scripts
This script will show default when ran without any arguments:
myvar=${myvar:=default}
echo $myvar
and running it like this:
myvar=different ./script.sh
will show different
git error cannot lock ref
Had this error:
error: cannot lock ref 'refs/remotes/origin/feature/SomeBranch': ref refs/remotes/origin/feature/SomeBranch is at SOME_SHA b ut expected SOME_OTHER_SHA
This fixed it:
rm .git/refs/remotes/origin/feature/SomeBranch
vim compare two files in a split
Setup a split, and then write:
:windo diffthis
To disable diffing:
:windo diffoff
bash process substitution while loop
I recently stumbled upon a problem in a bash script. I was doing something like this:
cat $1 | while read -r line; do
line_with_new_value=$line
echo $line_with_new_value | grep -vE '^#|^$' | grep -o -E '\${.*?}' | while read -r dollar_var; do
# do something with line_with_new_value here
#
done
echo $line_with_new_value
done
Basically, when I was echoing, after the while, I was expecting the modified value to be shown. However, piping into something will create a new subshell, and thus the modification is only visible inside that shell. This link gives some useful examples on process substitution. The fix was to rewrite that loop like this:
cat $1 | while read -r line; do
line_with_new_value=$line
while read -r interpolation; do
# modify line_with_new_value here
done < <(echo $interpolated_line | grep -vE '^#|^$' | grep -o -E '\${.*?}')
echo $line_with_new_value
done
bash regex capture group
capturing a group in bash ( regex MUST NOT BE QUOTED! )
$ [[ '${these}' =~ \${(.+)} ]] && echo ${BASH_REMATCH[1]}
these
grep and show only matching part
Here’s how to grep for something and show only the matching part:
# echo '${these}${are}${vars}' | grep -o -E '\${.*?}'
${these}
${are}
${vars}