Friday, May 2, 2014

Customize Bash prompt

Bash prompt text is defined in PS1 variable. PS1 variable is declared in .bashrc file which is in home directory. Open this file with your favorite text editor: ~/.bashrc
Find declaration of PS1 variable (maybe there are few of them) and, if you want to permanent change prompt, change this declaration to suit your needs. If you want to change prompt temporally (recommended!) until restart, type in Bash command prompt:

$ PS1="NEW PROMPT --> "
NEW PROMPT -->

If you look in default string there are some special character with some system parameters as:

  • \u username
  • \h hostname
  • \w working directory
  • ...


$ PS1="\h:\w"
linips:~


Thursday, May 1, 2014

Linux - Add User Account

To add user to Linux from command line (shell) type command adduser followed with options and username. Example to add user linips (type sudo if you are not root):

$ sudo adduser linips
Adding user `linips' ...
Adding new group `linips' (1001) ...
Adding new user `linips' (1001) with group `linips' ...
Creating home directory `/home/linips' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for linips
Enter the new value, or press ENTER for the default
        Full Name []: Linips Blog
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n]

Defaults are in file /etc/adduser.conf

What if we are want to add user with username format as firstname.secondname?

$ sudo adduser linips.blog
adduser: Please enter a username matching the regular expression configured
via the NAME_REGEX[_SYSTEM] configuration variable.  Use the `--force-badname'
option to relax this check or reconfigure NAME_REGEX.

Then we must add --force-badname switch to command

$ sudo adduser --force-badname linips.blog

and continue with entering data for new user.


Wednesday, April 30, 2014

Bash - String Lenght

Getting string length in Bash Script is very easy by using # operator:

$ some_variable="This is Test String"
$ echo ${#some_variable}
19

${#variable_name} gives us string length.

Tuesday, April 29, 2014

Linux Add Group

To add group linips to Linux type:

$ groupadd linips

General format of command groupadd is:

groupadd [options] groupname

options are:

  • --help 
  • -g, --GID gid -to assign Group ID
  • -f, --force - -f with -g acts like -g not exist - GID another unique
  • -K, --key KEY=VALUE
  • -o, --non-unique - to add group with non unique GID
  • -p, --password PASSWORD
  • -r, --system - To create system group




Monday, April 28, 2014

Bash - Ping ip range and email result

In Bash script is a very simple to write script for checking availability of network devices. In this case, the script will ping an address range and rusults will be sent to your email address.

#!/bin/bash
om="mailtempfile"
dat="$(date)"
eval "echo 'This is Computer status from Linips Script at $dat' >${om}"
eval "echo '' >>${om}"

for i in {1..6}
do
    out="$(ping 192.168.1.$i -c 5|tail -1|awk '{print $4}'|cut -d '/' -f 2)"
    case $i in
    1)
        comp="Router One     "
        ;;
    2)
        comp="Router Two     "
        ;;
    3)
        comp="Router Three   "
        ;;
    4)
        comp="Server One     "
        ;;
    5)
        comp="Server Two     "
        ;;
    6)
        comp="Server Three   "
        ;;
    esac
    eval "echo '192.168.1.$i   $comp    $out'>>${om}"
done
mail -s "Net status $dat" myemail@adress <mailtempfile


Or, the same script, with using Bash script arrays:

#!/bin/bash
comp[1]="Router One      "
comp[2]="Router Two      "
comp[3]="Router Three    "
comp[4]="Server One      "
comp[5]="Server Two      "
comp[6]="Server Three    "

om="mailtempfile"
dat="$(date)"
eval "echo 'This is Computer status from Linips Script at $dat' >${om}"
eval "echo '' >>${om}"

for i in {1..6}
do
    out="$(ping 192.168.1.$i -c 5|tail -1|awk '{print $4}'|cut -d '/' -f 2)"
    eval "echo '192.168.1.$i   ${comp[$i]}    $out'>>${om}"
done
mail -s "Net status $dat" myemail@address <mailtempfile



Received email:

This is Computer status from Linips Script at Mon Apr 28 08:13:45 CEST 2014

192.168.1.1   Router One         2.493
192.168.1.2   Router Two         17.712
192.168.1.3   Router Three       13.669
192.168.1.4   Server One         4.601
192.168.1.5   Server Two         8.199
192.168.1.6   Server Three       9.302


And finally, the greatest strenght of this script is that it runs automatically, so you need to add one line in the /etc/crontab file: (example is for script running every 20 min)

*/20  *       * * *     root     /home/user/pingscript..sh

Sunday, April 27, 2014

Bash String Concatenation

If you writing Bash scripts, it's probably happened that you need to concatenate two or more strings. There are several ways to do this:

1. Variable insert

$ a="Hello"
$ b="$a World"
$ echo $b
Hello World

2. Writing variables one after another

$ a="Hello"
$ b=" World"
$ c=$a$b
$ echo $c
Hello World

3. Using += operator

$ a="Hello"
$ a+=" World"
$ echo $a
Hello World




Saturday, April 26, 2014

Linux Get CPU Info

Probably the simplest way to get CPU info in Linux is to display /proc/cpuinfo (and example output)

$ cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 15
model           : 2
model name      : Intel(R) Celeron(R) CPU 2.40GHz
stepping        : 9
cpu MHz         : 2423.933
cache size      : 128 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 2
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe up pebs bts sync_rdtsc cid xtpr
bogomips        : 4851.34
clflush size    : 64

if we need only to display model name: 

$ cat /proc/cpuinfo|grep "model name"
model name      : Intel(R) Celeron(R) CPU 2.40GHz

In the case that there are multiple processors, info of second will continue after first one and so on... processor: 0, processor: 1,...