Use colors in the shell output
First asign color variables
red='\e[0;31m'
RED='\e[1;31m'
blue='\e[0;34m'
BLUE='\e[1;34m'
cyan='\e[0;36m'
CYAN='\e[1;36m'
NC='\e[0m' # No Color
Color variables with echo
echo ""
echo "Text example with some colors"
echo -e "${RED} Red text ${NC}"
echo -e "${BLUE} Blue text :${NC}"
- Note: echo must be used with the -e parameter to print colors.
- Note: if you don't end the line with ${NC} (No color), all the following echos will keep the last color set
Pause the script, wait for user input
function pause(){
read -p "$*"
}
echo ""
pause "Press a key to continue..."
Assign first and second user input to variables
VARIABLE_USER_INPUT1=$1
VARIABLE_USER_INPUT2=$2
Check if a folder doesn't exists in the system
if [ ! -d "$DIR_VARIABLE" ]; then
echo "${DIR_VARIABLE} doesn't exists"
fi
Check if a file exists in the system
if [ -f "filename.txt" ]; then
echo "filename.txt exists"
fi
Check if the user has executed the script with at least one parameter
if [ $# -ne 1 ]; then
echo -e "${RED} User hasn't introduced a parameter ${NC}"
else
echo -e "${CYAN} User has introduced a parameter ${NC}"
fi
Arrays in bash
DIR_ARRAY=('folder1' 'folder2' 'folder3')
Loop an array in bash
for DIR in "${DIR_ARRAY[@]}"
do
:
echo "Dirname: ${DIR}"
done