Posts tagged with “bash”

How to delete all bin/obj folders in a complex solution in GitBash

Sometimes we need to delete all the bin/obj folders in a solution to resolve issues in Rider or Visual Studio. I assume you already have your preferred method for this task, but I would like to share my approach here in case someone else is unaware of how to do it in GitBash or MSYS terminal:

#!/bin/bash

set -e

# Find and display directories to be deleted
find . \( -iname "bin" -o -iname "obj" \) -type d -print

# Prompt user for confirmation
read -p "The above folders are going to be deleted, are you sure? [Y/N]: " -n 1 -r
echo    # Move to a new line

# Delete directories if confirmed
if [[ $REPLY =~ ^[Yy]$ ]]; then
    find . \( -iname "bin" -o -iname "obj" \) -type d -exec rm -rfv {} +
    echo "Done"
else
    echo "No"
    exit 1
fi

Don't simple unset them afterward when using `shopt` change a setting

Sometimes we need to run shopt -s dotglob nullglob before moving files including dotfiles. So there's another question, do we need to set it back afterward? The most correct answer is

It's usually not clear if either dotglob or nullglob were already set before running shopt -s to set them. Thus, blindly un-setting them may not be the proper reset to do. Setting them in a subshell would leave the current shell's settings unchanged:

( shopt -s dotglob nullglob; mv ~/public/* ~/public_html/ )

Reference: Jeff Schaller's answer under this question

switch case in bash script

, change the first letter of the value of the variable to lowercase, ,, change the value of the whole variable to lower case.

$ test='HELLO'
$ echo $test
HELLO
$ echo ${test,}
hELLO
$ echo ${test,,}
hello
$ test=${test,,}
$ echo ${test^}
Hello
$ echo ${test^^}
HELLO

A bash script to keep two git branch identical.

#!/bin/bash

git push
currentbranch=`git branch --show-current`
if [ "${currentbranch}" == "local" ]; then
   git checkout develop
   git merge local
   git push
elif [ "${currentbranch}" == "develop" ]; then
   git checkout local
   git merge develop
   git push
fi
git checkout $currentbranch

Don't ask why I wrote this foolish script. I have to do it 😀

Database structure backup & Data dictionary automatically generation

Two bash script achieved the goals in this subject.

1. Exporting data dictionary

cat utils/exportDbDictionary 
if [ $# != 2 ] ; then
    echo "USAGE: $0 dbname tablename"
    echo " e.g.: $0 IDServer AspNetUsers"
    exit 1;
fi

document_path=/YourProjectName/documents/database

[ -d $document_path/$1 ] || mkdir -p $document_path/$1
mysqlshow $1 $2 | sed 's/+/|/g' | sed '1,2d' | sed '$d' | awk -F"[|]" '{print $2"|"$3"|"$5"|"$6"|"$7"|"$10}' | sed 's/ *$//g' > $document_path/$1/$2.md

People should always add comments to their field definitions. As soon as the definition of a table has been done, run this script could automatically generate a markdown table with the name of tableName.md.

2. export database structure without auto_incrment= statements

cat utils/backupDbStructure 
backupFile=/YourProjectName/documents/database/databaseStructure.sql
mysqldump -d --databases YourDB1 YourDB2 YourDB3 | sed 's/ AUTO_INCREMENT=[0-9]*//g' > $backupFile

When you made some changes to your database, run backupDbStruture script will automatically generate the latest table structure into one file. You could commit it later with your code change together.