linux poison RSS
linux poison Email
2

Perl Script: How to find if variable is defined or not?

Below is a simple perl script which explains how to find if any given variable is defined or not.

Feel free to copy and use this code.

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

use strict;
use warnings;

my $Undefined;
my $Defined = "linuxpoison";

print "The value of Undefined is ", defined $Undefined, "\n";
print "The value of Defined is ", defined $Defined, "\n";

print "------------------------------\n";

$Undefined = $Defined;
$Defined = undef;

print "The value of Undefined is ", defined $Undefined, "\n";
print "The value of Defined is ", defined $Defined, "\n";

Output: perl undefined.pl
The value of Undefined is
The value of Defined is 1
------------------------------
The value of Undefined is 1
The value of Defined is


Read more
0

Free eBook - GNU/Linux Basic

"GNU/Linux Basic"


This guide will introduce you to the world of GNU/Linux.
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux.

Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.

This guide is not intentionally based on any particular distribution, but in most examples and activities the book will be very specific so it will go into detail using Debian GNU/Linux (version 4.0 -Etch-).

Download your free eBook of "GNU/Linux Basic" from here


Read more
0

Bash Script: How to read password without dispalying on the terminal

Below is the simple script which reads the user's password without displaying on the terminal.

Feel free to copy and use this code.

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

# Here the input passwd string will get display on the terminal
echo -n "Enter your passwd: "
read passwd
echo
echo "Ok, your passwd is: $passwd"
echo

# Now, lets turn off the display of the input field.

stty -echo
echo -n "Again, please enter your passwd: "
read passwd
echo
echo "Ok, your passwd is: $passwd"
echo
stty echo

Output: ./passwd.sh
Enter your passwd: linuxpoison

Ok, your passwd is: linuxpoison

Again, please enter your passwd:
Ok, your passwd is: linuxpoison.blog@gmail.com

As you can see above in the scrip, with the use of stty we can enable or disable the screen echo.


Read more
0

Free eBook - A Newbie's Getting Started Guide to Linux

Learn the basics of the Linux operating systems. Get to know what it is all about, and familiarize yourself with the practical side. Basically, if you're a complete Linux newbie and looking for a quick and easy guide to get you started this is it.

You've probably heard about Linux, the free, open-source operating system that's been pushing up against Microsoft. It's way cheaper, faster, safer, and has a far bigger active community than Windows, so why aren't you on it?

Like many things, venturing off into a completely unknown world can seem rather scary, and also be pretty difficult in the beginning. It's while adapting to the unknown, that one needs a guiding, and caring hand.

This guide will tell you all you need to know in 20 illustrated pages, helping you to take your first steps. Let your curiosity take you hostage and start discovering Linux today, with this manual as your guide! Don't let Makeuseof.com keep you any longer, and download the Newbie's Initiation to Linux. With this free guide you will also receive daily updates on new cool websites and programs in your email for free courtesy of MakeUseOf.

Download free eBook of "A Newbie's Getting Started Guide to Linux" - here


Read more
0

Bash Script: Calculate the total time taken by script execution

There is a bash predefined parameter SECONDS

Each  time  this  parameter  is referenced, the number of seconds since shell invocation is returned.  If a value is assigned to SECONDS, the value returned upon subsequent references is the number of seconds since the assignment plus the value assigned.  If SECONDS is unset, it loses its special properties, even if it is subsequently reset.

Below is simple bash script which demonstrate the usage of SECOND parameter

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

for i in {1..10}; do
        sleep 1
        echo $i
done

echo "Total time taken by this script: $SECONDS"

Output: ./time_taken.sh
1
2
3
4
5
6
7
8
9
10
Total time taken by this script: 10


Read more
0

Bash Script: Execute loop in background withing a script

Below is simple bash script which shows the way to make any loop execute in background, with this approach one can make the script to perform multiple things at the same time .... somewhat similar to multi threading.

In the example below, the '&' at the end of the first loop which makes this loop to go into background and at the same time second loop also gets started which makes both the loop to operate simultaneously.

Source: cat background-loop.sh
#!/bin/bash

for i in 1 2 3 4 5 6 7 8 9 10; do
  echo -n $i
done &

for i in a b c d e f g h i j k; do
  echo -n $i
done
echo

Output: ./background-loop.sh
abcdef1gh23ij4k5

Practical example: you can use above approach to copy some set of files to some other server or storage (backup) and at the same time you can perform the ftp job to copy other set of files to ftp server.



Read more
0

Bash Script: How read file line by line (best and worst way)

There are many-many way to read file in bash script, look at the first section where I used while loop along with pipe (|) (cat $FILE | while read line; do ... ) and also incremented the value of (i) inside the loop and at the end I am getting the wrong value of i, the main reason is that the usage of pipe (|) will create a new sub-shell to read the file and any operation you do withing this while loop (example - i++) will get lost when this sub-shell finishes the operation.

In second and the worst method, One of the most common errors when reading a file line by line is by using a for loop (for fileline in $(cat $FILE);do ..), which prints the every word in a file on separate line, because, for loop uses the default value of IFS (space).

In third perfect method, The while loop (while read line;do .... done < $FILE) is the most appropriate and easiest way to read a file line by line, see the example below.

Input: $ cat sample.txt
This is sample file
This is normal text file

Source: $ cat readfile.sh
#!/bin/bash

i=1;
FILE=sample.txt

# Wrong way to read the file.
# This may cause problem, check the value of 'i' at the end of the loop
echo "###############################"
cat $FILE | while read line; do
        echo "Line # $i: $line"
        ((i++))
done
echo "Total number of lines in file: $i"

# The worst way to read file.
echo "###############################"
for fileline in $(cat $FILE);do
        echo $fileline
done

# This is correct way to read file.
echo "################################"
k=1
while read line;do
        echo "Line # $k: $line"
        ((k++))
done < $FILE
echo "Total number of lines in file: $k"

Read more
0

Bash Script: Using IFS to split the strings into tokens

IFS (Internal Field Separator) is one of Bash's internal variables. It determines how Bash recognizes fields, or word boundaries, when it interprets character strings.

$IFS defaults to whitespace (space, tab, and newline), but can be changed, below example show you how to change the IFS value to split the strings into tokens based on any delimiter, in our case we are using ':' to split the string into tokens.

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

var="google:yahoo:microsoft:apple:oracle:hp:dell:toshiba:sun:redhat"
echo "Original value of IFS is: $IFS"

# Saving the original value of IFS
OLD_IFS=$IFS
IFS=:
echo "New value of IFS is: $IFS"
echo "================="

for word in $var;do
        echo -e $word
done <<< $var

echo "================"

# Restore the value of IFS back to the script.
IFS=$OLD_IFS
echo "Back to original value of IFS: $IFS"

Read more
1

Free eBook - Securing & Optimizing Linux: The Hacking Solution

"Securing & Optimizing Linux: The Hacking Solution (v.3.0)"

A comprehensive collection of Linux security products and explanations in the most simple and structured manner on how to safely and easily configure and run many popular Linux-based applications and services.


This 800+ page eBook is intended for a technical audience and system administrators who manage Linux servers, but it also includes material for home users and others. It discusses how to install and setup a Linux server with all the necessary security and optimization for a high performance Linux specific machine. It can also be applied with some minor changes to other Linux variants without difficulty.

Download free eBook - Securing & Optimizing Linux: The Hacking Solution (v.3.0) - here


Read more
0

Bash Script: String manipulation (Find & Cut)

${parameter#word}
${parameter##word}

Remove matching prefix pattern. If the pattern matches the beginning of the value of parameter, then  the  result  of  the  expansion  is the expanded  value  of  parameter  with  the shortest matching pattern (the ``#'' case) or the longest matching pattern (the ``##'' case) deleted. 

${parameter%word}
${parameter%%word}

Remove matching suffix pattern.  If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ``%'' case) or the longest matching pattern (the ``%%'' case) deleted. 

Below bash script explains the concepts of find and cut the string within the string, feel free to copy and use this script.

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

var="Linuxpoison.Linuxpoison.Linuxpoison"
echo
echo -e "Value of var is: $var"
echo "--------------------------------------------"
echo 'Find all *nux from the start of the string and cut ${var##*nux} :'
echo ${var##*nux}

echo "-------------------------------------------"
echo 'Find the first *nux from the start of the string and cut ${var#*nux} :'
echo ${var#*nux}

echo "-------------------------------------------"
echo 'Find all .* from the back of the string and cut ${var%%.*} :'
echo ${var%%.*}

echo "------------------------------------------"
echo 'Find first .* from the back of the string and cut ${var%.*} :'
echo ${var%.*}

Read more
1

Free eBook - Linux from Scratch

Linux from Scratch describes the process of creating your own Linux system from scratch from an already installed Linux distribution, using nothing but the source code of software that you need.

This 318 page eBook provides readers with the background and instruction to design and build custom Linux systems. This eBook highlights the Linux from Scratch project and the benefits of using this system.

Users can dictate all aspects of their system, including directory layout, script setup, and security. The resulting system will be compiled completely from the source code, and the user will be able to specify where, why, and how programs are installed.

This eBook allows readers to fully customize Linux systems to their own needs and allows users more control over their system.

Download Free Linux from Scratch PDF Guide - here


Read more
0

Network Traffic and Bandwidth Monitor - NTM (Network Traffic Monitor)

NTM (Network Traffic Monitor) is a monitor of the network and internet traffic for Linux. NTM is useful for the people that have a internet plan with a limit, and moreover the exceed traffic is expensive.

NTM (Network Traffic Monitor) features:
 * Choice of the interface to monitoring.
 * Period to monitoring: Day, Week, Month, Year or Custom Days. With auto-update.
 * Threshold: Auto-disconnection if a limit is reached (by Network Manager).
 * Traffic Monitoring: Inbound, outbound and total traffic; Show the traffic speed.
 * Time Monitoring: Total time of connections in the period.
 * Time Slot Monitoring: Number of sessions used.
 * Reports: Show of average values and daily traffic of a configurable period.
 * Online checking with Network-manager or by "Ping Mode".
 * The traffic is attributed to the day when the session began.
 * Not need root privilege.
 * Not invasive, use a system try icon.

NTM is write in python and is a open source software, the license is the GNU GPL v2.

Read more
1

Simple & Powerful GTK based Download Manager - UGet

Uget (formerly urlgfe) is a Free and Open Source download manager written in GTK+ , it has many of features like ...

 * Free (as in freedom , also free of charge ) and Open Source.
 * Simple , easy-to-use and lightweight.
 * Support resume download , so if your connection lost you don’t need to start from first.
 * Classify downloads , and every category has independent configuration and queue.
 * Queue download.
 * Integrate with Firefox through Flashgot plugin.
 * Monitoring clipboard.
 * Import downloads from HTML file.
 * Batch download , you can download many files has same arrange , like file_1 file_2 …. file_20 ,  you don’t need to add all links , just one link and changeable character.
 * Can be used from command line.
Read more
Related Posts with Thumbnails