Shell Script to execute a command on every line of a file

There are many times, where you want to execute the same command every line of a file, it can be simple printing on the screen or some other manipulation. This simple shell script reads a file line by line and executes the command specified by the user on the line.

$ cat execute_command_per_line.sh
if [ $# -ne 2 ]
then
   echo "Usage: execute_command_per_line command filename"
   exit 1
fi

command=$1
filename=$2

while read line
do
   $command $line
done < $filename

As you can see that it takes two parameters,

  1. Command to be executed
  2. Name of the file

Let’s use a simple command like echo, The file contents are as follows

$ cat system_calls.txt
 
aa_change_hat
accept
accept4
access
acct
add_key
adjtime
adjtimex
afs_syscall
alarm

Let’s run the above script

$ ./execute_command_per_line.sh echo system_calls.txt 
aa_change_hat
accept
accept4
access
acct
add_key
adjtime
adjtimex
afs_syscall

Shell Script to extract various sections of a Man page

A Man page has various sections like

  1. NAME
  2. SYNOPSIS
  3. DESCRIPTION
  4. EXAMPLE
  5. AUTHORS

The above are some of the few sections to name for. You open a man page using the command man. It can also be redirected to a file.

Let’s see the script

$ cat man_section_extract.sh 

if [ $# -ne 3 ]
then
   echo "Usage: man_section_extract man_number section command"
   exit 1
fi
man_number=$1
section=$2
command=$3
   
man $man_number $command> /tmp/man.txt
if [ $? -eq 0 ]
 then
 cat /tmp/man.txt | sed -n -e "/^$section/,/^[A-Z]/ p"| grep -v -e "^[A-Z]" 
fi

As you can see that it takes three arguments,

  1. The Man page number (Commmands are usually under man page 1, system calls under man page 2, library calls under man page 3)
  2. Section: as described above, It can be NAME, DESCRIPTION
  3. Command

Let’s first take a look at a sample man page


NAME
 ls - list directory contents

SYNOPSIS
 ls [OPTION]... [FILE]...

DESCRIPTION
 List information about the FILEs (the current directory by default).  Sort entries alphabetically if none of -cftuvSUX nor --sort.

 Mandatory arguments to long options are mandatory for short options too.

Now let’s use our script to extract the NAME section

$ ./man_section_extract.sh 1 NAME ls
       ls - list directory contents

Let’s extract SYNOPSIS

$ ./man_section_extract.sh 1 SYNOPSIS ls
       ls [OPTION]... [FILE]...

Like this, you can extract various sections of a man page. All you need is to specify the command, section name and man page number

How to install csh in Ubuntu?

You may have worked with the Terminal a number of times and if you are a command line enthusiast, you may have definitely heard about bash. bash is a command language interpreter. It is an acronym for Bourne again Shell. Like bash, there are plenty of other shells, one of which is csh. It’s a shell with C-like syntax

To install csh in Ubuntu

$ sudo apt-get install csh

To work with csh, go to the command line and enter csh

$ csh
%

Now enter any commands of your choice

apt-get: Shell Script to remove the packages mentioned in a File

Check this simple shell script which reads all the packages mentioned in a file and removes them one by one.

#! /bin/sh
if [ $# -ne 1 ]
then echo "Usage: read.sh filename";
exit 1;
fi

if [ ! -f $1 ]
then echo "File $1 doesn't exist";
exit 1;
fi

while read line
do
  pkg=`echo $line| awk '{print $1}'`
  echo $pkg
  yes|sudo apt-get remove $pkg
done < $1

To use this script, mention the packages to be removed in a file. Take for example, pkgs.txt contains the files to be removed

$ cat pkgs.txt
vim
emacs

To execute the above script,

$ ./remove.sh pkgs.txt

It will remove all the packages one by one.

Shell script to find the length of a string

awk has a built in function called length with which you can find the length of a string. The following shell script shows how to find the length of the string with awk

#!/bin/sh
#Shell script to find the length of the string

string="Joys of Programming"
length=`echo|awk "{print length("$string")}"`
echo "Length of the string "$string" is $length"

On executing the program

$ ./length.sh 
Length of the string "Joys of Programming" is 19

You can even find the length from the terminal with this simple command

$ awk 'BEGIN {print length("Joys of Programming")}'
19

or

$ echo|awk '{print length("Joys of Programming")}'
19

Thus you can see that the syntax of length function is

length(string)

Shell script to read a file line by line

This is a simple shell script to read a file line by line

while read line
do
  echo $line
done < libraries-php.txt

Now execute the program

$ source read.sh
php5-cgi - server-side, HTML-embedded scripting language (CGI binary)
php5-cli - command-line interpreter for the php5 scripting language
php5-common - Common files for packages built from the php5 source

The above program can be modified to take file name as as an argument and some additional checks

#! /bin/sh
if [ $# -ne 1 ]
then echo "Usage: read.sh filename";
exit 1;
fi

if [ ! -f $1 ]
then echo "File $1 doesn't exist";
exit 1;
fi

while read line
do
  echo $line
done < $1

Now the program can take file name as argument

$ source read.sh libraries-php.txt

How to display the environment variables?

Environment variables are the various settings under which your program runs. For example, the most commonly used environment variable PWD corresponds to the current working directory. Similarly the environment variable PATH corresponds to the directories to be searched when a command is entered in the terminal.

To see all the environment variables, run env on your terminal


$ env

SSH_AGENT_PID=4874
TERM=xterm
SHELL=/bin/bash
XDG_SESSION_COOKIE=ad6a6ed4a2ab60aa7d8d10774c90bd44-1288194669.837171-448800695
WINDOWID=73400323
GNOME_KEYRING_CONTROL=/tmp/keyring-Ohk0kZ
GTK_MODULES=canberra-gtk-module
USER=user
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:
SSH_AUTH_SOCK=/tmp/keyring-Ohk0kZ/ssh
DEFAULTS_PATH=/usr/share/gconf/gnome.default.path

Working with GoogleCL

GoogleCL provides access to a number of services from the command line.

The available services are

  1. picasa
  2. blogger
  3. youtube
  4. docs
  5. contacts
  6. calendar

To work with each of these, let’s see how googleCL works

After installing GoogleCL, enter


$ google

>

Now you can type any command there. Let’s start with help


$ google
> help
Welcome to the Google CL tool!
 Commands are broken into several parts:
 service, task, options, and arguments.
 For example, in the command
 "> picasa post --title "My Cat Photos" photos/cats/*"
 the service is "picasa", the task is "post", the single
 option is a title of "My Cat Photos", and the argument is the
 path to the photos.

 The available services are
'picasa', 'blogger', 'youtube', 'docs', 'contacts', 'calendar'
 Enter "> help <service>" for more information on a service.
 Or, just "quit" to quit.
>

To work with any service, simply type the service name and the options


>help picasa

help picasa
Available tasks for service picasa: 'get', 'create', 'list', 'list-albums', 'tag', 'post', 'delete'
 get: Download photos
 Requires: none Optional: title, query Arguments: LOCATION

 create: Create an album
 Requires: title Optional: date, summary, tags Arguments: PATH_TO_PHOTOS

 list: List photos
 Requires: delimiter Optional: title, query

 list-albums: List albums
 Requires: delimiter Optional: title

 tag: Tag photos
 Requires: tags AND (title OR query)

 post: Post photos to an album
 Requires: title Optional: tags Arguments: PATH_TO_PHOTOS

 delete: Delete photos or albums
 Requires: (title OR query)

So you can see a number of options with picasa like create, list, delete


> picasa list

The above command will display all the URLs of your photographs


> picasa list-albums

This will display only your album URLs

similarly you can work with any service and know the options


> help service-name