Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Wednesday, January 14, 2015

Bash - Find substring in a string

Very simple solution to find substring in a string, make script sub.sh:
#!/bin/bash
txt="This is a text"
substr="text"
if [[ $txt == *$substr* ]]
then
echo "String [$txt] contains substring [$substr]"
else
echo "String [$txt] doesn't contains substring [$substr]"
fi

Variable txt is a string which should (or not) contains string from variable substr.

Output:
$ ./sub.sh
String [This is a text] contains substring [text]

Change variable substr:
substr="t1xt"

Output:
$ ./sub.sh
String [This is a text] doesn't contains substring [t1xt]

Tuesday, January 13, 2015

Linux - Search for empty directories and files

To search for empty directories type:
$ find . -empty -type d
To delete empty directories:
$ find . -empty -type d -delete

If enter -type f instead of -type d, this command will search for empty files:
$ find . -empty -type f
to delete empty files:
$ find . -empty -type f -delete

Monday, January 12, 2015

Bash Script: Number of Arguments

Number of arguments is in variable $#, example is script numarg.sh

#!/bin/bash
echo "number of arguments: $#"

Executing script: 
$ ./numarg.sh arg1 arg2 arg3
Number of arguments: 3

Variable $# doesn't count script name, only arguments.

Arguments are stored in variables $1, $2... or in $@ as array.


Thursday, January 8, 2015

[Bash] Count Letters in a Text File

Bash script have very useful commands for Text File analysis. For this example we will use small part from Leo Tolstoy: War and Peace, available on Project Gutenberg:




This part is placed in file onepart.txt. To count letters all letters must have same case. Command tr will translate all letters to uppercase letters:
$ cat onepart.txt|tr a-z A-Z
Next, output is filtered. Command tr is useful here with first argument are switches -cd and seccond is letter to be filtered. Switch -c means complent and -d to delete, in short those two arguments means erase everything what is not letter in argument.
Pipelining we get:
$ cat onepart.txt|tr a-z A-Z|tr -cd A
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
Or for B, C etc...

$ cat 3.txt|tr a-z A-Z|tr -cd B
BBBB

$ cat 3.txt|tr a-z A-Z|tr -cd C
CCCCCCCCCCCCCCCCCCCCCCC

 $ cat 3.txt|tr a-z A-Z|tr -cd D
DDDDDDDDDDDDDDDDDDDDDDDDDD

And of course we need to count those letters. Command wc with switch -m do exactly that:

$ cat 3.txt|tr a-z A-Z|tr -cd A|wc -m
40

$ cat 3.txt|tr a-z A-Z|tr -cd B|wc -m
4

$ cat 3.txt|tr a-z A-Z|tr -cd C|wc -m
23

$ cat 3.txt|tr a-z A-Z|tr -cd D|wc -m
26
and, of course, script to count all letters occurances: 

#!/bin/bash

text=$(cat $1|tr a-z A-Z)
echo "Letter occurances:"

for l in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
do 
let=$(echo $text|tr -cd $l|wc -m)
echo "$l $let"
done
Script is a little bit slower because text is analyzed for every letter.

Monday, May 12, 2014

Bash - Substring

How to get substring in Bash script? Simple :)

if we have string:

$ a=123456789

substring from position 1 to 5 is

$ echo ${a:1:5}
23456

First position in a string is 0.

in general: ${varname:start_pos:length}


Saturday, May 10, 2014

Linux - Printing first or last 10 lines

There are many examples when we need to print first or last lines of some file. Commands head and tail ca help to do this.

Command head prints 10 first lines of file.

$ head file

or

$ cat file|head

There is command line argument -n, which says how many lines to be printed.

Example:

$ head -n 5 file

prints first 5 linest from file.

if number is negative, command head prints last n lines from file.

Command tail does oposite from command head: prints last 10 lines from a file.

$ tail file

same as command head there is argument -n which tells to command head how many last lines from a file is to be printed:

$ tail -n 7 file

prints last 7 lines from file.

If you want to print all lines in file excluded first n lines, command is:

$ tail -n +5 file

prints all lines in file, excluded first 5 lines.

Command tail is very useful in log analyzing, when you are not interested in whole file, but only last n lines.

Thursday, May 8, 2014

Linux - File Checksum

The purpose of file checksums is to validate integrity of files. There are many ways to do checksums on Linux, but the simplest is by using md5sum command (for MD5 checksum). Format is:
md5sum [options] [file1] ... [file n]

Options are:
-b or --binary - binary mode
-t or --text - text mode (default)
-c or --check - read file with checksums and check them

For test purpose make one file named test

$ md5sum test
ee10c66475e841d934c0e39d07650d4a  test

output md5 checksum to file:
$ md5sum test >checksum

check:
$md5sum -c checksum
test: OK

MD5 gives 128 bit checksum. For security related purposes is better to use SHA-2 algorithm implemented in: sha224sum, sha256sum, sha384sum and sha512sum programs.








Tuesday, May 6, 2014

Bash script: User Input

For user general input in variable we use command read:

#!/bin/bash
read -p "Enter first number:" num1
read -p "Enter second number:" num2
if [ num1 -eq num2 ]
then
   echo "Numbers are equal"
else
   if [ $num1 -lt $num2 ]
   then
      echo "Second number is larger"
   else
      echo "First number is larger"
   fi
fi

For entering exact expected word (example Yes or No) we use command select:

#!/bin/bash
echo "Do you want continue?"
select inp in "yes" "no"
do
   case $inp in
      yes ) echo "You want to continue"
              break
              ;;
      no ) echo "You want to exit"
             break
             ;;
   esac
done


Monday, May 5, 2014

Bash FOR loop

For loop is a structure that executes a number of times, generally known in advance. In Bash script syntax of FOR loop is:

for VARIABLE [in LIST]; do COMMANDS BLOCK; done

or

for VARIABLE [in LIST]
do
   COMMANDS_BLOCK
done

LIST is a list of words or numbers. If LIST is omitted $@ is implied.

Simple examples:
1. This example prints all files in directory

dirlist=$(ls)
for i in $dirlist
do
   echo $i
done


2. This example prints numbers from 1 to 10

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

or

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

3. This example prints number from 1 to 10 with step 2

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

4. This example prints 

Sunday, May 4, 2014

Bash - Exit code

1. Exit code from Bash script

argument of exit command is exit code
exit n
example (exit_example.sh)

#!/bin/bash
exit $1  #return argument 1 as return code

2. Bash last exit code

last exit code is in variable $?

#!/bin/bash
./exit_example.sh 2
echo $?

result is:
2

Saturday, May 3, 2014

Bash - Join Arrays

How to join arrays in Bash Script? Very simple :) Example:

#!/bin/bash
a1=(a b c d) #array 1
a2=(e f g h) #array 2
a=(${a1[@]} ${a2[@]}) #join array
echo ${a[@]}

Output is:
a b c d e f g h

Make array of arrays and result is joined array :)

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:~


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,...

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