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

#!/bin/bash

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

Deleting Directory By Checking Existence

#!/bin/bash

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

Reading a File By Checking Existence

#!/bin/bash

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

Deleting a File By Checking Existence

#!/bin/bash

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

Append to File By Checking Existence

#!/bin/bash

echo "Enter File Name To Be Appended: "
read filename
if [ -f "$filename" ]; 
then
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
else
echo "File Does Not Exist"
fi

Parsing Date and Time

#!/bin/bash

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")
file_name="diskusage_$now.txt"
echo $file_name

Checking Disk Space

#!/bin/bash

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

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

Checking Server Utilization

#!/bin/bash

# Modified Existing Code
# Credits - https://www.tecmint.com/basic-shell-programming-part-ii/
now=$(date +"%d_%m_%Y_%H_%M_%S")
file_name="server_utilization_$now.txt"
exec 1> /home/sathiyan/Templates/$file_name
date;
echo "Up Time:"
uptime
echo "Currently Connected:"
w
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
echo
top -b |head -10 |tail -4
echo "--------------------"
echo "Open TCP Ports:"
nmap -p- -T4 127.0.0.1
echo "--------------------"
echo "Current Connections:"
ss -s
echo "--------------------"
echo "Processes:"
ps auxf --width=200
echo "--------------------"
echo "vmstat:"
vmstat 1 5

Reference

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)

#!/bin/bash
echo "foo"
# =================
#!/bin/sh
echo "foo"

Execution Of Shell Script

$ bash script.sh
$ bash script.sh <input>
$ sh script.sh

Example 1 – Echo, Read, Comment

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

Example 2 – Conditional, Looping, Logical

If, Else, Elif Condition

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

For Loop

#!/bin/bash
# For Loop Using Decremental Counter
for (( counter=10; counter>0; counter-- ))
do
echo -n "$counter "
done
printf "\n" 

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

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

While Loop

#!/bin/bash
# While Loop Until $n Equals 5

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

Case

#!/bin/bash
# Using Case and Getting Input Arguments
# -z $1 means input is null, -n $1 means input is not null
if [ -z $1 ]
then
  rental="No Valid Data Given"
elif [ -n $1 ]
then
  rental=$1
fi
 
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";;
esac

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

#!/bin/bash

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

Example 3 – Arguments or Positional Parameters

#!/bin/bash

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 arguments.sh Sathiyan Blog

Example 4 – Function

#!/bin/bash

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

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

rectangle_area $length $width

Reference

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 your_script.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....

Reference

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 https://builds.wkhtmltopdf.org/0.12.1.3/wkhtmltox_0.12.1.3-1~bionic_amd64.deb
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
psql
\password postgres
# Enter the password for postgres user
\q
exit
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
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
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
cd /etc/phppgadmin/
sudo nano config.inc.php
$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.
[options]
: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.