Posts Tagged ‘bash’

This post will explain how to create users and mailboxes for Zarafa.

I’ve tested it with CentOS 5.4 X86_64 and Zarafa 6.30.9 but it will fit with nearly all installation.

First of all you have to create a test file with all the users you want to create, providing the following information :

  • username
  • password
  • email address
  • name
  • surname

For example :

johndoe   password    johndoe@example.com    John    Doe
janedoe    password   janedoe@example.com     Jane     Doe

Save the text file whatever you want.

The following is the shell script that read your text file and create all Zarafa users.

If you use the script, all the users will not be administrator, so you have to create manually and administrator.


#!/bin/bash
# Written by Matteo Predieri - m.predieri_AT_damsistemi_DOT_it
# Written by Riccardo Riva - r.riva_AT_damsistemi_DOT_it
#
# Simple and raw script to create zarafa users and mailboxes from a test file
#
# The full fill-in of the text file is mandatory
# So fill in both "Name" and "Surname" with the following syntax
#
# username    password    email    name    surname
#

USERS_LIST=/tmp/userlists.txt
ZARAFA_CMD=/usr/bin/zarafa-admin
LOGFILE=/tmp/zara_user_creation.log

echo "Zarafa User Creation Log" > $LOGFILE
what="user";

for item in $( cat $USERS_LIST ); do

 if [ $what = "user" ]; then
 user=$item;
 what="passwd";
 else
 if [ $what = "passwd" ]; then
 passwd=$item;
 what="email";
 else
 if [ $what = "email" ]; then
 email=$item;
 what="name";
 else
 if [ $what = "name" ]; then
 name=$item;
 what="surname";
 else
 surname=$item;

 echo "Result in creating User: $user, with password: $passwd, with email address: $email with Full Name: $name $surname :" >> $LOGFILE
 FULLNAME="'$name $surname'"
 $ZARAFA_CMD -c $user -p $passwd -e $email -f "$FULLNAME" -a0 >> $LOGFILE
 echo -ne  "-------------------------------\n" >> $LOGFILE
 what="user";
 fi

 fi
 fi

 fi
done

# --- End of file ---

You’ve done.

Hope this help

Bye
Riccardo

Print This Post Print This Post

This is a simple shell script that count all files in a local directory and all subdir. It doesn’t care what extention the file has, it will just recursively go through each directory and count any files within it, plus recurse into any more directories it finds. Give it as a parameter the directory you want to count.


#!/bin/sh
NUMBER=0
count ()
{
for temporary in $1/* ; do
if [ -d "$temporary" ] ; then
count "$temporary"
elif [ -f "$temporary" ] ; then
NUMBER=$(($NUMBER+1))
fi
done
}
count $1
echo "I found $NUMBER files, in the specified folder and all subfolders"

I know you should also did it with

ls -R | wc -l

but it was a shell exercise.

Hope this help

Bye
Riccardo

Print This Post Print This Post

The following is a great memo for a lot of bash shortcuts.

Original link : HowToGeek

CTRL Key Bound

Ctrl + a – Jump to the start of the line
Ctrl + b – Move back a char
Ctrl + c – Terminate the command
Ctrl + d – Exit the current shell
Ctrl + e – Jump to the end of the line
Ctrl + f – Move forward a char
Ctrl + h – Same as backspace
Ctrl + k – Delete to EOL
Ctrl + l – Clear the screen
Ctrl + r – Search the history backwards
Ctrl + R – Search the history backwards with multi occurrence
Ctrl + t : Swap the last two characters before the cursor
Ctrl + u – Delete backward from cursor
Ctrl + w : Delete the word before the cursor
Ctrl + xx – Move between EOL and current cursor position
Ctrl + x @ – Show possible hostname completions
Ctrl + z – Suspend/ Stop the command. “fg” restores the suspended command.

ALT Key Bound

Alt + < – Move to the first line in the history
Alt + > – Move to the last line in the history
Alt + ? – Show current completion list
Alt + * – Insert all possible completions
Alt + / – Attempt to complete filename
Alt + . – Yank last argument to previous command
Alt + b – Move backward
Alt + c – Capitalize the word
Alt + d – Delete word
Alt + f – Move forward
Alt + l – Make word lowercase
Alt + n – Search the history forwards non-incremental
Alt + p – Search the history backwards non-incremental
Alt + r – Recall command
Alt + t – Move words around
Alt + u – Make word uppercase
Alt + back-space – Delete backward from cursor

More Special Keybindings

$ 2T – All available commands(common) (2T means press the TAB twice)
$ (string)2T – All available commands starting with (string)
$ /2T – Entire directory structure including Hidden one
$ 2T – Only Sub Dirs inside including Hidden one
$ *2T – Only Sub Dirs inside without Hidden one
$ ~2T – All Present Users on system from “/etc/passwd”
$ $2T – All Sys variables
$ @2T – Entries from “/etc/hosts”
$ =2T – Output like ls or dir

Hope this help

Bye
Riccardo

Print This Post Print This Post

The following is a simple memo on how to use the three powerful tools above.
It will not be a complete guide, and you should find only a small part of the possible use, but this would be a review only for the most common use (for me) and would not go a deep dive.

Grep

grep : print lines matching a pattern (equals “grep -G” which is the default)
egrep : equals grep -E (interpret extended regexp)

grep -n : line numbered
grep -i : ignore case
grep -c : count matches
grep -v : print non-matching lines
grep -r : recursivity, read all files under each directory

grep pattern file.txt

Will display the lines containing the pattern

grep -c pattern file.txt

Will display how many lines contain the pattern

grep -i pattern file.txt

Will display the lines containing the pattern regardless of the case

grep -A 1 -B 1 pattern output.txt

Will print the one line before (-B) and one line after (-A) the matching pattern

Awk

awk '{ print $0 }' file

Output the content of the file

awk '{ print $2 }' file

Output the second field of data of the file, space is the default separator

awk -F ':' '{ print $2 }' file

Same but separator is “:”

Sed

cat file | sed -e 's/old_pattern/new_pattern/g'

sed would replace old_pattern by new_pattern in the output

cat file | sed -e '4,10s/old_pattern/new_pattern/g'

sed would replace old_pattern by new_pattern in the output between line 4 and 10

cat file | sed '/pattern/d'

Delete a pattern

cat file | sed '/pattern/!d'

Delete everything but the pattern (this equals grep “string_to_remove”)

Hope this help

Bye
Riccardo

Print This Post Print This Post

This would be another little memo of all the most used (by me) Input/output redirections and pipes.
As other times this would not be a complete guide, and would not go into the deep.

The base :

Standard input (stdin) : 0
Standard output (stdout) : 1
Error output (stderr) : 2

> : output redirection
< : input redirection
>> : append output
| : pass the output to the next utility
|| : execute the next command if the previous failed
&& : execute the next command only if the previous has succeded

Examples :

cat file.txt > /tmp/file.txt

If file.txt exists, the output will be written in /tmp/file.txt
If file.txt doesn’t exist, the error message will be output to the screen while nothing will be written to /tmp/file.txt (but the file will be created if not existing)

cat file.txt 2> /tmp/error_file.txt > /tmp/file.txt

In this case the error message would be written to /tmp/error_file.txt if the file doesn’t exist

cat file.txt 2> /tmp/error_file.txt >> /tmp/file.txt

The error output would still go in error_file.txt while the standard output would be APPENDED to file.txt

cat file1 > file2

Will copy the content of file1 to file2

 cat &gt; file3

Hello world

Will write “Hello world” to file3

[/bash]wc < file.txt > count_file.txt[/bash]

This would count the number of lines, words and bytes in file.txt (”wc < file.txt”) and output the result to count_file.txt

Several ways of counting lines etc. :
$ wc file.txt
7 2 17 file.txt
$ wc < file.txt
7 2 17
$ cat file.txt | wc
7 2 17

ls -l test10 > file.txt 2>&1

This would output anything under file.txt

<code>ls -l test10 2>&1 | mail -s “output in a mail” address@domain.com
Any output in a mail

Hope this help

Bye
Riccardo

Print This Post Print This Post

This pot will show some useful example on how to use the “grep” command to find an occurency in a Linux system.
Grep searches the input file (or files) for lines containing a match to a given pattern. Whe an occurency math it copies to standard input the line with the occurrency or you should rediret the output whatever you want.

You should simpy invoke grep with the following :

grep 'STRING' filename

The above is a very simple use f grep, infact it check only in a single file searching all the occurency for ‘STRING’.
You should also use it to find ‘STRING1 STRING2′ in all files in your current location, running ::

grep 'STRING1 STRING2' *

or if you want to make the same research on a given path (i.e. all files in /etc) you should use :

grep 'STRING1 STRING2' /etc/*

Notice the use of single quotes; This are not essential but in this example it was required since the name contains a space. Double quotes could also have been used in this example.

In case of too much occurency you should redirect the output o a file to make more comfortable the research, for example using :

grep 'STRING 1 STRING2" /etc/* > /tmp/grepresults.txt

The following is a simple list for Grep Regular Expression

grep can search for complicated pattern to find what you need using some special characters used to create a regular expression:

`.’ The period `.’ matches any single character.

`?’ The preceding item is optional and will be matched at most once.

`*’ The preceding item will be matched zero or more times.

`+’ The preceding item will be matched one or more times.

for example, a regular expression search would be :

grep "\<[A-Za-z].*" file

The search above will search for any word which begins with a letter upper or lower case.

For more details check :

man grep

Hope this help

Bye
Riccardo

Print This Post Print This Post

Very often it happens that I must give support to a colleague or a customer on a Linux machine.

It’s very difficult to spell all bash command I will use to check which could be the problem, especially by phone.

When I discover “screen” it was a revelation.

With screen http://www.gnu.org/software/screen/screen.html you should share a linux session with other people.

You should use screen by simply ask to the user you want to assist to type on the console the following command :

screen

So if you can connect to the machine (even with ssh) you should run the following command :

screen -x

to share the same bash session.

Hope this help

Bye

Riccardo

Print This Post Print This Post

The following is a simple script to monitor a Software Raid configuration on a Linux System.
It execute a /proc/mdstat check to search a ‘blocks_’ occurency which indicates problems on the Raid system and in case of match it notify the system administrator with a mail message.

It should be very useful for non-presidiated system, even if I always prefer Hardware Raid.


#!/bin/bash
#
# Script created by Riccardo Riva
# http://www.riccardoriva.com
#
# It check a Software Raid subsystem and notify by mail any occurency problem

# Define variable
LOG_FILE=/tmp/raid-check.log
SYSTEM=`uname --nodename`
MAILTO='systemadmin@mail.exp'

# Checking /proc/mdstat
cat /proc/mdstat | grep 'blocks.*_' > $LOG_FILE

# Define function in case of problems detected
if [ $? -eq 0 ]
then
echo "The $SYSTEM system has RAID failures on it." >> $LOG_FILE
echo "Below is the output from /proc/mdstat" >> $LOG_FILE
echo "===========================================" >> $LOG_FILE
cat /proc/mdstat >> $LOG_FILE
echo "===========================================" >> $LOG_FILE
cat $LOG_FILE | mail -s 'URGENT: RAID disk failure detected' $MAILTO
fi

# Deleting log file
rm -f >> $LOG_FILE

# Exit
exit 0

Save the file above as /usr/local/bin/raidcheck.sh  and assign to it correct permission and ownership with the folowing :

chmod 700 /usr/local/bin/raidcheck.sh
chown root:nobody /usr/local/bin/raidcheck.sh

Change the email address variable with a real email address who will receive the notification.

You should now schedule the script execution at every time interval you want (i.e. twice a day, or hourly if you are paranoic).

Remember that is better to be paranoic that have a degraded system with unrecoverable data.

Hope this help

Bye
Riccardo

Print This Post Print This Post

The following wuold be a simpe example to define a function in a bash shell script.
You should use it to execute the same set of command more time in a single shell script simply calling the function name and avoiding to type it all the time.

The following is a very simple example :

</pre>
#!/bin/sh

# Start define function "function01"

# Function name
function01 ()
# Function content
{
# Put the content here
}
# Stop define function

# insert whatever you want here

# call function
function01

# insert whatever you want here

# call function
function01
<pre>

Hope this help

Bye
Riccardo

Print This Post Print This Post

With the following command you could configure a proxy server (http and/or ftp) and use it for your current bash session or for apt.
This configuration will be lost when you close your shell.

export http_proxy=’http://user:password@proxy-server:port’
export ftp_proxy=’http://user:password@proxy-server:port’

Hope this help

Bye
Riccardo

Aliases in Linux provide shortcuts that can save you typing, let you build your own powerful commands, and make your command line life easier.

Background
I’ve found it very helpful to create aliases to make my command line life faster and. For instance, instead of always typing

ls -lah

to get a directory listing (ls), with ouptut in a list (-l) with show all files (-a) in a human readable output (-h), I’ve created an alias so I only have to type this: “l” (That’s the lower case letter “L”.)

Using aliases like this you can create anything from simple shortcuts like this to powerful custom commands.

Creating aliases is very easy. You can either enter them at the command line as you’re working, or more likely, you’ll put them in one of your startup files, like your .bashrc file, so they will be available every time you log in.

I created the l alias above by entering the following command into my .bashrc file:

alias l=”ls -lah”

As you can see, the syntax is very easy:

1. Start with the alias command
2. Then type the name of the alias you want to create
3. Then an = sign, with no spaces on either side of the =
4. Then type the command (or commands) you want your alias to execute when it is run. This can be a simple command, or can be a powerful combination of commands.

Sample aliases
To get you going, here is a list of sample aliases I use all the time. I’ve pretty much just copied them here from my .bashrc file:

alias l=”ls -lah”
alias lm=”ls -lah | more”
alias htmldir=”cd /var/www/html”
alias logsdir=”cd /var/log”
alias showfw=”iptables -Lnv”
alias dropfw=”iptables -F”

As you can see, you can get as creative as you want, and pipe commands together to do just about anything.

Because the Linux shell is very programmable and because the output of commands is very consistent and reliable, you can create your own aliases to do just about anything.

Hope this help

Bye
Riccardo

#!/bin/sh

if [ $(command | grep condition | wc -l) -eq 1 ]; then
command
fi

# Explanation:
#if (declare a opened function)
#[ $(    (create a variable from the construct output)
#| grep condition (check if there is the condition in the output)
#| wc -l    (do a condition "word count" in the grep output)
#- eq 1    (declare that variable must be equal to 1)
#];        (declare the variable close)
#then     (condition to do only if the condition is matched)
#command_to_do_when_condition_is_matched    (command to do when condition is matched)
#fi        (declare a closed function)

# So for example :
#
# DEST=”you@youremail.com”
# TEST1=”192.168.1.254″
# TEST2=”192.168.2.254″
# CONDITION1=”230″
# CONDITION2=”231″
#
# Test Primary Route
# if [ $(traceroute -n $TEST1 | grep $CONDITION1 | wc -l) -eq 1 ]; then
#  echo “Link with DESTINATION on PRIMARY ROUTE !” | mail -s “Link with DESTINATION on PRIMARY ROUTE !” $DEST
# fi
#
# Test Secondary Route
# if [ $(traceroute -n $TEST2 | grep $CONDITION2 | wc -l) -eq 1 ]; then
#  echo “Link with DESTINATION on SECONDARY ROUTE !” | mail -s “Link with DESTINATION on SECONDARY ROUTE !” $DEST
# fi

#####################################################

Hope this help

Bye
Riccardo

Contacts
Look at me at Linkedin Follow me on Twitter
My Flickr Albums My Facebook profile My YouTube Videos
SkypeMe My Linux Counter GMail me
Search
Google Search
Categories
Tag Cloud 3D
FeedBurner RSS

Visitors
Locations of visitors to this page
VMware related Blogs
The following are Blog sites with feeds I personally follow. When I'll have some spare time I will complete all Feed and Twitter links.