Friday, April 25, 2014

Solution for maintaining old Ubuntu Linux computer

If you have an old server with Ubuntu Linux distribution, which for some reason you can not update to newer version and you need to maintain it. During new program installation many errors tells us that  aptitude or apt-get can not find the path to the server from which we need to download required packets. Solution is very simple for the following Ubuntu Linux distributions: 

  • Ubuntu 4.10 (Warty Warthog)
  • Ubuntu 5.04 (Hoary Hedgehog)
  • Ubuntu 5.10 (Breezy Badger)
  • Ubuntu 6.06.2 LTS (Dapper Drake)
  • Ubuntu 6.10 (Edgy Eft)
  • Ubuntu 7.04 (Feisty Fawn)
  • Ubuntu 7.10 (Gutsy Gibbon)
  • Ubuntu 8.04.4 LTS (Hardy Heron)
  • Ubuntu 8.10 (Intrepid Ibex)
  • Ubuntu 9.04 (Jaunty Jackalope)
  • Ubuntu 9.10 (Karmic Koala)
  • Ubuntu 10.04.4 LTS (Lucid Lynx)
  • Ubuntu 10.10 (Maverick Meerkat)
  • Ubuntu 11.04 (Natty Narwhal)
  • Ubuntu 11.10 (Oneiric Ocelot) 
  • Ubuntu 12.04.3 LTS (Precise Pangolin)
All repositories for those distributions are located at: old-releases.ubuntu.com

How to make changes

Edit your /etc/apt/sources.list : 
change all urls from: http://<>.archive.ubuntu.com/ubuntu  to http://old-releases.ubuntu.com/ubuntu

And your old Ubuntu Linux is ready for update :)


Thursday, April 24, 2014

BASH - customize ping output

Problem is simply: how to get the output of ping command suited to our needs. In this example we will extract average ping time in Bash scripting language.

First let's see output of ping command: ping 192.168.1.1 -c 5 (-c 5 tells to ping command to run five times, else it will run infinitely.)

$ ping 192.168.1.1 -c 5
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=2.62 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.477 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.482 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.482 ms
64 bytes from 192.168.1.1: icmp_seq=5 ttl=64 time=0.487 ms

--- 192.168.1.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4000ms
rtt min/avg/max/mdev = 0.477/0.911/2.629/0.859 ms

Average ping time is in last line. Use tail -1 to extract only last line. 

$ ping 192.168.1.1 -c 5|tail -1
rtt min/avg/max/mdev = 0.477/0.911/2.629/0.859 ms

Then extract values, count 'arguments' in this line separated by space: 'rtt' is the first, 'min/avg/max/mdev' 2nd, '=' 3rd, '0.477/0.911/2.629/0.859' 4th and 'ms' 5th. 

$ ping 192.168.1.1 -c 5|tail -1|awk '{print $4}'
0.477/0.911/2.629/0.859

and finally, from this output of four '/' separated values, take the second value: this is done by cut command: 

$ ping 192.168.1.1 -c 5|tail -1|awk '{print $4}'|cut -d '/' -f 2
0.911

and this is the value that we are looking for... 

Saturday, February 22, 2014

Linux How to concatenate text files

cat file1 file2 filen > outputfile

or if you want to concatenate all text files in directory:
cat *.txt > outputfile
or you can use >> operator to append to file:
cat file1 >> file2

Linux How to count words and lines in a text file

If you want to count words in some text file you could use:
wc -w <file>
example for file 2600.txt (War and Peace from Leo Tolstoy) from Guttenberg Project :
wc -w 2600.txt
output:
566321 2600.txt
 If you want to count lines in some text file you could use:
wc -l <file>
the same file for example
wc -l 2600.txt
output:
65008 2600.txt

Full options list for wc command:

  • -c or --bytes byte counts
  • -m or --chars character counts
  • -l or --lines newline counts
  • -L or --max-line-length lenght of longest line
  • -w or --words word counts

 also:
  • --help
  • --version


Friday, February 1, 2013

Bash Conditionals

Conditionals are used when you must make some decision (to execute some part of code or not) by evaluating expression in your script.
For conditionals in Bash scripting language are used clauses: if, then, else.

if (some expression)
then
(do some code)
else
(do some code)

Simplest example is use only if,then

if ["$Var1" = "$Var2"]; then
echo "Variables Var1 and Var2 are equal!"
fi

with clause fi we close if statement.

Other example:

if ["$Var1" = "$Var2"]; then
echo "Variables Var1 and Var 2 are equal!"
else
echo "Variables Var1 and Var2 are NOT equal!"
fi


Variables in Bash script

Variables in Bash script language have following properties:
  • They haven't data type
  • There is no need to declare variable
So, let's see how they works on simple "Hello World" Example:
#!/bin/bash
Var1 = "Hello World!"
echo $Var1
This simple example will output sentence Hello World!

 Interesting with $ sign in Bash is, if you put something in brackets after $ this will be interpreted as interpreter command.
#!/bin/bash
Var1= $(ls -la)
echo $Var1
This simple example will put output from ls -la command in $Var1 variable and then print content on screen.

Important thing about variables is that you can use local variables in functions, using keyword local.

local Var1="Local variable"

Builtin variables

 

 Bash have Built-in variables, some of them are:
  • $BASH - Path to the Bash binary
  • $BASHPID - Process ID of Bash 
  • $BASH_VERSION - Version of Bash
  • $HOME - Home directory of user
  • $MACHTYPE - Machine type
  • $PATH - Path to binaries
  • $PWD - Working directory
  • $SECONDS - number of second while script is running
  • $UID - User ID number
  • $0,$1 ... - Command line arguments
  • $# - Number of command line arguments
And many more... 





Sunday, October 14, 2012

Bash - Count words in text and make dictionary

In this tutorial i'll try to explain how to count words in some text and make list of used words.
First we could get some text file. I get War and Peace by Leo Tolstoy from Project Gutenberg:
wget http://gutenberg.org/files/2600/2600.txt

After getting file, we must to convert uppercase letters to lowercase:
tr A-Z a-z
then convert everything which is not small letter to new line
tr -cs a-z '\n'
then sort result
sort
finally get unique words and count them:
uniq -c
To get this into working condition we must put those commands into pipeline. To do that we'll make file wordcount:
nano wordcount
and type this:

cat "$@" | tr A-Z a-z | tr -cs a-z '\n'|sort|uniq -c
save and exit nano editor.
Give executable rights to file wordcount:
chmod +x wordcount
and count words with
./wordcount <textfile>
in this case:
./wordcount 2600.txt
and we'll get words used in this great book and their number of appearances.

If we want to get only dictionary used in this book, we make another file:
nano makedict
and type this:
cat "$@" | tr A-Z a-z | tr -cs a-z '\n'|sort|uniq
take note that only difference between wordcount and makedict scripts is -c switch in uniq command.
of course, we must give execution rights to file:
chmod +x makedict
and use:
./makedict 2600.txt