Friday, 27 December 2013

SHELL Scripting

shell script is a script written for the shell, or command line interpreter, of an operating system. The shell is often considered a simple domain-specific programming language.There are hundreds of UNIX commands that you can execute at the shell prompt. 

Shells have their own built-in syntax that helps you to work more effectively with existing commands by allowing you to perform functions like plugging commands into each other and controlling the flow of execution.

Conditional operators

|| . use the double pipe operator in the form, command1 || command2
In the above syntax, the second command executes only if the first command fails.
&& . use the double ampersand operator in the form, command1 && command2
In the above syntax, the second command executes only if the first command executes successfully.

Command grouping operators

{ } , enclose multiple statements in braces ({}) to create a code block. The shell returns one exit status value for the entire group, rather than for each command in the block.
( ) , enclose multiple statements in round brackets to create a code block. This code block functions in the same way as a code block enclosed in braces, but runs in a subshell.

I/O redirection operators

> , use this operator to redirect command output to a file. If the specified file doesn’t exist, the shell creates the file. If the file does exist, the shell overwrites it with the command output unless the noclobber environment variable is set.
>| , use this operator to redirect command output to a file. If the specified file doesn’t exist, the shell creates the file. If the file does exist, the shell overwrites it with the command output even if the noclobber environment variable is set.
>> , use this operator to redirect command output to a file. If the file doesn’t exist, the shell creates the file. If it does exist, the shell appends the new data to the end of it.
< , use this operator to redirect command input from a file.

File descriptor redirection operators

<&n , use this operator to redirect standard input from file descriptor n.
>&n , use this operator to redirect standard input to file descriptor n.
n< filename , use this operator with a filename to redirect descriptor n from the specified file.
n> filename , use this operator with a filename to redirect descriptor n to the specified file. Unlike ordinary redirection, this will not overwrite an existing file.
n>| filename , use this operator with a filename to redirect descriptor n to the specified file, overriding the noclobber environment variable if it is set.
n>> filename , use this operator with a filename to redirect a descriptor to the specified file. This will redirect to a file but, unlike ordinary redirection, this will append to an existing file.

Filename substitution

* , use the * wildcard to match a string of any length.
? , use the ? wildcard to match a single character.
[abc] , [a-c] , [a-c1-3]
use square brackets to match only characters that appear inside the specified set. For increased convenience, you can specify multiple ranges.
!pattern , use the ! operator with a pattern to perform a reverse match. The shell returns only filenames that don’t match the pattern.

Command substitution

$(command) , use this form of command substitution to resolve a command and pass its output to another command as an argument.
$(< filename) , use this form of command substitution to pass the entire contents of a file to a command as an argument.

Tilde substitution

~ , use the ~ operator to instruct the shell to return the value of the $HOME variable.
~username , use the ~ operator with a username to instruct the shell to return the full path of a specific user’s home directory.
~+ , use the ~+ operator to instruct the shell to return the full path of the current working directory.
~- , use the ~- operator to instruct the shell to return the full path of the previous working directory you used.

New Line is New Command

Every new line should be considered a new command, or a component of a larger system. If/then/else statements, for example, will take over multiple lines, but each component of that system is in a new line. Don’t let a command into the next line, as this can truncate the previous command and give you an error on the next line. If your text editor is doing that, you should turn off text-wrapping to be on the safe side. 

Comment(#=comment)

If you start a line with #, the line is ignored. This turns it into a comment line, where you can remind yourself of what the output of the previous command was, or what the next command will do. Again, turn off text wrapping, or break you comment into multiple lines that all begin with a hash. Using lots of comments is a good practice to keep, as it lets you and other people tweak your scripts more easily.

Case Statement
If there is a first parameter, we need to figure out which it is. For this, we use a case statement:
case "$1" in
   init)
    
   ;;
   create)
    
   ;;
   run)
    
   ;;
   *)
      help
   ;;
esac


We pass the first parameter to the case statement; then, it should match one of four things: "init", "create", "run", or our wildcard, default case. Notice that we don't have an explicit "help" case: that's just our default case. This works, because anything other than "init", "create", and "run" aren't commands we recognize, so it should get the help text.


Miscellaneous syntax


; , If you enter several commands on the same line, you need to separate the commands with semicolons. The shell will execute each command successively once you press Enter.

\ , use a backslash to allow you to press Enter and continue typing commands on a new line. The shell will only begin executing your commands when you press Enter on a line that doesn’t end in a backslash. Using a backlash in this way is known as backslash escaping.

& , add a single ampersand at the end of a command to run that command as a background process. This is useful for tasks that are likely to take a long time to complete.

Shell programs can execute a wide range of UNIX commands, but they also have built-in functions to help you use shells more effectively. Most shells support standard operators for conditional execution, input/output (I/O) redirection, file descriptor redirection, and command grouping. They also allow you to perform filename, tilde, and command substitution. Different shells have shell specific features. So, shell should be chosen based on useful shell features. If a shell script has to be created, which should be run on different shells, it is suggested to develop on a basic shell like Bourne.

Simple example :

Open a text editor (but not a word processor), such as gedit or vi, and type the following three lines exactly as shown on a new, blank page:

#!/bin/bash
Clear
echo "Hello world."

Alternatively, the above code could be copied from this page and pasted to a blank page opened by the text editor page using the standard keyboard or mouse copy and paste functions.

After saving this plain text file, with a file name such as test (or anything else desired), the script is complete and almost ready to run. Scripts are typically run by typing a dot, a forward slash and the file name (with no spaces in between) and then pressing the ENTER key.

./ test

However, the script might give Permission denied error. This is because the permissions for the file first have to be set to executable. (By default, the permissions for new files are set to read and write only.) The problem can easily be solved by using the chmod command with its 755 option (which will allow the file creator to read, write and execute the file) while in the same directory as that in which the file is located as follows:

chmod 755 test

Now the script is ready to run again:

./ test

No comments:

Post a Comment