Saturday 24 December 2016

Shell Scripting Tutorial

Shell Scripting Tutorial

Shell Scripting tutorial

Shell Scripting tutorial provides basic and advanced concepts of Shell Scripting. Our Shell Scripting tutorial is designed for beginners and professionals.

Shell Scripting is an open-source operating system.

Our Shell Scripting tutorial includes all topics of Scripting executing scripting, loops, scripting parameters, shift through parameters, sourcing, getopts, case, eval, let etc. There is also given Shell Scripting interview questions to help you better understand the Shell Scripting operating system.

Prerequisite

Before learning Shell Scripting, you must have the basic knowledge of Operating System.

Audience

Our Shell Scripting tutorial is designed to help beginners and professionals.

Problem

We assure that you will not find any problem in this Shell Scripting tutorial. But if there is any mistake, please post the problem in contact form.

What is Shell Scripting In Linux, shells like bash and korn support programming construct which are saved as scripts. These scripts become shell commands and hence many Linux commands are script. A system administrator should have a little knowledge about scripting to understand how their servers and applications are started, upgraded, maintained or removed and to understand how a user environment is built. How to determine Shell You can get the name of your shell prompt, with following command : Syntax: echo $SHELL Linuxss How to determine shell 1 Look at the above snapshot, with the help of above command we got the name of our shell which is 'bash'. The $ sign stands for a shell variable, echo will return the text whatever you typed in. Shell Scripting She-bang The sign #! is called she-bang and is written at top of the script. It passes instruction to program /bin/sh. To run your script in a certain shell (shell should be supported by your system), start your script with #! followed by the shell name. Example: #!/bin/bash echo Hello World #!/bin/ksh echo Hello World Shell Scripting Comments Any line starting with a hash (#) becomes comment. Comment means, that line will not take part in script execution. It will not show up in the output. Linuxss Comment 1 Look at the above snapshot, lines after the # are commented. Linuxss Comment 2 Look at the above snapshot, commented lines are not displayed in the output. Shell Scripting Variables Scripts can contain variables inside the script. Linux Shell Scripting Variables 1 Look at the above snapshot, two variables are assigned to the script $var1 and $var2. As scripts run in their own shell, hence variables do not survive the end of the script. Linux Shell Scripting Variables 2 Look at the above snapshot, var1 and var2 do not run outside the script. next →← prev Shell Scripting Sourcing a file A file is sourced in two ways. One is either writting as source or other is writting as . ./ in the command line. When a file is sourced, the code lines are executed as if they were printed on the command line. The difference between sourcing and executing a script is that, while executing a script it runs in a new shell whereas while sourcing a script, file will be read and executed in the same shell. In sourcing, script content is displayed in the same shell but while executing script run in a different shell. Shell Scripting Sourcing a file 1 Look at the above snapshot, we have sourced the file var with one of the method. Shell Scripting Sourcing a file 2 Look at the above snapshot, we have sourced the file var with another method. Troubleshooting a shell script There is one more way other than script execution to run a script in a different shell. Type bash with the name of the script as parameter. Syntax: bash Example: bash exm Linux Troubleshooting a script 1 Look at the above snapshot, it displays the exm script content with bash command. Linux Troubleshooting a script 2 Look at the above snapshot, this is the exm script we have written. By expanding bash command with -x, shell allows us to see commands that the shell is executing. Linux Troubleshooting a script 3 Look at the above snapshot, with command bash -x, we can see the shell expansion. Shell Scripting Prevent setuid root spoofing Spoofing is a technique through which a user tries to grant unauthorized access on a system by pretending to be the root user. This is called setuid root spoofing. To prevent from spoofing you can add -- after #!/bin/bash. It disables further option processing so that shell will not accept any options. Linux Shell Scripting Prevent setuid root spoofing 1 Look at the above snapshot, any argument after -- are treated as filenames and arguments. An argument of - is equivalent to - Steps to write and execute a script Open the terminal. Go to the directory where you want to create your script. Create a file with .sh extension. Write the script in the file using an editor. Make the script executable with command chmod +x . Run the script using ./. Note: In the last step you have to mention the path of the script if your script is in other directory. Hello World script Here we'll write a simple programme for Hello World. First of all, create a simple script in any editor or with echo. Then we'll make it executable with chmod +x command. To find the script you have to type the script path for the shell. Linuxss Steps to write and execute a script 1 Look at the above snapshot, script echo Hello World is created with echo command as hello_world. Now command chmod +x hello_world is passed to make it executable. We have given the command ./hello_world to mention the hello_world path. And output is displayed. Shell Script Parameters A bash shell script have parameters. These parameters start from $1 to $9. When we pass arguments into the command line interface, a positional parameter is assigned to these arguments through the shell. The first argument is assigned as $1, second argument is assigned as $2 and so on... If there are more than 9 arguments, then tenth or onwards arguments can't be assigned as $10 or $11. You have to either process or save the $1 parameter, then with the help of shift command drop parameter 1 and move all other arguments down by one. It will make $10 as $9, $9 as $8 and so on. Shell Parameters Parameters Function $1-$9 Represent positional parameters for arguments one to nine ${10}-${n} Represent positional parameters for arguments after nine $0 Represent name of the script $∗ Represent all the arguments as a single string $@ Same as $∗, but differ when enclosed in (") $# Represent total number of arguments $$ PID of the script $? Represent last return code Example: Linux Shell Scripting parameters 1 Look at the above snapshot, this is the script we have written to show the different parameters. Linux Script parameters 2 Look at the above snapshot, we have passed arguments 1, 5, 90. All the parameters show their value when script is run. Shell Scripting Shift Through Parameters Shift command is a built-in command. Command takes number as argument. Arguments shift down by this number. For example, if number is 5, then $5 become $1, $6 become $2 and so on. Example: The shift command is mostly used when arguments are unknown. Arguments are processed in a while loop with a condition of (( $# )). this condition holds true as long as arguments are not zero. Number of arguments are reduced each time as the shift command executes. Shell Scripting Shift through parameters 1 Look at the above snapshot, this is our script. Shell Scripting Shift through parameters 2 Look at the above snapshot, this is the output of the above script. read command The read command allows a user to provide the runtime input. Shell Scripting Shift through parameters 3 Look at the above snapshot, this is our script using read command. Shell Scripting Shift through parameters 4 Look at the above snapshot, a user can enter the name in the shell. Shell Scripting Sourcing a config file Many programs use external configuration files. Use of external configuration files prevents a user from making changes to a script. Config file is added with the help of source command. If a script is shared in many users and every user need a different configuration file, then instead of changing the script each time simply include the config files. Example: We have two files, one is parent file (main.sh) and other is configuration file (config.sh). We have to source this configuration file into our parent file. Script for main.sh Shell Scripting Sourcing a config file 1 Script for config.sh Linux Shell Scripting a config file 2 Look at the above snapshot, we've included config.sh file with source command. Note: We can also use ( . config.sh ) command instead of ( source config.sh ) . Now on running main.sh file, config.sh file is included. Shell Scripting Sourcing a config file 3 Look at the above snapshot, on running main.sh file, content of config.sh file is imported via source command.

No comments:

Post a Comment