This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the bash category.
Last Updated: 2024-11-23
setup_database() {
createdb ...
}
# call it... note that is uses no parens
setup_database
if ! command -v xcode-select &> /dev/null; then
xcode-select --install
else
echo "-> Skipping xcode-select because it was already available."
fi
if ! test -f .env; then
echo "No .env file found in project root."
else
echo "ask colleage for env file"
fi
# not equal
if [[ $var -ne 0 ]]
echo "not equal 0"
else
echo "equal 0"
fi
reverse
# equal
if [[ $var -eq 0 ]]
echo "equal 0"
else
echo "not equal 0"
fi
string="abcd"
if [[ $string == *"cd"* ]]; then
echo "It's there!"
fi
Use !=
for the opposite.
# inside a script to wrap manage.py
python mysite/manage.py "$@"
Yep, for sure
# .env contain FOO=1
source .env
# $FOO is now available
Use slashes
gh pr list | \
cut -f 3 | \
xargs -I {} sh -c \
'echo Listing potential conflicts for branch "$1": && git diff HEAD...origin/$1 --name-only -- mobile-app && echo ' \
sh {}
Say you have this:
$ git diff --name-only HEAD...optimize-header-space-follow-up
mysite/static/react-app/src/api/savedSearches.ts
mysite/static/react-app/src/api/stores/filters.js
mysite/static/react-app/src/components/Common/Filters/CategoryFilter/definitions.ts
mysite/static/react-app/src/components/ContextTabs/ContextTabs.tsx
mysite/static/react-app/src/components/experimental/Breakpoint/Breakpoint.tsx
mysite/static/react-app/src/components/experimental/Chip/Chip.module.scss
mysite/static/react-app/src/components/experimental/Layouts/Grid.module.scss
mysite/static/react-app/src/constants/user.ts
mysite/static/react-app/src/pages/Project/SavedSearches/components/Header.tsx
mysite/static/react-app/src/pages/Project/SavedSearches/index.tsx
mysite/static/react-app/src/pages/Project/Search/helpers.ts
mysite/static/react-app/src/pages/SavedSearches/SavedSearches.tsx
mysite/static/react-app/src/pages/SavedSearches/components/Header.tsx
You can convert this to a single line with echo
(handles new lines, tabs, multiple spaces)
$ echo $(git diff --name-only HEAD...optimize-header-space-follow-up)
mysite/static/react-app/src/api/savedSearches.ts mysite/static/react-app/src/api/stores/filters.js mysite/static/react-app/src/components/Common/Filters/CategoryFilter/definitions.ts mysite/static/react-app/src/components/ContextTabs/ContextTabs.tsx mysite/static/react-app/src/components/experimental/Breakpoint/Breakpoint.tsx mysite/static/react-app/src/components/experimental/Chip/Chip.module.scss mysite/static/react-app/src/components/experimental/Layouts/Grid.module.scss mysite/static/react-app/src/constants/user.ts mysite/static/react-app/src/pages/Project/SavedSearches/components/Header.tsx mysite/static/react-app/src/pages/Project/SavedSearches/index.tsx mysite/static/react-app/src/pages/Project/Search/helpers.ts mysite/static/react-app/src/pages/SavedSearches/SavedSearches.tsx mysite/static/react-app/src/pages/SavedSearches/components/Header.tsx mysite/static/react-app/src/services/constants/misc.ts mysite/static/react-app/src/types/search.ts
Sub [[
and a subshell.
if [[ $(du -k mysite/static/react-app/build/bundle.js) -gt 3000 ]]; then
echo "File is too big"
echo
exit 2
else
echo "File is small enough"
exit 0
fi
Note that passing the internal bit to /dev/null will cause not output to top-shell, breaking things. if [[ $(du -k mysite/static/react-app/build/bundle.js 1>/dev/null) -gt 3000 ]]; then
# Declare an array. Parentheses. No commas
# Note that there must be no space before or after the equal sign
branches=(
change-us-region-to-nam-2453
dashbaord-date-range-filters-3170
persist-store-analysis-2413
validate-traits-2907
import-2-polish-2617
import-dam-sire-2901
)
# Use a for loop with syntax - `${array_name[@]}`
for branch in "${branches[@]}"
do
echo "Working on $branch"
done
for branch in "${branches[@]}"
do
# Use || continue. Otherwise, by default, it will try next command in the loop
command_that_might_fail || continue
echo "Working on $branch"
done
cat
instead of echo<<-
format for HereDoc and wrapping the delimiter in quotescat <<- "DELIMITER" > pyproject.toml
[tool.isort]
profile = "black"
skip_gitignore = true
line_length = 120
skip = ["site-packages"]
skip_glob = ["*/migrations/*"]
[tool.black]
line-length = 120
target-version = ['py311']
include = '\.pyi?$'
skip-string-normalization = true
exclude = '''
/(
| migrations
| site-packages
)/
'''
DELIMITER
pbpaste | tr "," "\n"
current_filename=$(basename "$0")