background preloader

ShellScripting

Facebook Twitter

Bash Shell Scripting - 10 Seconds Guide. This Bash shell scripting guide is not a detailed study but a quick reference to the BASH syntax.

Bash Shell Scripting - 10 Seconds Guide

So lets begin... Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable in MSDOS. HOME - Home directory of the user. MAIL - Contains the path to the location where mail addressed to the user is stored. IFS - Contains a string of characters which are used as word seperators in the command line. . $ echo $IFS | od -bc PS1 and PS2 - Primary and secondary prompts in bash. . $ ls | ... and press enter. USER - User login name. TERM - indicates the terminal type being used. SHELL - Determines the type of shell that the user sees on logging in. To see what are the values held by the above environment variables, just do an echo of the name of the variable preceded with a $. For example, if I do the following: $ echo $USER ravi ... Some bash shell scripting rules The first line in your script must be #! Conditional statements 'if' Statement Example : Example:

Tutorial: Conditions in bash scripting (if statements) « Linux Tutorial Blog. If you use bash for scripting you will undoubtedly have to use conditions a lot, for example for an if … then construct or a while loop.

Tutorial: Conditions in bash scripting (if statements) « Linux Tutorial Blog

The syntax of these conditions can seem a bit daunting to learn and use. This tutorial aims to help the reader understanding conditions in bash, and provides a comprehensive list of the possibilities. A small amount of general shell knowledge is assumed. Difficulty: Basic – Medium Introduction Bash features a lot of built-in checks and comparisons, coming in quite handy in many situations. If [ $foo -ge 3 ]; then The condition in this example is essentially a command.

If test $foo -ge 3; then If $foo is Greater then or Equal to 3, the block after ‘then’ will be executed. If [ -f regularfile ]; then The above condition is true if the file ‘regularfile’ exists and is a regular file. If [ -r readablefile]; then The above condition is true if the file ‘readablefile’ exists and is readable.

The syntax of an if statement (a short explanation) 1. 2. 3. Conclusion. Advanced Bash-Scripting Guide. Good Shell Coding Practices. Why is it necessary to write something about command line arguments?

Good Shell Coding Practices

The concept is very easy and clear: if you enter the following command $ ls -l *.txt the command "ls" is executed with the command line flag "-l" and all files in the current directory ending with ".txt" as arguments. Still many shell scripts do not accept command line arguments the way we are used to (and came to like) from other standard commands. Some shell programmers do not even bother implementing command line argument parsing, often aggravating the script's users with other strange calling conventions.

For examples on how to name command line flags to be consistent with existing UNIX commands see the table Frequent option names. Here are some examples of bad coding practices. Setting environment variables for script input that could be specified on the command line. Our goal are shellscripts, that use "standard" command line flags and options. Consider the following command line: command line flags (i.e.