Linux – Bash Shell Scripting Examples

Bash scripting with some examples that can be used in real time scenarios. Here we go…

Creating Directory By Checking Existence


echo "Enter New Directory Name: "
read new_dir
if [ -d "$new_dir" ]
echo "$new_dir Directory Exist"
mkdir $new_dir
echo "$new_dir Directory Created"

Deleting Directory By Checking Existence


echo "Enter Directory Name To Be Deleted: "
read existing_dir
if [ -d "$existing_dir" ]
rm -rf $existing_dir
echo "$existing_dir Directory Exist - All Of It's Contents Deleted"
echo "$existing_dir Directory Does Not Exist"

Reading a File By Checking Existence


echo "Enter File Name: "
read filename
if [ -f "$filename" ]; 
echo "File Exists - Reading a File"
nano "$filename"
echo "File Does Not Exist"

Deleting a File By Checking Existence


echo "Enter File Name To Be Deleted: "
read filename
if [ -f "$filename" ]; 
echo "File Exists - Deleting File"
rm -i $filename
echo "File Does Not Exist"

Append to File By Checking Existence


echo "Enter File Name To Be Appended: "
read filename
if [ -f "$filename" ]; 
printf "\n"
echo "Before Appending The File"
cat $filename
printf "\n"
echo "File Exists - Reading Data To Be Appended: "
read appending_string
echo $appending_string >> $filename
printf "\n"
echo "After Appending The Data To The File"
cat $filename
echo "File Does Not Exist"

Parsing Date and Time


year=$(date +"%Y")
echo "Year Is "$year
echo "12 Hr Clock "$(date +"%r")
echo "24 Hr Clock "$(date)
echo "Current Date is: "$(date +"%d-%m-%Y")
echo "Current Time is: "$(date +"%H:%M:%S")
printf "\n"
echo "File Name Parsing With Date and Time"
now=$(date +"%d_%m_%Y_%H_%M_%S")
echo $file_name

Checking Disk Space


# set alert level 90% is default
# Exclude list of unwanted monitoring, if several partitions then use "|" to separate the partitions.
# An example: exclude_list="/dev/hdd1|/dev/hdc5"
# ================================================================

function main_prog() {
while read output;
  used_percent=$(echo $output | awk '{ print $2}' | cut -d'%' -f1)
  partition=$(echo $output | awk '{print $1}')
  if [ "$used_percent" -ge "$alert" ]; 
     echo "Partition "$partition " running out of space, using" $used_percent%" on server $(hostname), $(date)" 
if [ "$exclude_list" != "" ]; 
  df -H | grep -vE "^Filesystem|tmpfs|cdrom|${exclude_list}" | awk '{print $1 " " $5}' | main_prog
  df -H | grep -vE "^Filesystem|tmpfs|cdrom" | awk '{print $5 " " $6}' | main_prog

Checking Server Utilization


# Modified Existing Code
# Credits -
now=$(date +"%d_%m_%Y_%H_%M_%S")
exec 1> /home/sathiyan/Templates/$file_name
echo "Up Time:"
echo "Currently Connected:"
echo "--------------------"
echo "Last Logins:"
last -a |head -3
echo "--------------------"
echo "Disk and Memory Usage:"
df -h | xargs | awk '{print "Free/total disk: " $11 " / " $9}'
free -m | xargs | awk '{print "Free/total memory: " $17 " / " $8 " MB"}'
echo "--------------------"
start_log=$(head -1 /var/log/messages |cut -c 1-12)
oom=$(grep -ci kill /var/log/messages)
echo -n "OOM Errors Since $start_log :" $oom
echo ""
echo "--------------------"
echo "Utilization and Most Expensive Processes:"
top -b |head -3
top -b |head -10 |tail -4
echo "--------------------"
echo "Open TCP Ports:"
nmap -p- -T4
echo "--------------------"
echo "Current Connections:"
ss -s
echo "--------------------"
echo "Processes:"
ps auxf --width=200
echo "--------------------"
echo "vmstat:"
vmstat 1 5


Linux – Basic Bash Shell Scripting

Shell accept command from you (via keyboard) and execute them. But if you use command one by one (sequence of ‘n’ number of commands) , the you can store this sequence of command to text file and tell the shell to execute this text file instead of entering the commands. This is know as shell script.

Types (I use)

echo "foo"
# =================
echo "foo"

Execution Of Shell Script

$ bash
$ bash <input>
$ sh

Example 1 – Echo, Read, Comment

# This is Single Line Comment
: 'This 
# Echo & Read
echo "Enter Your Name: "
read name
echo "Welcome $name to learn shell scripting"

Example 2 – Conditional, Looping, Logical

If, Else, Elif Condition

# Checking a Number Is Positive, Negative or Zero
read -p "Enter a number : " n
if [ $n -gt 0 ]; 
  echo "$n is a positive."
elif [ $n -lt 0 ]
  echo "$n is a negative."
elif [ $n -eq 0 ]
  echo "$n is zero number."
  echo "Oops! $n is not a number."

For Loop

# For Loop Using Decremental Counter
for (( counter=10; counter>0; counter-- ))
echo -n "$counter "
printf "\n" 

# For Loop Using Incremental Counter
for (( c=1; c<=5; c++ ))
echo "Welcome $c times"
printf "\n" 

# For Loop Over Sequence
for i in 1 2 3 4 5
echo "Waving $i times"

While Loop

# While Loop Until $n Equals 5

while [ $n -le 5 ]
echo "Welcome $n times."
n=$(( n+1 )) 


# Using Case and Getting Input Arguments
# -z $1 means input is null, -n $1 means input is not null
if [ -z $1 ]
  rental="No Valid Data Given"
elif [ -n $1 ]
case $rental in
   "car") echo "For $rental Rs.20 per k/m";;
   "van") echo "For $rental Rs.10 per k/m";;
   "jeep") echo "For $rental Rs.5 per k/m";;
   "bicycle") echo "For $rental 20 paisa per k/m";;
   *) echo "Sorry, I can not get a $rental for you";;

Logical – AND(&&), OR(||), NOT(!)


[ 5 == 5 ] && echo "Yes" || echo "No"
[ 5 == 15 ] && echo "Yes" || echo "No"
[ 5 != 10 ] && echo "Yes" || echo "No"

Example 3 – Arguments or Positional Parameters


echo "File Name: $0"
echo "First Parameter : $1"
echo "Second Parameter : $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"

# Execution
$ bash Sathiyan Blog

Example 4 – Function


rectangle_area() {
area=$(($1 * $2))
echo "Area is : $area"

echo "Length: "
read length
echo "Width: "
read width

rectangle_area $length $width


Linux – Basic Commands

A shell is a program that receives commands from the user and gives it to the OS to process, and it shows the output. Linux’s shell is its main part. Its distros come in GUI (graphical user interface), but basically, Linux has a CLI (command line interface). In this post, we are going to cover some of the basic commands that we use in the Linux terminal.

# Manual Page
$ man <command>
$ man cp

# Disk Free
$ df -lah

# History
$ history

# Job Scheduler
$ crontab -e

# Kill Process
$ ps ax | grep 'process_name'
$ kill -9 <pid>

# String Variable to Standard Output
$ echo "Hello World!"

# Read
$ read name
$ echo $name

# Cat (Mostly I use nano for file editing)
$ cat > file_1 # Create File and Enter Data 
$ cat file_1 # Access the File
$ cat > file_2
$ cat file_1 file_2 > file_3 # Combining Two Files 
$ cat file_3

# Package Manager (apt, apt-get, pip)
$ sudo apt update
$ sudo apt-get install <package_name>
$ pip install <package_name>

# Copy
$ cp /home/sathiyan/Template/file_1 /home/sathiyan/Public/ # Moving "file_1" From Template Folder to Public Folder

# Secure Copy (Copy files and directories between two locations)
$ scp file_3 remote_username@remote_ip:/remote/directory # Requires an ssh key or password to authenticate on the remote system

# Move (Renaming File or Directory)
$ mv file_3 file_1_2 # Renaming "file_3" as "file_1_2"
# Date
$ date # Display "Wed Mar 19 20:47:42 IST 2019"

# Difference (File or Directory)
$ diff file_1 file_1_2

# Link (Kind of Shortcut)
$ ln
$ ln -s # Symbolic Link
$ ln -s file_1_2 file_3

# List (Files and Directory)
$ ls
$ ls -lah # With Permissions, Owner, Group, Size, Accessed Date Time (Along With Hidden Files)

# Gzip (Compress)
$ gzip file_1_2
$ pg_dump -U sathiyan odoo_12 -f odoo_12.dump | gzip -9 odoo_12.gz

# Gunzip (Extract from Gzip Compress File)
$ gunzip file_1_2.gz
$ gunzip -9 odoo_12.gz

# Ping (Connectivity Check)
$ ping <dns_or_ip>

# Mount (Mounting Windows Shared Folder in Linux)
$ sudo mount -t cifs //remote_directory_path /mounting_directory_path -o username=<username>,password=<password>

# Host Name
$ hostname

# IP Address Check
$ ip addr

# Present Working Directory
$ pwd

# Who Am I
$ whoami 

# Getfacl - Getting ACL of file or directory  
$ getfacl file_1

# Setfacl - Setting up ACL
$ setfacl -m [d:o:perms] filename
$ setfacl -m "u:sathiyan:rwx" file_1

# Change Mod
## u=user, g=group, o=others, r=read=4, w=write=2, x=execute=1
$ chmod u=rwx, g=rw, o=r file_1
$ chmod 764 file_1
$ chmod -R 754 Templates/ # Change the permission of file/sub_folders inside Templates direcotry.

# Change Ownership
$ chown <username> file_1

# Processor Activity
$ top

# Current Directory
$ cd # To the home directory
$ cd .. # Directory One Level Up

# Make Directory
$ mkdir linux_commands

# Delete Directory
$ rmdir linux_commands
$ rmdir -rf linux_commands # Delete directory and its contents

# Display All Process
$ ps ax
$ ps -ef

# SSH - Remotely accessing and running commands on a remote machine
$ ssh remote_username@remote_ip

# Running As Root
$ sudo -i # As Root User
$ sudo <command> # In Current User Home

# System Up Time
$ uptime

# Rsync - Transfer just the differences between two sets of files
$ rsync -r <source> <destination>

# Switch User
$ su <username>

# Tail - Display the last 10 lines by default of a file
$ tail /var/log/auth.log
$ tail -n 50 /var/log/auth.log # Changing no. of lines displayed
$ tail -f /var/log/auth.log # Streaming output of a changing file

# Last
$ last # Last logins of user

# Grep
$ grep [options] pattern [files]
$ grep -i "hEllo" file_2

# Nohup - It tells the Linux system not to stop 
$ nohup sh &

# Awk
$ awk '{print $1}' file_1 # Print first word of each line
$ awk '{$3="a"; print $0}' file_1 # Replace 3rd word as "a" and print full line.

# Less
$ cat file_1.txt | less

# More
$ cat file_1.txt | more

# Locate (Search All)
$ updatedb
$ locate file_1.txt

# Find (I use find, to search in pwd)
$ find file_1

# Will be Updated with more commands from time to time....


Odoo 12 With PyCharm Install – Ubuntu 18.04 LTS

Step 1

  • Run following commands
sudo apt-get update
sudo apt-get -y upgrade

Install Python Dependencies

sudo apt install git python3-pip build-essential wget python3-dev python3-venv python3-wheel libxslt-dev libzip-dev libldap2-dev libsasl2-dev python3-setuptools node-less
pip3 install Babel decorator docutils ebaysdk feedparser gevent greenlet html2text Jinja2 lxml Mako MarkupSafe mock num2words ofxparse passlib Pillow psutil psycogreen psycopg2 pydot pyparsing PyPDF2 pyserial python-dateutil python-openid pytz pyusb PyYAML qrcode reportlab requests six suds-jurko vatnumber vobject Werkzeug XlsxWriter xlwt xlrd

Install Odoo Web Dependencies

sudo apt-get install -y npm
sudo ln -s /usr/bin/nodejs /usr/bin/node
sudo npm install -g less less-plugin-clean-css
sudo apt-get install node-less
sudo python3 -m pip install libass

Install wkhtmltopdf

sudo wget
sudo dpkg -i wkhtmltox_0.12.1.3-1~bionic_amd64.deb
sudo apt-get install -f
sudo ln -s /usr/local/bin/wkhtmltopdf /usr/bin
sudo ln -s /usr/local/bin/wkhtmltoimage /usr/bin

Install & Configure Postgres

sudo apt -y install postgresql postgresql-contrib phppgadmin
su – postgres
\password postgres
# Enter the password for postgres user
su – postgres
createuser –interactive <username>
# Make this new user a superuser.
# Create the username with care this user will be used in Odoo “odoo_12.conf” file.
Connection Settings
cd /etc/postgresql/10/main
sudo nano postgresql.conf
# Find and change value of listen_addresses as shown below, 
listen_addresses = '*'
Configure Apache Web Server
cd /etc/apache2/conf-available/
sudo nano phppgadmin.conf
Find the below content

# Only allow connections from localhost:
Require local
And change as it shown below,
# Only allow connections from localhost:
# Require local
Require all granted
Configure phpPgAdmin
cd /etc/phppgadmin/
sudo nano
$conf['extra_login_security'] = true; 
# Find and Change the above line as shown below,
$conf['extra_login_security'] = false;
Re-start Apache & Postgres
systemctl restart postgresql
systemctl restart apache2

Now access phpPgAdmin with your browser http://localhost/phppgadmin

Step 2

  • Download Pycharm Community Edition from ”Ubuntu Software Center” or download the source and install.
  • Clone Odoo source code from git hub, this has to be done in PyCharm.

Step 3

  • Odoo configuration file need to be created in side odoo folder.
  • Note: Here I clone Odoo source code to odoo folder.
:admin_passwd = admin
db_host = localhost
db_port = 5432
db_user = odoo_user_db
db_password = odoo
xmlrpc_port = 8069
addons_path = home/sathiyan/PycharmProjects/odoo/addons,/ home/sathiyan/PycharmProjects/odoo/odoo/addons

Step 4

In PyCharm menu Run –> Edit Configurations, click + on the top left to create a new configuration and choose Python.

And follow the setting as shown in the below screenshot,

Beauty of this you can run many instance of any version of Odoo with any number of addons. You can update all selected module in “Parameters” field. And integration git will come in handy.