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)