Showing posts with label Shell Scripting. Show all posts
Showing posts with label Shell Scripting. Show all posts

Monday, 20 October 2014

WEBMIN- Managing Unix Systems Graphically

What is Webmin?

Webmin is a web-based interface for system administration for Unix. Using any modern web browser, you can setup user accounts, Apache, DNS, file sharing and much more.

Demo:

http://webmin-demo.virtualmin.com/   login: demo &  password: demo.

Download Link:

How to Install:

Install on RedHat/CentOS/Fedora:

If you are using the RPM version of Webmin, first download the file from the downloads page , or run the command :
[root@UMLinux1 ~]# wget http://prdownloads.sourceforge.net/webadmin/webmin-1.710-1.noarch.rpm

and then run the command

[root@UMLinux1 ~]# rpm -U webmin-1.710-1.noarch.rpm
The rest of the install will be done automatically to the directory /usr/libexec/webmin, the administration username set to root and the password to your current root password. You should now be able to login to Webmin at the URL http://localhost:10000/.Or if accessing it remotely, replace localhost with your system's IP address.

If you want to connect from a remote server and your system has a firewall installed, see this page for instructions on how to open up port 10000.

Install on Debian:

If you are using the DEB version of webmin, first download the file from the downloads page , or run the command :
[root@UMLinux1 ~]# wget http://prdownloads.sourceforge.net/webadmin/webmin_1.710_all.deb

then run the command :

[root@UMLinux1 ~]# dpkg --install webmin_1.710_all.deb
The install will be done automatically to /usr/share/webmin, the administration username set to root and the password to your current root password. You should now be able to login to Webmin at the URL http://localhost:10000/. Or if accessing it remotely, replace localhost with your system's IP address.

How to Stop& Start Webmin Services:

In order to start the Webmin service on CentOS (Linux) you will need to issue the following command:
[root@UMLinux1 ~]# service webmin start
You can check to make sure that Webmin is running by issuing the following command:
[root@UMLinux1 ~]# service webmin status
Webmin (pid 1729) is running
[root@UMLinux1 ~]#
If you wish to configure your server to ensure that the Webmin service is started at boot time you can issue the following command:
[root@UMLinux1 ~]# chkconfig --level 3 webmin on
To verify that Webmin will start at boot, issue the following command:
[root@UMLinux1 ~]# chkconfig --list webmin
webmin 0:off 1:off 2:off 3:on 4:off 5:off 6:off
[root@UMLinux1 ~]#
In the previous listing, Webmin is listed to start in run level 3, which is the default run level that the dedicated servers boot into.

Friday, 10 October 2014

Expressions used with if condition in shell scripts

1-1. Primary Expressions

PrimaryMeaning
[ -a FILE ]True if FILE exists.
[ -b FILE ]True if FILE exists and is a block-special file.
[ -c FILE ]True if FILE exists and is a character-special file.
[ -d FILE ]True if FILE exists and is a directory.
[ -e FILE ]True if FILE exists.
[ -f FILE ]True if FILE exists and is a regular file.
[ -g FILE ]True if FILE exists and its SGID bit is set.
[ -h FILE ]True if FILE exists and is a symbolic link.
[ -k FILE ]True if FILE exists and its sticky bit is set.
[ -p FILE ]True if FILE exists and is a named pipe (FIFO).
[ -r FILE ]True if FILE exists and is readable.
[ -s FILE ]True if FILE exists and has a size greater than zero.
[ -t FD ]True if file descriptor FD is open and refers to a terminal.
[ -u FILE ]True if FILE exists and its SUID (set user ID) bit is set.
[ -w FILE ]True if FILE exists and is writable.
[ -x FILE ]True if FILE exists and is executable.
[ -O FILE ]True if FILE exists and is owned by the effective user ID.
[ -G FILE ]True if FILE exists and is owned by the effective group ID.
[ -L FILE ]True if FILE exists and is a symbolic link.
[ -N FILE ]True if FILE exists and has been modified since it was last read.
[ -S FILE ]True if FILE exists and is a socket.
[ FILE1 -nt FILE2 ]True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not.
[ FILE1 -ot FILE2 ]True if FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not.
[ FILE1 -ef FILE2 ]True if FILE1 and FILE2 refer to the same device and inode numbers.
[ -o OPTIONNAME ]True if shell option "OPTIONNAME" is enabled.
[ -z STRING ]True of the length if "STRING" is zero.
[ -n STRING ] or [ STRING ]True if the length of "STRING" is non-zero.
[ STRING1 == STRING2 ] True if the strings are equal. "=" may be used instead of "==" for strict POSIX compliance.
[ STRING1 != STRING2 ] True if the strings are not equal.
[ STRING1 < STRING2 ] True if "STRING1" sorts before "STRING2" lexicographically in the current locale.
[ STRING1 > STRING2 ] True if "STRING1" sorts after "STRING2" lexicographically in the current locale.
[ ARG1 OP ARG2 ]"OP" is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if "ARG1" is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to "ARG2", respectively. "ARG1" and "ARG2" are integers.

Expressions may be combined using the following operators, listed in decreasing order of precedence:

1-2. Combining expressions

OperationEffect
[ ! EXPR ]True if EXPR is false.
[ ( EXPR ) ]Returns the value of EXPR. This may be used to override the normal precedence of operators.
[ EXPR1 -a EXPR2 ]True if both EXPR1 and EXPR2 are true.
[ EXPR1 -o EXPR2 ]True if either EXPR1 or EXPR2 is true.
The [ (or test) built-in evaluates conditional expressions using a set of rules based on the number of arguments. Just like the if is closed with fi, the opening square bracket should be closed after the conditions have been listed.

Thursday, 19 June 2014

How to Convert OpenSSH to SSH2 and vise versa

The program SSH (Secure Shell) provides an encrypted channel for logging into another computer over a network, executing commands on a remote computer, and moving files from one computer to another. SSH provides strong host-to-host and user authentication as well as secure encrypted communications over the Internet.

SSH2 is a more secure, efficient, and portable version of SSH .

Connecting two servers running different type of SSH can be a danting task if you does not know how to convert the key. In this article ,we are going to learn about how to convert  keys   SSH( OpenSSH) to SSH2.

How to Generate OpenSSH(SSH v1) key :

umadm@umixserv1 [/home/umadm/.ssh]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/umadm/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/umadm/.ssh/id_rsa.
Your public key has been saved in /home/umadm/.ssh/id_rsa.pub.
The key fingerprint is:
5b:ac:ea:c3:25:cf:2d:31:a2:aa:83:76:4b:a2:c9:eb umadm@umixserv1
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|         .       |
|        S o      |
|. o   . .+       |
|+o o + oo        |
|Bo.   =.         |
|#Eo..oo.         |
+-----------------+
umadm@umixserv1 [/home/umadm/.ssh]$
Here we get two encrypted keys  callled   private key( called id_rsa) and public key id_rsa.pub  undr ~$HOME/.ssh directory.
  
You can generate dsa key by using below command.
#ssh-keygen -t dsa

Convert SSH2 to  OpenSSH(SSH):


The command below can be used to convert an SSH2 private key into the OpenSSH format:
ssh-keygen -i -f path/to/private.key > path/to/new/opensshprivate.key
The command below can be used to convert an SSH2 public key into the OpenSSH format:
ssh-keygen -i -f path/to/publicsshkey.pub > path/to/publickey.pub
Here  -i ==> SSH to read an SSH2 key and convert it into the OpenSSH format

Convert OpenSSH(SSH) to SSH2:

The  reverse  process to convert an OpenSSH key into the SSH2 format in the event that a client application requires the other format. This can be done using the following command:

OpenSSH to SSH2 Private key conversion:
ssh-keygen -e -f path/to/opensshprivate.key > path/to/ssh2privatekey/ssh2privatekey
OpenSSH to SSH2 Public key conversion:
ssh-keygen -e -f path/to/publickey.pub > path/to/ssh2privatekey/ssh2publickey.pub
Here  -e ==> SSH to read an OpenSSH key file and convert it to SSH2 format

Note:If you need passwordless authentication  b/w two different hosts , you need to convert the publickey as per the destination server SSH version and  append the public key to   ~/.ssh/authorized_keys or  ~/.ssh2/authorized_keys at destination server.

Friday, 25 April 2014

Unix /Linux Mail Command Tutorial with Examples

unix-linux-mail-command-tutorial
The mail/Mail/mailx  commands are used in unix flavoured operating systems like Linux, HPUX, AIX,Linux and many more unix based systems  are  used to send emails to the users, to read the received emails, to delete the emails etc.

It would be very usefull  when you are working with shell scripts. A good application for using mail/mailx would be to send alerts, or process a file and then email it to somebody, extract data from your database/application  and email the resulting data or file, etc.



The syntax of mail command is:
mail [options] to-address [-- sendmail-options]

-v : Verbose mode. Delivery details are displayed on the terminal.
-s : Specify the subject of the mail
-c : Send carbon copies of the mail to the list of users. This is like cc option in Microsoft outlook.
-b : Send blind copies of the mail to the list of users. This is like bcc option in outlook.
-f : Read the contents of the mailbox
-e : Tests for the presence of mail in the system mailbox.
-F : Records the message in a file named after the recipient.
-r : Specify the from address in send mail options.
-u : Specifies an abbreviated equivalent of doing mail -f /var/spool/mail/UserID.

1)To start the Mail program and list the messages in your mailbox:

# mail
The mail command lists every messages in your system mailbox. The mail system then displays the mailbox prompt (?) to indicate waiting for input.

When you see this prompt, enter any mailbox subcommand. To see a list of subcommands, type:
?
This entry lists the Mail subcommands.

2) Sending  email to  a user:

# echo "Test of Mail body" | mail -s "Mail subject" [email protected]

Here the echo statement is used for specifying the body of the email.
The -s option is used for specifying the mail subject. The mail command sends the email to the user [email protected]
another way is
# mail -s "Mail subject" [email protected]
in this example you are then expected to type in your message, followed by an "control-D" at the beginning of a line. To stop simply type dot (.) as follows:
Hi,
This is a test
.
Cc:
if you wish to send to multiple mail users just add the mail ids side by side with spaces

Lets re-write above command for multi recipients
# echo "Test of Mail body" | mail -s "Mail subject" recipient[email protected]  [email protected] [email protected]

3) Sending contents of a text file

you can send the contets in two ways using cat/echo or using a input redirect < operator

Lets say for example here we need send contents of  somefile.txt  through mail ,
# cat  somefile.txt  | mail -s "Mail subject" "[email protected],[email protected]"
# mail -s "Mail subject" "[email protected],[email protected]" < somefile.txt

4) Mail Usage with CC & Bcc :

 Using the cc and bcc option You can copy the emails to more number of users by using the -c & -b options. An example is shown below:
# echo "something" | mailx -s "subject" -b [email protected] -c [email protected]  -r [email protected] [email protected]
# mail -s "Mail subject" -c "[email protected]" -b "[email protected]" "[email protected]" < somefile.txt

5) Attaching files:

The mail command does not provide an option for attaching files.

There is a workaround for attaching files using the uuencode command. Pipe the output of uuencode command for attaching files.
 # uuencode attachment-file attachment-file | mail -s "Mail subject" "[email protected]" < somefile.txt

Working with Mailbox in the server

1) To start the Mail program and list the messages in your mailbox:

# mail
The mail command lists every messages in your system mailbox. The mail system then displays the mailbox prompt (?) to indicate waiting for input.

When you see this prompt, enter any mailbox subcommand. To see a list of subcommands, type:
?

Another way of viewing the emails is using the -f option. This is shown below:
# mail -f /var/spool/mail/user
Mail version 8.1 6/6/93.  Type ? for help.
"/var/spool/mail/user": 2 messages 2 new
>N  1 root@hostname  Tue May 17 00:00  21/1013  "Mail subject 1"
 N  2 root@hostname  Wed May 18 00:00  21/1053  "Mail subject 2"
&
From the above output, you can see that, it displays the from-address, date and subject of the emails in the inbox. It also displays the ampersand (&) prompt at the end. To go back to the main prompt, type CTRL+z or CTRL+d depending on your operating system and press enter. The ampersand prompt allows you to read, reply, navigate and delete the emails.

2. Reading an email.

To read the Nth email, just enter the mail number at the ampersand prompt and press enter. This is shown below:
> mail -f /var/spool/mail/user

Mail version 8.1 6/6/93.  Type ? for help.
"/var/spool/mail/user": 2 messages 2 new
>N  1 root@hostname  Tue May 17 00:00  21/1013  "Mail subject 1"
 N  2 root@hostname  Wed May 18 00:00  21/1053  "Mail subject 2"
&2
Message 2:
From root@hostname  Wed May 18 00:00  21/1053
---------------
Subject: Mail subject 2
------------

This displays the second email details.

3. Navigating through inbox emails. 

To go to the next email, enter the + symbol. To go back to the previous email, enter the - symbol at the ampersand prompt.
&-
Message 1:
From root@hostname  Tue May 17 00:00  21/1013
---------------
Subject: Mail subject 1
------------

4. Replying email. 

Once you have read an email, you can give reply to the mail by typing "reply" and pressing enter.
&reply
To: root@hostname
    root@hostname
Subject: Re: Mail subject1

5. Deleting emails. 

You can delete a read email by typing the d and pressing enter. You can also specify the email numbers to d option for deleting them.
To delete read email
&d
To delete emails 1 and 2
&d 1 2
To delete range emails from 10 to 30
&d 10-30
To delete all emails in the mbox (mail box)
&d *

Saturday, 8 March 2014

A Quick Guide To Unix Shell Scripting

1) what is shell script ?

Normally shells are interactive. It means shell accept command from you (via keyboard) and execute them. But if you use command one by one (sequence of 'n' number of commands) , the you can store this sequence of command to text file and tell the shell to execute this text file instead of entering the commands. This is know as shell script.
Shell script defined as:

"Shell Script is series of command written in plain text file. Shell script is just like batch file is MS-DOS but have more power than the MS-DOS batch file."

2)  Shebang:

Naturally, a shell script should start with a line such as the following:
#!/bin/bash
This indicates that the script should be run in the bash shell regardless of which interactive shell the user has chosen. This is very important, since the syntax of different shells can vary greatly.

3) How to write shell script ?

Now i will walk you through  how to write shell script,execute them etc.We will getting started with writing small shell script, that will print "Hello UnixMantra" on screen. Before starting with this you should know.
Following steps are required to write shell script:
(1) Use any editor like vi or exedit to write shell script.

(2) After writing shell script set execute permission for your script as follows

syntax: chmod permission your-script-name

Examples:
$ chmod +x your-script-name
$ chmod 755 your-script-name

Note: This will set read write execute(7) permission for owner, for group and other permission is read and execute only(5).

(3) Execute your script as

syntax: bash your-script-name
sh your-script-name
./your-script-name

Examples:
$ bash bar
$ sh bar
$ ./bar

NOTE: In the last syntax ./ means current directory, But only . (dot) means execute given command file in current shell without starting the new copy of shell, The syntax for . (dot) command is as follows

Syntax: . command-name

Example:
$ . foo

Now you are ready to write first shell script that will print "Hello UnixMantra" on screen. See the common vi command list , if you are new to vi.

$ vi firstscript
#
# My first shell script
#
clear
echo "Hello UnixMantra"

After saving the above script, you can run the script as follows:
$ ./firstscript

This will not run script since we have not set execute permission for our script first; to do this type command

$ chmod 755 firstscript
$ ./firstscript

4) Commenting Commands:

Any line beginning with a hash '#' character in the first column is taken to be a comment and is ignored. The only exception is the first line (shebang #!/bin/sh)  in the file, where the comment is used to indicate which shell should be used.

5) Shell Variables:

Like every programming language, shells support variables. Shell variables may be assigned values, manipulated, and used. Some variables are automatically assigned for use by the shell.
there are two types of variable:

(1) System variables - Created and maintained by Unix OS itself. This type of variable defined in CAPITAL LETTERS.
(2) User defined variables (UDV) - Created and maintained by user. This type of variable defined in lower letters.
Any programming language needs variables. You define a variable as follows:
Y="hello"
and refer to it as follows:
$Y
More specifically, $Y is used to denote the value of the variable Y.

$ no=10
# this is ok
$ 10=no
# Error, NOT Ok, Value must be on right side of = sign.

To define variable called 'vech' having value car
$ vech=car

To define variable called n having value 10
$ n=10
Caution: Do not modify System variable this can some time create problems.
Yon can  print the value of the variable or command using  "echo"  or  "print"
#echo "$Y"
hello
I always suggest you to use "curly braces {}" to protect the variables, we have a good advantage when grabbing the actual values of variables.
Eg:
# X=Hello
#echo "$XWorld"
There wont be any output  by above command because the shell looks for "Xworld" as variable rather X.We can avoid this embracing situation using curly braces.
#echo "${X}World"
HelloWorld

6)  Analysing  quotes:

There are three types of quotes

Quotes
Name
Meaning
"
Double Quotes
"Double Quotes" - Anything enclose in double quotes removed meaning of that characters (except \ and $).
'
Single quotes
'Single quotes' - Enclosed in single quotes remains unchanged.
`
Back quote

`Back quote` - To execute command
Eg:
MY_VALUE=Hello
$ echo '$MY_VALUE'
$MY_VALUE
$ echo "$MY_VALUE"
Hello
$ echo "Today is date"
Can't print message with today's date.
$ echo "Today is `date`".
It will print today's date as, Today is  Fri Mar 07 15:35:08 EDT 2014

7)  Conditional Statement:

if or elif
Conditionals are used where an action is appropriate only under certain circumstances. The most frequently used conditional operator is the if-statement. For example, the shell below displays the contents of a file on the screen using cat, but lists the contents of a directory using ls.
#!/bin/sh
# show script
if [ -d $1 ]
then
  ls $1
else
  cat $1
fi
Here, we notice a number of points:

  • The if-statement begins with the keyword if, and ends with the keyword fi (if, reversed).
  • The if keyword is followed by a condition, which is enclosed in square brackets. In this case, the condition -d $1 may be read as: if $1 is a directory.
  • The line after the if keyword contains the keyword then.
  • Optionally, you may include an else keyword.
If the condition is satisfied (in this case, if $1 is a directory) then the commands between the then and else keywords are executed; if the condition isn't satisfied then the commands between the else and fi keywords are executed. If an else keyword isn't included, then the commands between the then and fi keywords are executed if the condition is true; otherwise the whole section is skipped.

Type1
Type2
Type3

if condition

then

    statement1

    statement2
    ..........
fi

if condition
then
    statement1
    statement2
    ..........
else
    statement3
fi

"if condition1
then
    statement1
    statement2
    ..........
elif condition2
then
    statement3
    statement4
    ........   
elif condition3
then
    statement5
    statement6
    ........   
fi

To run simple test

If you wish to specify an alternate action when the condition fails

it is possible to test for another condition if the first "if" fails. Note that any number of elifs can be added.
The Test Command and Operators
The command used in conditionals nearly all the time is the test command. Test returns true or false (more accurately, exits with 0 or non zero status) depending respectively on whether the test is passed or failed. It works like this:
test operand1 operator operand2
for some tests, there need be only one operand (operand2) The test command is typically abbreviated in this form:
[ operand1 operator operand2 ]
To bring this discussion back down to earth, we give a few examples:
#!/bin/bash
X=3
Y=4
empty_string=""
if [ $X -lt $Y ] # is $X less than $Y ? 
then
 echo "\$X=${X}, which is smaller than \$Y=${Y}"
fi

if [ -n "$empty_string" ]; then
 echo "empty string is non_empty"
fi

if [ -e "${HOME}/.surya" ]; then    # test to see if ~/.surya exists
 echo "you have a .surya file"
 if [ -L "${HOME}/.surya" ]; then   # is it a symlink ?  
  echo "it's a symbolic link
 elif [ -f "${HOME}/.surya" ]; then  # is it a regular file ?
  echo "it's a regular file"
 fi
else
 echo "you have no .surya file"
fi
A brief summary of test operators
Here's a quick list of test operators. It's by no means comprehensive, but its likely to be all you'll need to remember (if you need anything else, you can always check the bash manpage ... )

operator produces true if... number of operands
-n operand non zero length 1
-z operand has zero length 1
-d there exists a directory whose name is operand 1
-f there exists a file whose name is operand 1
-eq the operands are integers and they are equal 2
-neq the opposite of -eq 2
= the operands are equal (as strings) 2
!= opposite of = 2
-lt operand1 is strictly less than operand2 (both operands should be integers) 2
-gt operand1 is strictly greater than operand2 (both operands should be integers) 2
-ge operand1 is greater than or equal to operand2 (both operands should be integers) 2
-le operand1 is less than or equal to operand2 (both operands should be integers) 2
Case Statements:
The case construct has the following syntax:
case word in
pattern) list ;;
...esac      
An example of this should make things clearer:
!#/bin/sh
case $1
in
1) echo 'First Choice';;
2) echo 'Second Choice';;
*) echo 'Other Choice';;
esac
"1", "2" and "*" are patterns, word is compared to each pattern and if a match is found the body of the corresponding pattern is executed, we have used "*" to represent everything, since this is checked last we will still catch "1" and "2" because they are checked first. In our example word is "$1", the first parameter, hence if the script is ran with the argument "1" it will output "First Choice", "2" "Second Choice" and anything else "Other Choice". In this example we compared against numbers (essentially still a string comparison however) but the pattern can be more complex, see the SH man page for more information.

8) Looping Commands:

Whereas conditional statements allow programs to make choices about what to do, looping commands support repetition. Many scripts are written precisely because some repetitious processing of many files is required, so looping commands are extremely important.

Loops are constructions that enable one to reiterate a procedure or perform the same procedure on several different items. There are the following kinds of loops available in bash

  • for loops
  • while loops
'For' loops
The syntax for the for loops is best demonstrated by example.
#!/bin/bash
for X in red green blue
do
 echo $X
done
The for loop iterates the loop over the space seperated items. Note that if some of the items have embedded spaces, you need to protect them with quotes. Here's an example:
#!/bin/bash
colour1="red"
colour2="light blue"
colour3="dark green"
for X in "$colour1" $colour2" $colour3"
do
 echo $X
done
Can you guess what would happen if we left out the quotes in the for statement ? This indicates that variable names should be protected with quotes unless you are pretty sure that they do not contain any spaces.
'While' Loops
While loops iterate "while" a given condition is true. An example of this:
#!/bin/bash
X=0
while [ $X -le 20 ]
do
 echo $X
 X=$((X+1))
done
This raises a natural question: why doesn't bash allow the C like for loops
for (X=1,X<10; X++)
As it happens, this is discouraged for a reason: bash is an interpreted language, and a rather slow one for that matter. For this reason, heavy iteration is discouraged.

9) Functions:

When program gets complex we need to use divide and conquer technique. It means whenever programs gets complicated, we divide it into small chunks/entities which is know as function.
Function is series of instruction/commands. Function performs particular activity in shell i.e. it had specific work to do or simply say task. To define function use following syntax:
Syntax:
           function-name ( )
           {
                command1
                command2
                .....
                ...
                commandN
                return
           }
Where function-name is name of you function, that executes series of commands. A return statement will terminate the function. Example:
Type SayHello() at $ prompt as follows
$ SayHello()
{ echo "Hello $LOGNAME, Have nice computing"
return}
To execute this SayHello() function just type it name as follows:
$ SayHello
Hello surya, Have nice computing.
This way you can call function.

10)  Command Substitution:

Command Substitution is a very handy feature of the bash shell. It enables you to take the output of a command and treat it as though it was written on the command line. For example, if you want to set the variable X to the output of a command, the way you do this is via command substitution.

There are two means of command substitution: brace expansion and backtick expansion.
Brace expansion workls as follows:
$(commands) expands to the output of commands

This permits nesting, so commands can include brace expansions

Backtick expansion expands
`commands` to the output of commands
An example is given;:
#!/bin/bash
files="$(ls)"
web_files=`ls public_html`
echo "$files"      # we need the quotes to preserve embedded newlines in $files
echo "$web_files"  # we need the quotes to preserve newlines 
X=`expr 3 \* 2 + 4` # expr evaluate arithmatic expressions. man expr for details.
echo "$X"
The advantage of the $() substitution method is almost self evident: it is very easy to nest. It is supported by most of the bourne shell varients (the POSIX shell or better is OK). However, the backtick substitution is slightly more readable, and is supported by even the most basic shells (any #!/bin/sh version is just fine)

Note that if strings are not quote-protected in the above echo statement, new lines are replaced by spaces in the output.

11)  Shell Arithmetic:

Use to perform arithmetic operations.
Syntax:
expr op1 math-operator op2
Examples:
$ expr 1 + 3
$ expr 2 - 1
$ expr 10 / 2
$ expr 20 % 3
$ expr 10 \* 3
$ echo `expr 6 + 3`

12)  How to de-bug the shell script?

While programming shell sometimes you need to find the errors (bugs) in shell script and correct the errors (remove errors - debug). For this purpose you can use -v and -x option with sh or bash command to debug the shell script.
General syntax is as follows:
sh   option   { shell-script-name }
OR
bash   option   { shell-script-name }

Option can be

-v Print shell input lines as they are read.
-x After expanding each simple-command, bash displays the expanded value of PS4 system variable, followed by the command and its expanded arguments.

Thursday, 23 January 2014

" $0" Parameter Shell Scripting

$0 is one of the Bash special parameters. It can only be referenced as follows (just an example as there are various ways to reference and use $0 while scripting) :
echo "$0"
echo "Usage: $0 fileName"
However, assignment to it is not allowed:
0=foo

Purpose

$0 expands to the name of the shell or shell script. This is set at shell initialization. If bash is invoked with a file of commands, $0 is set to the name of that file. It is often used to display script usage message:
#!/bin/bash
_file="$1"
 
 # if filename not supplied at the command prompt
# display usae message and die
[ $# -eq 0 ] && { echo "Usage: $0 filename"; exit 1; }
 
echo "Script name: $0"
echo "\$1 = $1, so \$_file set to $1"
 
# if file not found, display an error and die
[ ! -f "$_file" ] && { echo "$0: $_file file not found."; exit 2; }
 
# if we are here, means everything is okay
echo "Processing $_file..."
Save and close the file. You can run it as follows:
chmod +x test_1.sh 
./test_1.sh
Sample outputs:
Usage: ./test_1.sh filename 
Now, try to pass /etc/passwd filename :
./test_1.sh /etc/passwd
Sample outputs:
./test_1.sh /etc/passwd
Script name: ./test_1.sh
$1 = /etc/passwd, so $_file set to /etc/passwd
Processing /etc/passwd...
Finally, try to pass any /nonexistencefile.txt filename :
./test_1.sh /nonexistencefile.txt
Sample outputs:
Script name: ./test_1.sh
$1 = /nonexistencefile.txt, so $_file set to /nonexistencefile.txt
./test_1.sh: /nonexistencefile.txt file not found.
If bash is started with the -c option, then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the file name used to invoke bash, as given by argument zero.

Saturday, 24 August 2013

script to check if a string is a palindrome?

A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction.

Here is a script which will tell you whether a given string is a palindrome or not .

Script:
#!/usr/bin/ksh
len=0
i=1
echo -n "Enter a String: "
read str
len=`echo $str | wc -c`
len=`expr $len - 1`
halfLen=`expr $len / 2`
while [ $i -le $halfLen ]
do
c1=`echo $str|cut -c$i`
c2=`echo $str|cut -c$len`
if [ $c1 != $c2 ] ; then
echo "string is not palindrome"
exit
fi
i=`expr $i + 1`
len=`expr $len - 1`
done
echo "String is Palindrome"

Tuesday, 23 July 2013

PuTTYCM (PuTTY Connection Manager) alternatives

PuttyCM (Putty Connection Manager) alternatives:

This post is in continuation with my earlier post  PuttyCM , we have some alternatives to puttycm , you can try them as well.
1) SuperPutty:
This is almost similar to the PuTTY CM. Also, it uses PuTTY as the back-end program to process. You can use different tabs in the same window, here. It allows you to see multiple connections.
There is no difference between PuTTY and this. Both look similar and perform, too. If you are familiar with the former one, the transition to this will be completely effortless and allows the PuTTY SSH Client to be opened in Tabs. Additionally there is support for SCP to transfer files.

Features

  • Docking user interface allows personalized workspace and managing multiple PuTTY sessions easy
  • Export/Import session configuration
  • Upload files securely using the scp or sftp protocols
  • Layouts
  • Supports PuTTY session configurations including Private Keys
  • Supports SSH, RLogin, Telnet and RAW protocols
  • Supports local shell via MinTTY or puttycyg
  • Supports KiTTY
Download SuperPuTTy

2) Multi-Tabbed Putty:
Multi-Tabbed putty is another free application that uses Putty or Kitty (forked version of Putty) in background. It provides clear tabbed user interfaces for each connection. The servers are grouped in a sidebar which is similar to Putty connection Manager. It also allows detaching of tabs and converting them into a general Putty window.

Multi-Tabbed Putty

3) Poderosa Connection Manager:
This is something different. This application does not add on to Putty, and works independently. Users can split the window space into multiple panes and allocate each to a different connection. Telnet and SSH are used to establish connections. Moreover, developers can enhance the functions of Poderosa by using and create new plugins.

Download Poderosa

4)   Putty tab manager :
Windows tool for managing multiple PuTTY instances in a single tabbed window. PuTTY Tab Manager allows you to run multiple sessions of PuTTY , known ssh and telnet client for windows in one window with tabs.
  • One is an executable does not need installation.
  • Platforms  Win32 , you do not need MS. NET framework. 
  • Unlimited tabs.
  • PuTTY menu for each open session.
  • Multilingual (English, Spanish).
  • Support KiTTY  (kitty.exe), "fork" (branch) of PuTTY.
  • Portable version.
  • Send scripts
  • Automatic capture mode . Catch all PuTTY sessions open or opening.
  • Option to launch sessions from a file ( -f <file>).
  • Hotkeys  
    • Add tab [Ctrl] + [Ins] / [ Ctrl] + [+]
    • Next  tab: [Ctrl] + [Tab] / [ Ctrl] + [Page Down]
    • previous tab [Ctrl] + [Shift] + [Tab] / [Ctrl] + [Page Up]
Download PuTTYTabManager

5) AutoPutty :
AutoPuTTY is a simple connection manager / launcher -
It's written in C# so you'll need at least Microsoft .NET Framework Version 2.0
What you can do with it:
- Manage a server list and connect thru PuTTY, WinSCP, Microsoft Remote Desktop and VNC (only VNC    3.3 encryption is supported for passwords yet)
- Easily connect to multiple servers at once
- Import a list from a simple text file
- Protect the application startup with a password (note that the list is always encrypted)
Download AutoPuTTY

6) Tunnelier:
SSH and SFTP client for Windows incorporates:
  • one of the most advanced graphical SFTP clients;
  • state-of-the-art terminal emulation with support for the bvterm, xterm, and vt100 protocols;
  • support for corporation-wide single sign-on using SSPI (GSSAPI) Kerberos 5 and NTLM user authentication, as well as Kerberos 5 host authentication;
  • support for RSA and DSA public key authentication with comprehensive user keypair management;
  • powerful SSH port forwarding capabilities, including dynamic forwarding through integrated SOCKS and HTTP CONNECT proxy;
  • powerful command-line parameters which make the SSH client highly customizable and suitable for use in specific situations and controlled environments;
  • an advanced, scriptable command-line SFTP client (sftpc);
  • a scriptable command-line remote execution client (sexec) and a command-line terminal emulation client (stermc);
  • an FTP-to-SFTP bridge allowing you to connect to an SFTP server using legacy FTP applications;
  • Bitvise SSH Server remote administration features;
  • single-click Remote Desktop forwarding.
Download Tunnelier


7) SecureCRT:
SecureCRT® combines rock-solid terminal emulation with the strong encryption, broad range of authentication options, and data integrity of the Secure Shell protocol for secure network administration and end user access.

Download SecureCRT
8) mRemote:
mRemoteNG is a fork of mRemote, an open source, tabbed, multi-protocol, remote connections manager. mRemoteNG adds bug fixes and new features to mRemote.
It allows you to view all of your remote connections in a simple yet powerful tabbed interface.
mRemoteNG supports the following protocols:
  • RDP (Remote Desktop/Terminal Server)
  • VNC (Virtual Network Computing)
  • ICA (Citrix Independent Computing Architecture)
  • SSH (Secure Shell)
  • Telnet (TELecommunication NETwork)
  • HTTP/HTTPS (Hypertext Transfer Protocol)
  • rlogin
  • Raw Socket Connections
Download mRemote


Wednesday, 5 June 2013

Get exit status of process that's piped to another

I have two processes foo and bar, connected with a pipe:
$ foo
bar
bar always exits 0;
I'm interested in the exit code of foo. Is there any way to get at it?

Ans:

There are 3 ways of doing this. However your current setup should work. The reason here being that the grep won't match anything if the command fails, so grep will return with status 1 (unless the program always shows that text no matter what).

Pipefail

The first way is to set the pipefail option. This is the simplest and what it does is basically set the exit status $? to the exit code of the last program to exit non-zero (or zero if all exited successfully).
# false |rue; echo $?
0
# set -o pipefail
# false |true; echo $?
1

$PIPESTATUS

Bash also has a variable called $PIPESTATUS which contains the exit status of all the programs in the last command.
# true |true; echo "${PIPESTATUS[@]}"
0 0
# false |true; echo "${PIPESTATUS[@]}"
1 0
# false |true; echo "${PIPESTATUS[0]}"
1
# true |false; echo "${PIPESTATUS[@]}"
0 1You can use the 3rd command example to get the specific value in the pipeline that you need.

This solution might not be available though. I think $PIPESTATUS might have been added in a fairly recent version of bash, and your OS may not have it.

Separate executions

This is the most unwieldy of the solutions. Run each command separately and capture the status
# OUTPUT="$(haconf -makerw)"
# STATUS_HACONF="$?"
# printf '%s' "$OUTPUT"
grep -iq "Cluster already writable"

Saturday, 18 May 2013

Show which hdisk(s) each of your filesystems reside on for AIX

Here is a small script to map out which hdisk(s) each of your filesystems reside on for AIX: 
#!/bin/ksh
for vg in `lsvg -o`; do
        for fs in `lsvgfs $vg`; do
                printf "%-22s" $fs;
                for disk in `lsvg -p $vg | tail +3 | awk '{print $1}'`; do
                        lspv -l $disk | grep -q " ${fs}$" && printf "%-8s" $disk;
                done;
                echo
        done;
done

Here is what the output looks like:


Saturday, 4 May 2013

Setting a Custom Prompt in Ksh

Customizing the prompt, a.k.a. $PS1, is usually the first thing I do when I'm trying a new shell; if you are using bash  there are many resources on the internet that teaches you how to do it.

But what if you are using a less-known shell like the Korn Shell? Usually the informations are outdated or incomplete. This tutorial is intended for the original ksh93, if you don't know which version you are running run.
ksh --version
# version         sh (AT&T Research) 93u 2011-02-08
or
echo ${.sh.version}
# Version JM 93u 2013-03-07
or
echo $KSH_VERSION
# Version JM 93u 2013-03-07

Basic prompt

Let's start with a very simple example, I set the prompt to the classic user@host $
export PS1='$(logname)@$(hostname -s) $ '
$(logname) returns the output of the command enclosed in parentheses.
To show the current path use the $PWD variable.
export PS1='$(logname)@$(hostname -s):$PWD $ '
The result is
umadm@unixmantra:/home/umadmin $
A black-and-white (in my case, grey-and-sand) prompt is so '80s!

Let's add some colors.

A string between $'…' is like a normal string, but \E is escaped to \033.

 Color escape sequences have to be enclosed in \E[…m, for example $'\E[32mhello will display the string 'hello' in green.

To have a red prompt set $PS1 to
export PS1=$'\E[31m$(logname)@$(hostname -s):$PWD $ \E[0m'
Note the \E[0m before the closing ', this resets the text color.

You can add more attributes to \E[…m, the complete form is:
\E[bgcolor;textattribute;fgcolor m

The possible values for bgcolor are integers between 40 and 47, for fgcolor are integers between 30 and 37 and for tetxattribute are 0, 1, 4, 5, 7 and 8.

Refer to the following table for the colors













Color table Explanation:

The values for textattribute have the following meaning:

0: normal text
1: bold text or light color, depending on the term preferences
4: underscore text
5: blinking text(yep!)
7: inverse
8: concealed text

Putting all together

My prompt is the following, you can modify it to fulfill your needs.
export PS1=$'\E[35;7m$(logname)@$(hostname -s)\E[0m:\E[32;1m$PWD \E[0m $ 

Merging lines in two files

merge 1st line file1  2nd line as 1st line of 2nd file 

#! /bin/sh
echo "enter the first file name"
read first_file
echo "enter the second file name"
read second_file
echo "$first_file  $second_file"
echo "enter output file name"
read out_file

merge(){
paste -d"\n" $first_file $second_file|sed -e '/^$/d'>$out_file
}

merge;


I/P:

cat file1
aaa
bbb
ccc
bash-4.2$ cat file2
a
b
c
d
e

execution:

bash-4.2$ merge.sh
enter the first file name
file1
enter the second file name
file2
file1  file2
enter output file name
out

O/P:

bash-4.2$ cat out
aaa
a
bbb
b
ccc
c
d
e


Thursday, 25 April 2013

How to format date for display or to use in a shell script

Q. How do I format date to display on screen on for my scripts as per my requirements?

A. You need to use standard date command to format date or time for output or to use in a shell script.

Syntax to specify format
date +FORMAT
Task: Display date in mm-dd-yy format
Type the command as follows:
$ date +"%m-%d-%y"
Output:
02-27-07     
Turn on 4 digit year display:
$ date +"%m-%d-%Y"
Just display date as mm/dd/yy format:
$ date +"%D"
Task: Display time only
Type the command as follows:
$ date +"%T"
Output:
19:55:04
Display locale’s 12-hour clock time
$ date +"%r"
Output:
07:56:05 PM
Display time in HH:MM format:
$ date +"%H-%M"
How do I save time/date format to a variable?
Simply type command as follows at a shell prompt:
$ NOW=$(date +"%m-%d-%Y")
To display a variable use echo / printf command:
$ echo $NOW
Sample shell script:
#!/bin/bash
NOW=$(date +"%m-%d-%Y")
FILE="backup.$NOW.tar.gz"
# rest of script Complete list of FORMAT control characters supported by date command
FORMAT controls the output.It can be the combination of any one of the following:
%%
a literal %
%a
locale's abbreviated weekday name (e.g., Sun)
%A
locale's full weekday name (e.g., Sunday)
%b
locale's abbreviated month name (e.g., Jan)
%B
locale's full month name (e.g., January)
%c
locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C
century; like %Y, except omit last two digits (e.g., 21)
%d
day of month (e.g, 01)
%D
date; same as %m/%d/%y
%e
day of month, space padded; same as %_d
%F
full date; same as %Y-%m-%d
%g
last two digits of year of ISO week number (see %G)
%G
year of ISO week number (see %V); normally useful only with %V
%h
same as %b
%H
hour (00..23)
%I
hour (01..12)
%j
day of year (001..366)
%k
hour ( 0..23)
%l
hour ( 1..12)
%m
month (01..12)
%M
minute (00..59)
%n
a newline
%N
nanoseconds (000000000..999999999)
%p
locale's equivalent of either AM or PM; blank if not known
%P
like %p, but lower case
%r
locale's 12-hour clock time (e.g., 11:11:04 PM)
%R
24-hour hour and minute; same as %H:%M
%s
seconds since 1970-01-01 00:00:00 UTC
%S
second (00..60)
%t
a tab
%T
time; same as %H:%M:%S
%u
day of week (1..7); 1 is Monday
%U
week number of year, with Sunday as first day of week (00..53)
%V
ISO week number, with Monday as first day of week (01..53)
%w
day of week (0..6); 0 is Sunday
%W
week number of year, with Monday as first day of week (00..53)
%x
locale's date representation (e.g., 12/31/99)
%X
locale's time representation (e.g., 23:13:48)
%y
last two digits of year (00..99)
%Y
year
%z
+hhmm numeric timezone (e.g., -0400)
%:z
+hh:mm numeric timezone (e.g., -04:00)
%::z
+hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z
numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z

CUT command examples in unix / linux

Cut command in unix (or linux) is used to select sections of text from each line of files. You can use the cut command to select fields or columns from a line by specifying a delimiter or you can select a portion of text by specifying the range or characters. Basically the cut command slices a line and extracts the text.

Unix Cut Command Examples

We will see the usage of cut command by considering the below text file as an example
#cat file.txt
unix or linux os
is unix good os
is linux good os

1. Write a unix/linux cut command to print characters by position?

The cut command can be used to print characters in a line by specifying the position of the characters. To print the characters in a line, use the -c option in cut command
#cut -c4 file.txt
x
u
l
The above cut command prints the fourth character in each line of the file. You can print more than one character at a time by specifying the character positions in a comma separated list as shown in the below example
#cut -c4,6 file.txt
xo
ui
ln
This command prints the fourth and sixth character in each line.

2.Write a unix/linux cut command to print characters by range?

You can print a range of characters in a line by specifying the start and end position of the characters.
# cut -c4-7 file.txt
x or
unix
linu
The above cut command prints the characters from fourth position to the seventh position in each line. To print the first six characters in a line, omit the start position and specify only the end position.
# cut -c-6 file.txt
unix o
is uni
is lin
To print the characters from tenth position to the end, specify only the start position and omit the end position.
# cut -c10- file.txt
inux os
ood os
good os
If you omit the start and end positions, then the cut command prints the entire line.
# cut -c- file.txt

3.Write a unix/linux cut command to print the fields using the delimiter?

You can use the cut command just as awk command to extract the fields in a file using a delimiter. The -d option in cut command can be used to specify the delimiter and -f option is used to specify the field position.
# cut -d' ' -f2 file.txt
or
unix
linux
This command prints the second field in each line by treating the space as delimiter. You can print more than one field by specifying the position of the fields in a comma delimited list.
# cut -d' ' -f2,3 file.txt
or linux
unix good
linux good
The above command prints the second and third field in each line.

Note: If the delimiter you specified is not exists in the line, then the cut command prints the entire line. To suppress these lines use the -s option in cut command.

4. Write a unix/linux cut command to display range of fields?

You can print a range of fields by specifying the start and end position.
# cut -d' ' -f1-3 file.txt
The above command prints the first, second and third fields. To print the first three fields, you can ignore the start position and specify only the end position.
# cut -d' ' -f-3 file.txt
To print the fields from second fields to last field, you can omit the last field position.
cut -d' ' -f2- file.txt

5. Write a unix/linux cut command to display the first field from /etc/passwd file?

The /etc/passwd is a delimited file and the delimiter is a colon (:). The cut command to display the first field in /etc/passwd file is
# cut -d':' -f1 /etc/passwd

6. The input file contains the below text

# cat filenames.txt
logfile.dat
sum.pl
add_int.sh
Using the cut command extract the portion after the dot.
First reverse the text in each line and then apply the command on it.
# rev filenames.txt | cut -d'.' -f1

Wednesday, 24 April 2013

Script to Compare two files in and out

In my day today activities  you may come across a situation where in you need to compare two files  either of below situations.

Q:
1)  List the entries which are in File A and not in File B
2) List the entries which are in File  B and not in File A
3) List the entries which are common in File A and File B
4) List the entries merging both File A and File B contents  with out duplicates.

Solution Shell Script:

#!/bin/sh
echo "Enter an Option from the below"
echo "enter Option 1 : to choose records which are in first file not there in second file"
echo "enter Option 2 : to choose records which are not there in first file and there in second file"
echo "enter Option 3 : to choose records which are common in both the file"
echo "enter Option 4 : to merge two files with unique records"
read option
echo "you have choosen option $option"
echo "enter first file name"
read first_file
echo "$first_file"
echo "enter second file name"
read second_file
echo "$second_file"
echo "enter output file name"
read output_filename

#### to choose records which are in first file not there in second file

fun_1(){
sort $first_file>${first_file}_sorted
sort $second_file>${second_file}_sorted
comm -23 ${first_file}_sorted ${second_file}_sorted>${output_filename}
}

#### to choose records which are not there in first file and there in second file

fun_2(){
sort $first_file>${first_file}_sorted
sort $second_file>${second_file}_sorted
comm -13 ${first_file}_sorted ${second_file}_sorted>${output_filename}
}

#### to choose records which are common in both the file

fun_3(){
sort $first_file>${first_file}_sorted
sort $second_file>${second_file}_sorted
comm -12 ${first_file}_sorted ${second_file}_sorted>${output_filename}
}

#### to merge two files with unique records

fun_4(){
sort $first_file $second_file|uniq>${output_filename}
}

#### check whether both the input files are exists and regular files then only we can proceed

if [ -f $first_file -a -f $second_file ];then
echo "both files are exists and regular files"

#### according to the chosen option function will be called

if [ $option -eq 1 -o $option -eq 2 -o $option -eq 3 -o $option -eq 4 ];then
fun_${option}
rm -f ${first_file}_sorted ${second_file}_sorted
else
echo "choose proper option"
fi

else
echo "mentioned input files are not correct "
fi

Execution &O/P

bash-4.2$ cat a.txt
ORANGE2009040910002|10|1
GREEN200903304001052|1101|2
GREEN200903304001053|1101|2
GREEN200903304001054|1101|2
GREEN200903304001055|1101|2
ORANGE2009040910001|10|3
ORANGE2009040910004|10|1
ORANGE2009040910006|10|1
ORANGE2009040910003|10|1
ORANGE2009040910008|10|1
ORANGE2009040910005|10|1
ORANGE2009040910010|10|1
ORANGE2009040910007|10|1
ORANGE2009040910012|10|1
GREEN200903304001052|1101|2
GREEN200903304001053|1101|2
GREEN200903304001054|1101|2
GREEN200903304001055|1101|2

bash-4.2$ cat b.txt
ORANGE2009040910002|10|1
ORANGE2009040910001|10|3
ORANGE2009040910004|10|1
ORANGE2009040910006|10|1
ORANGE2009040910003|10|1
ORANGE2009040910008|10|1
ORANGE2009040910005|10|1
ORANGE2009040910010|10|1
ORANGE2009040910007|10|1
ORANGE2009040910012|10|1
GREEN200903304001008|1101|2
GREEN200903304001007|1101|2
GREEN200903304001010|1101|2
GREEN200903304001009|1101|2
GREEN200903304001011|1101|2
GREEN200903304001012|1101|2
GREEN200903304001014|1101|2

Execution:


bash-4.2$ comm.sh
Enter an Option from the below
enter Option 1 : to choose records which are in first file not there in second file
enter Option 2 : to choose records which are not there in first file and there in second file
enter Option 3 : to choose records which are common in both the file
enter Option 4 : to merge two files with unique records
3
you have choose option 3
enter first file name
a.txt
a.txt
enter second file name
b.txt
b.txt
enter output file name
c.txt
both files are exists and regular files

bash-4.2$ cat c.txt
ORANGE2009040910001|10|3
ORANGE2009040910002|10|1
ORANGE2009040910003|10|1
ORANGE2009040910004|10|1
ORANGE2009040910005|10|1
ORANGE2009040910006|10|1
ORANGE2009040910007|10|1
ORANGE2009040910008|10|1
ORANGE2009040910010|10|1
ORANGE2009040910012|10|1