Posts

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

Positional parameters in Bash scripting

Here we will see the naming convention of parameters which are passed to a script and how we can use them? how shifting works? #!/bin/bash # Positional parameters #       $0          $1     $2    $3   .......   $n # Command  Arg1 Arg2 Arg3 ....... Argn echo -e "First argument $1, Second argument $2, Third argument $3, Fourth argument $4\n" This will print value of all four arguments which you passed, just for your reference. shift The positional parameters from n+1 ... are renamed  to  $1, so that every time after shift is performed there is a new argument to be processed. This will shift to the left by one value. echo "Shifting...." echo -e "First argument $1, second argument $2, third argument $3, fourth argument $4 \n" This will print new values after shifting the arguments by one value. shift 2 This will shift to the left by two values. echo "Shifting.... by 2 vlaues" echo -e "First argument $1, second

Merging of various commands

Lets create a file with following content: #!/bin/bash echo "Value which is there in variable USER  is $USER" What this above line will do, it will fetch the variable USER which is built-in. echo -e "-e is used to add new line $USER.\n" Adding -e will execute \n and will add a new line. name=$USER We also can assign the built-in variables to another variables as here value of $USER will be stored in name variable. echo -e "Assigning value of USER variable to another variable $name \n" shortname=$(cat /etc/passwd | grep "sunny" | awk -F ":" '{print $5}' | cut -d " " -f1) Here we are merging various commands. What above line will do is firstly it will cat the file /etc/passwd from that it will grep the line which has sunny. sunny :x:1000:1000:Sunny Bhambhani:/home/sunny:/bin/bash Then by the help of awk command it will get the 5th field which contains the full name of the user. Sunn

Sourcing a variable

Lets create a file with following content: #!/bin/bash echo "$var1" Assign a value to a local variable var1 sunny@sunny-workstation bin]$ var1=sunny-workstation sunny@sunny-workstation bin]$ chmod +x 2_simple_script.sh  When you will run this script it will state nothing. That's because script doesn't know anything about var1 variable and that it exists in shell environment. sunny@sunny-workstation bin]$ ./2_simple_script.sh  sunny@sunny-workstation bin]$ But if we source this script via . or source built-in command it will show the output as the script will run in the same environment as our shell sunny@sunny-workstation bin]$ . ./2_simple_script.sh  sunny-workstation sunny@sunny-workstation bin]$ source 2_simple_script.sh  sunny-workstation sunny@sunny-workstation bin]$

Writing your first script

#!/bin/bash First line of the script, lets divide it into pieces: ! is ka she-bang /bin location of bash bash is an interpreter stored in /bin directory. vim is a text editor that supports color coding, that will be really helpful. A good script always have following things: #Author #Date #Contact details #Script usage #Version history Lets try a sample hello world script echo "Hello World"  Print this on stdout echo command is used to print text to your standard output. Add above line in a file called 1_simple_script.sh or any name of your choice. Then we need to change its permission that is we need to get it an execute permission. chmod u+x X.sh If we will not add +x it will show permission denied. There are various ways of executing a script some are given below: [sunny@sunny-workstation bin]$ ~/bin/1_simple_script.sh Hello World [sunny@sunny-workstation bin]$ /home/sunny/bin/1_simple_script.sh Hello World [sunny@sunny-workstation b

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: