linux poison RSS
linux poison Email
0

Bash Script: Performing Floating point Math Operation

NOTE: If your program depends heavily on Math operation (floating point), better switch to ksh instead of traditional bash.

Bash does not support floating point operations but it's possible to redirects these Math operation to some other program (bc).

The "bc" calculator comes as a part of your Linux distro, so there's no need for you to install anything extra. In addition to performing simple math functions, it can also perform conversions between different number systems, perform a number of scientific math functions, and can even run programs that you write and save in a text file. look the bc man pages for more details.

Below is simple bash script which demonstrate the usage of bc commands in the script

Source: cat floating.sh
#!/bin/bash

echo "----- Addition ------"
x=10.2
y=20.2
z=`echo $x+$y|bc`
echo $z
Read more
2

White Paper - A Multi-Level Approach to Addressing Targeted Attacks

"A Multi-Level Approach to Addressing Targeted Attacks"
88% of targeted malware is NOT detected by anti-virus according to research by Trustwave.

Targeted malware is tailor-designed to take advantage of an organization's specific device(s), data networks or a specific employee. Taking a multi-level approach, using many solutions, businesses can enable wider coverage for all types of targeted malware, reducing the attack surface and preventing attacks.

Download this paper and learn about:
 * The current state of targeted attacks
 * How and why targeted attacks work
 * Multi-level defenses to combat malware and persistent threats
 * Targeted attacks are increasing in number and complexity - every business is at risk.

 Download this paper now to find out how to improve your security and protect your business - here


Read more
0

Perl Script: Include external Perl script withing Perl script

This is something different that including the package/modules, the require function provides a way to break your program into separate files and create libraries of functions.

NOTE: The last expression evaluated inside a file included by require becomes the return value. The require function checks whether this value is zero, and terminates if it is so, in this case make sure to return some non-zero value from withing your include file and the best way to do is to insert the 1; at the end of this include file.

Below Perl script show the way to include another Perl script within the Perl script.


Include File: cat include.pl
#!/usr/bin/perl

print "Inside the include file \n";
$var = 20;

1;

Source: cat require.pl
#!/usr/bin/perl

require ("include.pl");
$var = $var + 20;
print "final value of var is : $var \n";

Output: perl require.pl
Inside the include file
final value of var is : 40


Read more
0

Perl Script: Sorting an Array

The Perl sort function sorts a string ARRAY by an ASCII numeric value and numbers using alphanumerical order and returns the sorted list value.

The problem is that capital letters have a lower ASCII numeric value than the lowercase letters so the words beginning with capital letters will be shown first, so in order to get the desire result we need to do something else to this default sort functionality provide by the Perl.

The Perl sort function uses two operators: cmp and <=>, you can use the cmp (string comparison operator) or <=> (the numerical comparison operator) and also uses two special variables $a and $b are compared in pairs by sort to determine how to order the list.

Below is simple Perl script which demonstrate the usage of the sort functionality of Perl, feel free to copy and use this script.

Source: cat sortarray.pl
#!/bin/usr/perl

@string_array = ("Foo", "loo", "Boo", "zoo", "Moo", "hoO", "GOo", "Poo");
@sorted_stringarray = sort (@string_array);
print "This is defaul sort : ", join(", ", @sorted_stringarray), "\n";

@sorted_stringarray = ();
@sorted_stringarray = sort ({lc($a) cmp lc($b)} @string_array);
print "This is what we want: ", join(", ", @sorted_stringarray), "\n";

Read more
0

Free EBook - Introduction to Linux - A Hands on Guide

"Introduction to Linux - A Hands on Guide"

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.

For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Download your free copy of "Introduction to Linux - A Hands on Guide" - here


Read more
Related Posts with Thumbnails