The UNIX way.

Around the clock, across the globe. By Vladimir Legeza

Archive for the ‘Shell Scripting’ Category

Bash script template

leave a comment »

Probably it will be a good idea not to write every script from a blank page, because the most scripts at the beginning contains the same information. like “what this pease of code does”, “who wrote it and when”, “default options declarations” and etc.

In following template I’ll define the most popular parts that all good scripts should contain. Also I figured out that adding --debug option is a very usefull thing that allows you to deep dive throught the code without extra manipulations.

Simple, clean, usefull:

#!/usr/bin/env bash
# Description:
# Execution frequency:
# Options:
# Author: Name e-mail DD/MM/YYYY
# Editions:

echo “Usage:

ARGS=`getopt -o h --long help,debug -- $@`
eval set -- $ARGS

while true ; do
case “$1” in
-h| --help) usage; exit 0;;
--debug) set -x; shift;;
--) shift ; break ;;
*) echo “Internal error!“; usage; exit 1;;

exit 0


Written by Vladimir Legeza

May 30, 2012 at 12:06 pm

Parallelization task in a shell script.

leave a comment »

Assume we have a 1000 files and we need to process each of them in a some way. File processing takes at least a 15 minutes. So, to process them all consistently we need aproximately 10 days. If we suppose that we will process them on a computer with more then one core, it would be logical to run some processes in parallel. But if we would start them all at the same time – our computer is going to be dead.

The following shell code defines a number of a processes that should be run concurrently, and supported the same number of concurrently running processes until the last file would not be processed.

FILES_LIST=1 2 3 4 5 6 7

echo "Start processing file $1"
sleep 1
echo "File $1 is processed."

# Main loop
for i in $FILES_LIST; do
processing $i &
pid_list="$pid_list $!"

# Control of the concurrent processes
while [ `echo $pid_list|wc -w` -ge "$PROC_IN_PARALLEL" ] ; do
for pid in $pid_list ; do
if [ -d /proc/$pid ] ;then
new_pid_list="$new_pid_list $pid"
sleep 1


Read the rest of this entry »

Written by Vladimir Legeza

April 1, 2011 at 9:54 am

Network connection from the shell script.

leave a comment »

Shell allow users to send udp or tcp messages to the remote host and to receive replies  directly from the command line without any additional tools. It seems like a quite useful thing, but the functionality are limited to act only as a tcp/udp client, and not as a server.

The whole following code shows how to exploit this functionality. It should works properly in both major shell interpretors: Bash and Ksh.


exec 3<> /dev/tcp/$host_name/$port_number
echo -e "GET / HTTP/1.0\nHost: $host_name\n\n" >&3
while true ;do
read -r response <&3
echo $response
if [ -z "$response" ];then
Read the rest of this entry »

Written by Vladimir Legeza

March 31, 2011 at 4:23 pm