skip to main |
skip to sidebar
#!/usr/bin/perl
#===============================================================================
#
# FILE: above_avg.pl
#
# USAGE: perl above_avg.pl
#
# DESCRIPTION: Script to ask the numbers from user as a input and return the numbers which are above average.
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Parag Kalra, paragkalra@gmail.com, www.paragkalra.com (),
# COMPANY: As of now Persistent System LTD, www.persistentsys.com
# VERSION: 1.0
# CREATED: Wednesday 11 February 2009 07:06:21 IST
# REVISION: ---
#===============================================================================
print "Please enter the numbers: \n";
@nums=;
@more_avg=&above_avg(@nums);
#=== FUNCTION ================================================================
# NAME: above_avg
# PURPOSE: To calculate numbers above average
# PARAMETERS: ????
# RETURNS: ????
# DESCRIPTION: ????
# THROWS: no exceptions
# COMMENTS: none
# SEE ALSO: n/a
#===============================================================================
sub above_avg {
print "You have entered following numbers: \n ";
print "@_ \n";
$avg_value=&avg(@_);
print "Numbers greater than average - $mean are: \n";
foreach (@_) {
if ($_ > $avg_value) {
#@return_more=(@return_more,$_);
push(@return_more,$_);
}
}
print " @return_more \n";
}
#=== FUNCTION ================================================================
# NAME: avg
# PURPOSE: To calculate mean value
# PARAMETERS: ????
# RETURNS: ????
# DESCRIPTION: ????
# THROWS: no exceptions
# COMMENTS: none
# SEE ALSO: n/a
#===============================================================================
sub avg {
$sum=0;
$count=0;
foreach (@_) {
$sum=$sum+$_;
$count=$count+1;
}
$mean=$sum / $count;
print "Total numbers entered: $count \n";
print "Sum of numbers: $sum \n";
print "Mean of numbers: $mean \n";
$mean;
}
People who do extensive PERL scripting on UNIX and love "vi' editor. Here's some interesting stuff for you:
http://www.vim.org/scripts/script.php?script_id=556
After unzipping it in the folder "$HOME/.vim/", read the file "$HOME/.vim/README.perlsupport"
#!/bin/bash
#===============================================================================
#
# FILE: vars_bash.sh
#
# USAGE: ./vars_bash.sh
#
# DESCRIPTION: Using variables in bash script.
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Parag Kalra (), paragkalra@gmail.com, www.paragkalra.com
# COMPANY: As of now Persistent System LTD, www.persistentsys.com
# VERSION: 1.0
# CREATED: Sunday 01 February 2009 11:16:38 IST IST
# REVISION: ---
#===============================================================================
echo "uninitialized_variable = $uni_var"
null_variable=
echo "null_variable = $null_variable"
let "num_var2 += 2"
echo "$num_var2"
num_var1=1
let "num_var1 += 4"
echo "$num_var1"
#-------------------------------------------------------------------------------
# Unsetting a variable
#-------------------------------------------------------------------------------
num_var=13
echo "$num_var"
unset num_var
echo "$num_var"
#-------------------------------------------------------------------------------
# Different ways a variable can be assigned
#-------------------------------------------------------------------------------
a=13;
echo $a
let a=a+13
echo $a
echo "Enter the value you want to assign to variable a: "
#read a
for a in 1 2 3
do
echo $a
done
list1=`ls -l`
echo $list1
list2=$(ls -l)
echo $list2
#-------------------------------------------------------------------------------
# Converting integers to strings and vice-versa
#-------------------------------------------------------------------------------
a=1234
echo $a
let a=a+1
echo $a
a=${a/35/TF}
echo $a
a=${a/TF/13}
echo $a
#!/bin/bash
#===============================================================================
#
# FILE: posi_param.sh
#
# USAGE: ./posi_param.sh 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
#
# DESCRIPTION: To exhibit usage of positional parameters
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Parag Kalra (), paragkalra@gmail.com, www.paragkalra.com
# COMPANY: As of now Persistent System LTD, www.persistentsys.com
# VERSION: 1.0
# CREATED: Sunday 01 February 2009 01:35:54 IST IST
# REVISION: ---
#===============================================================================
echo -e "\n \n"
echo "The arguments passed are: $*"
echo -e "Total number of arguments passed are: $# \n \n"
echo $1
echo $2
echo $3
echo $4
echo $5
echo $6
echo $7
echo $8
echo $9
echo -e "\n \n"
#-------------------------------------------------------------------------------
# Accessing positional parameters with curly braces produce unexpected result
#-------------------------------------------------------------------------------
echo $10
echo $11
echo $12
echo $13
echo $14
echo $15
echo -e "\n \n"
#-------------------------------------------------------------------------------
# This is the way to access double digit positional parameters
#-------------------------------------------------------------------------------
echo ${10}
echo ${11}
echo ${12}
echo ${13}
echo ${14}
echo ${15}
echo -e "\n \n"
#-------------------------------------------------------------------------------
# This is strange
#-------------------------------------------------------------------------------
for a in 1 2 3 4 5
do
echo $`echo $a`
done
echo -e "\n \n"
#-------------------------------------------------------------------------------
# and it still remains strange
#-------------------------------------------------------------------------------
for a in 1 2 3 4 5
do
echo "$`echo $a`"
done
echo -e "\n \n"
#-------------------------------------------------------------------------------
# Shiting the positional parameters
#-------------------------------------------------------------------------------
#: << COMMENTED
echo "Shifting positional parameters.."
until [ -z $1 ]
do
echo $1
shift
done
echo -e "\n \n"
#COMMENTED
#-------------------------------------------------------------------------------
# Shiting the positional parameters by 5 characters
#-------------------------------------------------------------------------------
echo "Shifting positional parameters by 4 characters.."
until [ -z $1 ]
do
echo $1
shift 4
done
echo -e "\n \n"