Skip to content

Instantly share code, notes, and snippets.

@bitsandbytes
Last active April 25, 2023 17:23
Show Gist options
  • Save bitsandbytes/609ada9f1c083e35ddb2 to your computer and use it in GitHub Desktop.
Save bitsandbytes/609ada9f1c083e35ddb2 to your computer and use it in GitHub Desktop.
Bash setup
# Conditionally set one parameter to another, or use default value
# If ${1} is unset or null, this will set SOME_VAR to "default_value" instead
SOME_VAR=${1:-default_value}
# If ${1} is unset or null, this will set SOME_VAR to the value of OTHER_VAR instead
SOME_VAR=${1:=OTHER_VAR}
# ^M
# in vim
# no really
# here's how to enter ^M so that you can remove it from a file
# in vim:
:%s/
# then, hit <ctrl-v> then <enter>
# viola!
# walla!
# test is var is unset
if [ -z ${var+x} ]; then echo "var is unset"; else echo "var is set to '$var'"; fi
# where ${var+x} is a parameter expansion which evaluates to nothing if var is unset, and substitutes the string x otherwise.
# which rpm provides file...
yum whatprovides *bin/ls
rpm -qf [file name]
# what files in ...
rpm -ql <packagename>
rpm -qlp <path_to_rpm_file>
# Multiple tests within if statement
if [[ $TESTA == "true" && $TESTB == "false ]]; then
...
fi
# return exit code within a script
exit # returns success, 0
exit 1 # returns 1
# test for substring
if [[ $WORD == *"my_substr"* ]]; then
...
fi
# substring substitution
# first instance only
VAR="she favors the bold. That's cold"
echo "${VAR/old/new}" // she favors the bnew. That's cold
# all instances
echo "${VAR//old/new}" // she favors the bnew. That's cnew
# redirect stdout/stderr
# redirect stderr into stdout
./somecommand 2>&1
# write error message to stderr
>&2 echo "error"
# test exit code of last command, found in $?
if [[ $? != 0 ]]; then exit $?; fi
# grep testing if a string exists in a file
if grep -q $somevar $somefile; then...
# incrementing a variable
((var++))
# or if you need to add by more than 1
((var=var+1))
# finding out what ciphers curl uses
curl https://www.howsmyssl.com/a/check
# getting root's mail via sudo
sudo su - root -c mail
# get number of arguments to a script, excluding the script itself
$#
# loop through x times
for i in {1..10}; do ... done
# loop through variable times
typeset -i i END # sets type which helps with memory
for ((i=1;i<=END;++i)); do echo $i; done
# declare an array
HOSTS=()
# declare and initialize an array
declare -a HOSTS=(element1 element2 element3)
# loop through array
for HOST in "${HOSTS[@]}"; do
echo $HOST
done
# append to array
HOSTS+=("$NEW_HOST")
# join an array
HOSTS=$(IFS=, ; echo "${HOSTS[*]}")
echo "$HOSTS"
# add/remove multiple dirs at once
mkdir -p ~/somewhere/{a,b,c,d}
# view certs from an SSL endpoint
openssl s_client -connect [host]:[port]
if [ -f ~/.aliases ]; then source ~/.aliases; fi
PS1='\[\033[1;33m\]`if [ -f /etc/sysconfig/hostname.ext ]; then cat /etc/sysconfig/hostname.ext; fi`\[\033[0;32m\]:\[\e[1;35m\]\w\[\e[0;32m\]\[\e[1;36m\]$(__git_ps1) \[\033[0;32m\]\$ '
function md {
ssh md-${1}-i.trinityalps.org
}
alias vib='vi ~/.bashrc'
alias via='vi ~/.aliases'
alias app='cd ~/src/build'
alias vi='vim'
alias fetch='git fetch'
alias br='git br'
alias st='git st'
alias lol='git lol'
export PATH=${PATH}:/sbin:/usr/sbin:/usr/local/sbin
# Install pandoc
# pandoc converts markdown to html (and other formats)
sudo yum install -y pandoc
# Install lynx
sudo yum install -y lynx
# Add function to .bashrc
rmd () {
pandoc $1 | lynx -stdin
}
# Source it/log out and in and run free!
rmd path_to_markdown_file.md
# many/most of these are helpful within vim, not necessarily in .vimrc
# show current line number
:#
autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o
:set softtabstop=2 shiftwidth=2 expandtab
" turn on/off displaying end lines
:set list
:set nolist
" turn off auto-indent
:set paste
" turn on auto-indent
:set nopaste
color murphy
" After growing tired of NERDTree I found how to use netrw nicely
" use dir listing for netrw
let g:netrw_liststyle = 3
" turn off netrw banner
let g:netrw_banner = 0
" have netrw open file in vertical split
let g:netrw_browse_split = 4
let g:netrw_altv = 1
" set netrw width in percentage of page
let g:netrw_winsize = 15
" always place cursor at top of file when opening a file
:au! redhat BufReadPost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment