Linux Headline

Linux | Linux Server | Linux Tutorial | Ubuntu

Amazon shopping finder
  • About
  • Contact
  • Privacy Policy
  • Sitemap

UNIX Shell Scripting Basics Tutorial

Publish on December 21st, 2012 under Tutorial by Admin | No Comments »

UNIX Shell Scripting Basics Tutorial – When you are using the Linux operating system probably you will start interested in learning more about UNIX shell scripting. In this post, we will share about Basics UNIX Shell Scripting, we would give you the basic tutorial in a simple language that is easy to understand. The purpose of We only sharing basics tutorial is that we want the reader to know basic Shell Scripting first, before getting the material that more difficult and complicated. Happy reading!

unix shell scripting basics

What is a Shell Script?
A text file containing commands which could have been typed directly into the shell.

Any shell can be used for writing a shell script. To allow for this, the first line of every script is:
#!/path/to/shell (e.g. #!/bin/ksh).
Any file can be used as input to a shell by using the syntax:
ksh myscript
If the file is made executable using chmod, it becomes a new command and available for use (subject to the usual $PATH search).
chmod +x myscript

Why use Shell Scripts

  • Combine lengthy and repetitive sequences of commands into a single, simple command.
  • Generalize a sequence of operations on one set of data, into a procedure that can be applied to any similar set of data.
  • Create new commands using combinations of utilities in ways the original authors never thought of.

ksh/bash vs sh

Ksh and bash are both supersets of sh. For maximum portability, even to very old computers, you should stick to the commands found in sh. Where possible, ksh or bash-specific features will be noted in the following pages. In general, the newer shells run a little faster and scripts are often more readable because logic can be expressed more cleanly user the newer syntax. Many commands and conditional tests are now internal.
If you follow textbooks on Bourne shell programming, all of the advice should apply no matter which of the Bourne-derived shells you use. Unfortunately, many vendors have added features over the years and achieving complete portability can be a challenge. Explicitly writing for ksh (or bash) and insisting on that shell being installed, can often be simpler.

The sh and ksh man pages use the term special command for the internal commands – handled by the shell itself.

Basic sh script syntax

The most basic shell script is a list of commands exactly as could be typed interactively, prefaced by the #! magic header. All the parsing rules, filename wildcards, $PATH searches etc., which were summarized above, apply.
In addition:
# as the first non-whitespace character on a line
flags the line as a comment, and the rest of the line is completely ignored. Use comments liberally in your scripts, as in all other forms of programming.
\ as the last character on a line
causes the following line to be logically joined before interpretation. This allows single very long commands to be entered in the script in a more readable fashion. You can continue the line as many times as needed.
; as a separator between words on a line
is interpreted as a newline. It allows you to put multiple commands on a single line. There are few occasions when you must do this, but often it is used to improve the layout of compound commands.

Shell Variables

Scripts are not very useful if all the commands and options and filenames are explicitly coded. By using variables, you can make a script generic and apply it to different situations. Variable names consist of letters, numbers and underscores ([a-zA-Z0-9_], cannot start with a number, and are case sensitive. Several special variables (always uppercase names) are used by the system — resetting these may cause unexpected behaviour. Some special variables may be read-only. Using lowercase names for your own variables is safest.

Setting and exporting variables

srcfile=dataset1
Creates (if it didn’t exist) a variable named “srcfile” and sets it to the value “dataset1″. If the variable already existed, it is overwritten. Variables are treated as text strings, unless the context implies a numeric interpretation. You can make a variable always be treated as a number. Note there must be no spaces around the “=”.

set
Display all the variables currently set in the shell

unset srcfile
Remove the variable “srcfile”

srcfile=
Give the variable a null value, (not the same as removing it).

export srcfile
Added srcfile to the list of variables which will be made available to external program through the environment. If you don’t do this, the variable is local to this shell instance.

export
List all the variables currently being exported – this is the environment which will be passed to external programs.

Using variables

$srcfile
Prefacing the variable name with $ causes the value of the variable to be substituted in place of the name.
${srcfile}
If the variable is not surrounded by whitespace (or other characters that can’t be in a name), the name must be surrounded by “{}” braces so that the shell knows what characters you intend to be part of the name.

There are various ways to conditionally use a variable in a command.

${datafile-default}
Substitute the value of $datafile, if it has been defined, otherwise use the string “default”. This is an easy way to allow for optional variables, and have sensible defaults if they haven’t been set. If datafile was undefined, it remains so.
${datafile=default}
Similar to the above, except if datafile has not been defined, set it to the string “default”.
${datafile+default}
If variable datafile has been defined, use the string “default”, otherwise use null. In this case the actual value $datafile is not used.
${datafile?”error message”}
Substitute the value of $datafile, if it has been defined, otherwise display datafile: error message. This is used for diagnostics when a variable should have been set and there is no sensible default value to use.
Variable assignment command prefix

It is possible to export a variable just for the duration of a single command using the syntax:
var=value command args

Done, that is our basic shell scripting tutorial. Hope useful for you.
Thanks for reading, read also: Bash Shell Scripting Tutorial

dartmouth.edu | linuxheadline

Recent Posts

  • LinuxHeadline.com Site Homepage Changed Yesterday
  • Lets Know Ubuntu Video Converter
  • Top 5 Antivirus for Ubuntu
  • Ubuntu for Netbook Review and Download
  • Linux How To Tutorial

Categories

  • Blog
  • Download
  • Linux
  • Linux Server
  • Tutorial
  • Ubuntu
  • About
  • Contact
  • Privacy Policy
  • Sitemap
Theme designed by Berita Teknologi