Posts

Showing posts from April, 2015

Featured Post

Taking inputs from user in a script

Here we will see how to take input from user in a script with the help of read command. #!/bin/bash Lets try multiplication of two numbers which the user enters. What read does is ? It takes values by user on stdin. It prompt the user to input any value on stdin. -p is used to display a prompt. read -p "First number:" value1 This will give a prompt as " First number " and store the value in variable " value1 " echo Will display a blank line. read -p "Second number:" value2 This will give a prompt as " Second number " and store the value in variable " value2 " echo Will display a blank line. read -sn1 -p "Press any key to see the answer..." Just to make it bit more interactive lets add one more thing. -s means silent mode that is if any value is pressed it will not be displayed on the screen like as in the password. -nX means how many characters. X here is one that is any ...

BASH?

BASH is a Unix shell, which is a command line interface for interacting with the operating system. Also it is the default shell on many linux distributions including Max OS X. It was created in late 1980's by Brian Fox (FSF). It was basically an alertnative to the Bourne SHell and it stands for Bourne Again SHell. It has all the features of shell as well as new features such as regex/airthmetic operations etc. Its location is  /bin/bash How to know on which shell are you working? as in if it is a bash,sh,ksh,csh etc [root@sunny-workstation ~]$ echo $SHELL /bin/bash [root@sunny-workstation ~]$ [root@sunny-workstation ~]$ echo $0 bash [root@sunny-workstation ~]$ Either by checking the value of $SHELL variable or $0 variable. If you remember when we wrote a sample script, the first line was #!/bin/bash this particular line is used to let the operating system know under which interpreter the script will run. In this case its bash, so what will happen is the spe...

What is a Shell Script?

"Shell Script is series of command written in plain text file. It is same as a batch file which is there in MS operating system". Basically it contains a sequence of commands for a UNIX-based operating system. It is called a script because it combines all the commands into a single file and we need not to execute each and every command its just the script which we need to execute. A shell script is usually created for command sequences for which a user has a repeated need and the script can be executed simply by writing the script name with its syntax. Lets write a simple script to display user by which you are logged in? date and time? your present working directory? For user we can use "whoami" command. For date and time we can use "date" command. For present working directory we can use "pwd" command. For getting this printed on standard output we can use "echo" command. Lets say we wrote a file as test.sh as: ...

Introduction to Linux Shell

Shell is a program that takes commands from the keyboard and gives them to the operating system to perform actions accordingly. Mostly Linux operating system has a application called Bash (aka B ourne A gain SH ell, which is an enhanced version of the original Bourne SH ell. There are several additional shell programs available on a Linux system such as ksh, csh, tcsh etc. There are various "terminal emulators" available. Basically these are the programs that helps to interact with the shell. For instance xterm, konsole, gnome-terminal. You can get this launched via your utilities or programs menu. Your terminal will look somewhat like given below: [root@sunny-workstation ~]#  or [sunny@sunny-workstation ~]$  This means USER@HOSTNAME PWD followed by # or $ if you are logged in via root then it will show # otherwise for every normal user it will state $. In above example root/sunny are the users, sunny-workstation is the hostname and ~ means the...