Search Windows and Linux Networking

Tuesday, March 18, 2014

How to print or display colorful text on screen in terminal or in bash scripting with echo command

How to print or display colorful text on screen in terminal    


 It is always good practice to show your script related information in colorful text in terminal to highlight or attention to the user. for example you can give output of script test as successful or fail in Green and red color, you can also display error message in Red color so user can attention for result\Output.

There are many method to do this for example with help of sed , awk , echo and may more. I will show you how to display text in colorful output with the help of echo command.

suppose you wanted to display only Hello world in Red color. you can do this with echo like follow

echo -e "\e[0;31m Hello World \e[om"

or
echo -e  "\033[31m Hello World \033[0m"
Hello World

suppose you wanted to display only Hello in Red color and World in Blue then your command look like follow

echo -e "\e[0;31m Hello \e[0;34m World \e[0m"
or
echo -e  "\033[31m Hello \033[34m World \033[0m"
Hello World


For Bold text Hello  World command looks:

echo -e "\e[1m Hello World\e[0m"
or
echo -e "\033[1m Hello World\033[0m"
Hello World

to underline the Hello world command is as follow:

echo -e "\e[4m Hello World\e[0m"
 Hello World
echo -e "\e[4;31m Hello World\e[0m"
 Hello World

Following are some other color code you wanted to use in your script.


Black 0;30
Red 0;31
Green 0;32
Orange 0;33
Yellow   1;33
Blue 0;34
Purple 0;35
Cyan 0;36
Light Gray 0;37



 

Friday, March 7, 2014

How to check File or Folder exist in Bash Scripting

How to check file of folder exist in Bash scripting.


When we write any bash scripting many time, we wanted to performing any task base on validating  for file or folder is exist or not . There are many ways to check for file or folder in Bash Scripting. In following example we will see how to check file or folder exist or not to do some task.


Suppose you wanted to check if folder name Backup exist in path /home/Backup and if it does not exist then create new folder as Backup in /home directory and if it exist the display the massage saying Backup folder exist. and you also wanted to check if file name backup.tar exist in path /home/Backup directory and if it doest not exist then create tar file as backup.tar in /home/Backup directory and if it exist then display massage that saying backup.tar file exist.


!#/bin/bash

# SCRIPT: backup.sh
# AUTHOR: Your Name.
# CONTACT : Your Contact details.
# DATE: 06th Mar, 2014
# REV: 1.0 A

# PURPOSE: To check Backup folder and backup.tar file exist or not. If not exist then create  Backup directory in path /home and backup.tar file in /home/Backup direcoty.

# 1st Way to check folder exist.

test -d /home/Backup
if [ "$?" -eq 0 ]
then
   echo "\"/Backup\" Directory exist in /home"
else
   echo "\"/Backup\" Directory does not exist in /home"
   echo "Creating /\"Backup\" Directory in \"/home\" Directory."
   mkdir /home/Backup
fi


# 2nd Way to check folder exist.

if test -d /home/Backup
then
   echo "\"/Backup\" Directory exist in /home"
else
   echo "\"/Backup\" Directory does not exist in /home"
   echo "Creating /\"Backup\" Directory in \"/home\" Directory."
   mkdir /home/Backup
fi

# 3rd Way to check folder exist.

if [ -d /home/Backup ]
then
   echo "\"/Backup\" Directory exist in /home"
else
   echo "\"/Backup\" Directory does not exist in /home"
   echo "Creating /\"Backup\" Directory in \"/home\" Directory."
   mkdir /home/Backup
fi

# There are also many other ways to check folder. for like using "ls -ld /home/Backup" command .
# To check file method is same as above just replace -d option with -f if you wanted to check file and if you wanted to check link replace with -l option.

# 1st Way to check file exist.

test -f /home/Backup/backup.tar
if [ "$?" -eq 0 ]
then
   echo "\"backup.tar\" file exist in /home/Backup Directory"
else
   echo "\"backup.tar\" file does not exist in \"/home/Backup\" Directory"
   echo "Creating \"backup.tar\" file in \"/home/Backup\" Directory."
   cd /
   tar -cvpf /home/Backup/backup.tar /home/sandeep
fi

# 2nd Way to check file exist.

if test -f /home/Backup/backup.tar
then
   echo "\"backup.tar\" file exist in /home/Backup Directory"
else
   echo "\"backup.tar\" file does not exist in \"/home/Backup\" Directory"
   echo "Creating \"backup.tar\" file in \"/home/Backup\" Directory."
   cd /
   tar -cvpf /home/Backup/backup.tar /home/sandeep
fi

# 3rd way to check file exist.

if [ -f /home/Backup/backup.tar ]
then
   echo "\"backup.tar\" file exist in /home/Backup Directory"
else
   echo "\"backup.tar\" file does not exist in \"/home/Backup\" Directory"
   echo "Creating \"backup.tar\" file in \"/home/Backup\" Directory."
   cd /
   tar -cvpf /home/Backup/backup.tar /home/sandeep
fi


# END

you can use any one method which is comfortable to you .