Skip to content

Instantly share code, notes, and snippets.

@u5-03
Last active July 26, 2023 16:31
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save u5-03/8d83100e25db3984e61e99831d6273aa to your computer and use it in GitHub Desktop.
shell memo
# Check if file exist
if [ ! -e ${FILE_PATH ]; then
echo "File not exist!"
else
echo "File exist!"
fi
if [ -f $FILE_PATH ]; then
echo 'File exists!'
fi
# Check if directory exist
if [ ! -d ${DIRECTORY_PATH} ]; then
echo "No directory, create new directory."
mkdir -p ${DIRECTORY_PATH}/
fi
# Check if variable is empty
if [ -n "$VARIABLE" ]; then
echo "$VARIABLE" is not empty.
fi
if [ -z "$VARIABLE" ]; then
echo "$VARIABLE" is empty.
fi
# Check if text contains something
if [[ "$TEXT" == *SEARCH_TEXT* ]]; then
# do something
fi
# Switch sentense
case "${TEXT}" in
"A")
RESULT="A";;
"B")
RESULT="B";;
*)
break;;
esac
# Download shell file from GitHub public repository and run it
sudo curl -fsSL https://raw.githubusercontent.com/${GITHUB_USER_ID}/${REPOSITORY_NAME}/${BRANCH_NAME}/${PATH_TO_FILE} | sh
# Download shell file from GitHub private repository with GitHub personal token and run it
## Ref: https://qiita.com/kymmt90/items/0e45d4b69d6e4d5cd24a
curl -H "Authorization: token ${PERSONAL_ACCESS_TOKEN}" https://api.github.com/repos/${GITHUB_USER_ID}/${BRANCH_NAME}/contents/${PATH_TO_FILE}?ref="${BRANCH_NAME or TAG_NAME}" | jq -r .content | base64 --decode > ${FILE_PATH_TO_SAVE}
sh ${FILE_PATH}
# Copy directory
cp -rfp ${DIRECTORY_PATH_FROM} ${DIRECTORY_PATH_TO}
# Change directory name
mv ${DIRECTORY_PATH_FROM} ${DIRECTORY_PATH_TO}/
#Extract word from file
##e.g. file name is 'version'
## version=1.0.0
WORD=`cat './version'|grep "version="|sed "s/version=//"`
## echo $WORD # 1.0.0
# Create random text
## create random text from ${MIN_LENGTH} to ${MAX_LENGTH}
TEXT_LENGTH=`jot -r 1 ${MIN_LENGTH} ${MAX_LENGTH}`
## Ref: https://github.com/projectzeroindia/CVE-2019-19781/issues/3#issue-550396570
export LC_CTYPE=C
## Ref: https://gist.github.com/earthgecko/3089509
export RANDOM_KEYWORD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $TEXT_LENGTH | head -n 1)
# Create Random number digits(4 digits)
RANDOM_NUM=$RANDOM
BUILD_NUM_SUFFIX=$(printf "%04d" "${RANDOM_NUM}")
# Create file from String variable
## create new empty file
touch ${FILE_PATH}
## add text to new file
echo $TEXT > $FILE_PATH
# File path
FILE_PATH=$(cd $(dirname $0) && pwd)
# Rename file
mv $FILE_NAME_BEFORE $FILE_NAME_AFTER
# Merge array
ARRAY_0=(
"ITEM0"
"ITEM1"
)
ARRAY_1=(
"ITEM2"
)
MERGED_ARRAY=(${ARRAY_0[*]} ${ARRAY_1[*]})
# For loop
for item in ${array[@]}; do
echo $item
done
# Remove directory
rm -rf ${DIRECTORY_PATH}
# After error happen, exit 1
sh sript.sh||exit 1
# Zip directory
zip ${ZIP_FILE_NAME_PATH} -r ${DIRECTORY_PATH}
# Untracked and tracked files list. Used when running swiftlint and so on.
## all git-untracked files of extension `.swift`
untracked_files=$(git ls-files --others --exclude-standard "*/*.swift")
## all tracked files of extension `.swift`
diff_files=$(git diff --diff-filter=d --name-only -- "*.swift" -- ':(exclude)${EXCLUDE_FILE_PATH}')
all_files=("${untracked_files[@]} ${diff_files[@]}")
# Run script only when M1 regardless of rosetta settings
arch -arm64 uname -m
if [[ $? == 0 ]]; then
echo 'This Mac is M1!'
fi
# Check command exist
if ! command -v ${COMMAND} >/dev/null; then
echo "The commnad is not installed!"
exit 1
fi
# Check if executed in CI
if [ -n "$CI" ] && "$CI" ; then
echo "CI"
else
echo "Not CI"
fi
# Flutter app version
PLUGIN_VERSION=v`cat 'pubspec.yaml'|grep 'version: '|sed 's/version: //'`
# Check if tag is already added and if not, push automatically.
FILE_PATH=$(cd $(dirname $0) && pwd)
VERSION=`cat "${FILE_PATH}/../../Configs/Version.xcconfig"|grep "MARKETING_VERSION="|sed "s/MARKETING_VERSION=//"`
TAG_NAME=$VERSION
# Check if the tag already exists in the current commit
if git tag --contains HEAD | grep -q "$TAG_NAME"; then
echo "The current commit already has the tag."
else
# Check if the tag already exists in the commit history
if git show-ref --tags | grep -q "$TAG_NAME"; then
# Delete existed tag
git tag -d $TAG_NAME
git push origin :refs/tags/$TAG_NAME
echo "Delete the tag of ${VERSION}."
fi
# Add new tag
git tag $TAG_NAME
git push origin $TAG_NAME
echo "Add tag ${VERSION}."
fi
# Current Git branch name
BRANCH_NAME=`git rev-parse --abbrev-ref HEAD`
# Get ID from Branch name(branch name is e.g. feature/ticket-1294/ticket-sub-1295-2)
ID=`echo $BRANCH_NAME | sed -E 's/.*-([0-9]{3,}).*/\1/'`
echo $ID # This shows `1295`
# Replace some text in file(Mac)
# `VERSION=1234` -> `VERSION=1235`
# Ref: http://to-developer.com/blog/?p=1034
NEW_VERSION=1235
FILE_PATH=hoge
sed -i "" -e "s/VERSION=.*/VERSION=${NEW_VERSION}/g" $FILE_PATH
# Rename all files containing `xyz` to `abc`
# $f is for loop iteration item(file path).
# "$(echo "$f" | sed s/xyz/abc/)" means file name after rename
for f in **/*xyz*.*; do mv "$f" "$(echo "$f" | sed s/xyz/abc/)"; done;
# Script file directory
SCRIPT_FILE_DIRECTORY=$(cd $(dirname $0);pwd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment