Originally published by Robert Beisert at fortcollinsprogram.robert-beisert.com

Linux + C – Scripting in the Shell II

Here’s a quick example of scripting in sh.

Shell_Script

#! /bin/bash
echo ''
echo 'FILE            LINES    CODE    COMMENTS    ART'
echo '--------------------------------------------------------------'

for file in *; do
if [ -f $file ]; then
awk '
BEGIN    {
LINES=0;
CODE=0;
ART=0;
COMMENTS=0;

}
{
if(    $0 ~ /^$/         ||
$0 ~ /^[\t]*;$/     ||
$0 ~ /^[\t]*{$/     ||
$0 ~ /^[\t]*}$/     ||
$0 ~ /^[\t]*[(]$/        ||
$0 ~ /^[\t]*[)]$/        ||
$0 ~ /^[\t]*[};]$/    )
{
ART++;
}
else if($0 ~ /[//]+/)
{
COMMENTS++;
}
else if($0 ~ /[/*]/)
{
COMMENTS++;
while(!($0 ~ /[*/]/))
{
getline;
COMMENTS++;
}
}
else
{
CODE++;
}
}
END{
LINES=NR;
printf("%16s\t%d\t%d\t%d\t\t%d\n", FILENAME, LINES, CODE, COMMENTS, ART);
}
' $file
fi
done
echo ''

 

 

 

Permissions

You will likely notice that you cannot run this script right after you write it. You will have to use chmod to enable execution of your shell scripts:

chmod +x Shell_Script

The script will then run normally.

More Information

I confess that I am not the most knowledgeable when it comes to the Shell. However, The Grymoire has an excellent tutorial, with just about every possible feature of the shell in great detail. I highly recommend that you bookmark this site, at minimum.

photo by: