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.

Wednesday, October 29, 2014

Bash Script - Convert int to binary

There are many ways to convert integer variable to binary. Here is one:
#!/bin/bash
a=65535
out=""
while [ $a -gt 0 ]
do
  b=$(($a%2))
a=$(($a/2))
out="$b$out"
done
echo $out

Thursday, May 15, 2014

Linux - How to change default sshd port

There are many reasons why default port 22 for ssh is need to be changed. If you need to change default port or add other listening port:

1. open /etc/ssh/sshd_config with your favorite text editor
2. find line
Port 22

and

edit this value to your needs.

or if you want to add other listening port, simply add line with port. Example:

Port 22
Port 1000

after editing save config file and restart sshd:

$ sudo /etc/init.d/ssh restart

or

$ service sshd restart

dependant on system you use.

File /etc/ssh/sshd_config opened in editor



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}