Last active
May 22, 2021 23:52
-
-
Save gwsu2008/3e24d1dc96d38a387595e150d63b9dbc to your computer and use it in GitHub Desktop.
Useful Bash utils and tips
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Useful Bash command and tips ### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# bash array | |
${arr[*]} # All of the items in the array | |
${!arr[*]} # All of the indexes in the array | |
${#arr[*]} # Number of items in the array | |
${#arr[0]} # Length of item zero |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
usage() { echo "Usage: $0 [-s <45|90>] [-p <string>]" 1>&2; exit 1; } | |
while getopts ":s:p:" o; do | |
case "${o}" in | |
s) s=${OPTARG} | |
((s == 45 || s == 90)) || usage | |
;; | |
p) p=${OPTARG} | |
;; | |
*) usage | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
if [ -z "${s}" ] || [ -z "${p}" ]; then | |
usage | |
fi | |
echo "s = ${s} and p = ${p}" | |
$# = total number of arguments / $1 is the first argument / $0 is the script itself. | |
!# = last argument |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# bash - IFS | |
for loop splits when it sees any whitespace like space, tab, or newline. So, you should use IFS (Internal Field Separator): | |
IFS=$'\n' # make newlines the only separator | |
for j in $(cat ./file_wget_med) | |
do | |
echo "$j" | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# default shell variable value | |
The := syntax | |
echo ${granslam=Maria Sharapova} NOTE: will not work with parameter. | |
echo ${granslam:-Maria Sharapova} | |
echo ${granslam:=Maria Sharapova} | |
if $granslam is empty, set assign Maria Sharapova | |
u=${1:-root} if “$1” not set, then use root | |
${varName:?Error varName is not defined or is empty} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How to detect if a script is being sourced | |
[[ $_ != $0 ]] && echo "Script is being sourced" || echo "Script is a subshell" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# download JAVA from oracle | |
wget --no-check-certificate -c --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u162-b12/0da788060d494f5095bf8624735fa2f1/jdk-8u162-linux-x64.rpm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Execute variable as command | |
TIME_CMD='date +\%b" "\%d" "\%X" "\%Y' | |
echo "`eval $CMD` - INFO" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get filename without extension | |
${FILENAME%.*} => bash_hackers.txt | |
# get file extension | |
${FILENAME##*.} => bash_hackers.txt | |
# get directory name | |
${PATHNAME%/*} => /hom/bash/bash_hackers.txt | |
# get filename | |
${PATHNAME##*/} => /hom/bash/bash_hackers.txt | |
# get script name without basename | |
_self="${0##*/}"; echo "$_self is called" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get my IP address | |
curl https://ifconfig.co/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# test function exists | |
isFunction() { [[ "$(declare -Ff "$1")" ]]; } | |
Usage: | |
isFunction some_name && echo yes || echo no |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# mkdir with mode | |
_mkdir() { | |
local d="$1" # get dir name | |
local p=${2:-0755} # get permission, set default to 0755 | |
[ $# -eq 0 ] && { echo "$0: dirname"; return; } | |
[ ! -d "$d" ] && mkdir -m $p -p "$d" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Parse HREF URL | |
shopt -s nocasematch #Dont care about the character case | |
text='<a href="http://www.google.com/here2;i=!mfo1iu489fn1o2jlk21m4098mdoi">"test link"</a><br>' | |
regex='(<a\ +href=\")([^\"]+)(\">)' | |
[[ $text =~ $regex ]] && echo ${BASH_REMATCH[2]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pipe status | |
<command> | tee out.txt; test ${PIPESTATUS[0]} -eq 0 | |
echo ${PIPESTATUS[*}} all status in commands |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# remove all spaces | |
echo " This is a test" | sed -e 's/^[ \t]*//' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# remove first 2 columns using cut command | |
You can do it with cut: | |
cut -d " " -f 3- input_filename > output_filename | |
Explanation: | |
cut: invoke the cut command | |
-d " ": use a single space as the delimiter (cut uses TAB by default) | |
-f: specify fields to keep | |
3-: all the fields starting with field 3 | |
input_filename: use this file as the input | |
> output_filename: write the output to this file. | |
awk '{$1=""; $2=""; sub(" ", " "); print}' input_filename > output_filename | |
awk: invoke the awk command | |
$1=""; $2="";: set field 1 and 2 to the empty string | |
sub(...);: clean up the output fields because field 1 & 2 will still be delimited by " " | |
print: print the modified line | |
input_filename > output_filename: same as above. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# remove non-printable chars | |
JENKINS_VER=$(tr -dc '[[:print:]]' <<< "${JENKINS_VER}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# rename all *.txt filenames to lowercase | |
for file in *.txt; do | |
/bin/mv "$file" "${file,,}" | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# terminate a screen session | |
screen -X -S [session # you want to kill] kill | |
# start screen session | |
/usr/bin/screen -d -m -S android_emu ./emulator -avd ${DEVICE_NAME} -gpu host -wipe-data -no-boot-anim -no-audio -noaudio -netfast -timezone America/Los_Angeles >> emulator.log 2>&1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# If variable is set outside of this script, use what is set instead to allow overrides of param store | |
function set_environment_variable { | |
local ENV_VAR=$1 | |
if test -z ${!ENV_VAR}; | |
then | |
CMD="export $1=$2" | |
eval $CMD | |
else | |
echo "${ENV_VAR} is already set in environment" | |
fi | |
} | |
function get_db_params { | |
pattern='^([[:print:]]+)://([[:print:]]+):([[:digit:]]+)/([[:print:]]+)' | |
if [[ $1 =~ $pattern ]]; then | |
echo $BASH_REMATCH | |
proto=${BASH_REMATCH[1]} | |
host=${BASH_REMATCH[2]} | |
port=${BASH_REMATCH[3]} | |
dbname=${BASH_REMATCH[4]} | |
set_environment_variable PGHOST $host | |
set_environment_variable PGPORT $port | |
set_environment_variable POSTGRES_DB $dbname | |
else | |
echo "ERROR failed to parse url - $1" | |
exit | |
fi | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
${parameter:-defaultValue} | |
${parameter:=defaultValue} | |
${parameter:?”ErrorMsg”} | |
${#var} - length of string | |
${var%pattern} | |
${var#Pattern} - Remove from $var the shortest part of $Pattern that matches the front end | |
${var##Pattern} - Remove from $var the longest part of $Pattern that matches the front end | |
${var%Pattern} - Remove from $var the shortest part of $Pattern that matches the back end | |
${var%%Pattern} - Remove from $var the longest part of $Pattern that matches the back end | |
${var:pos} - Variable var expanded, starting from offset pos. | |
${var:pos:len} - Expansion to a max of len characters of variable var, from offset pos | |
${var/Pattern/Replacement} - First match of Pattern, within var replaced with Replacement. | |
${var//Pattern/Replacement} - Global replacement. All matches of Pattern, within var replaced. | |
${var/#Pattern/Replacement} - If prefix of var matches Pattern, then substitute Replacement for Pattern. | |
${var/%Pattern/Replacement} - If suffix of var matches Pattern, then substitute Replacement for Pattern. | |
${!varprefix*}, ${!varprefix@} - Matches names of all previously declared variables beginning with varprefix. | |
numeric validation = [[ $var == ${var//[^0-9]/} ]] || echo “Not numeric” |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# find N days ago. | |
# yesterday | |
YES_DAT=$(date --date=' 1 days ago' '+%Y%d%m') | |
# any day | |
ANY_YES_DAT=$(date --date=' $1 days ago' '+%Y%d%m') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo "$(whoami):$(id -g -n $(whoami))" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment