I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…
Unix shells provide a very powerful and extensible framework for scripting.
A number of advanced mechanisms allow for more complex scripting using the inbuilt commands.
Unix Video #24:
Advanced Shell Scripting in Unix
Unix includes commands for:
Testing various conditions associated with specified files.
Testing various conditions associated with specified strings.
Performing file read/write operations.
Arrays
Arrays are used to store a series of values in an indexed list. Items in an array are stored and retrieved using an index. Note that Arrays are not supported by the original Bourne Shell, but are supported by bash and other newer shells.
File Test Operators
Shell scripts often need to check various properties of files as a part of the control flow. Unix provides a number of options for this purpose.
File existence checks:
-f file True if the file exists and is an ordinary file.
-d file True if the file exists and is a directory.
-s file True if the file exists and is not empty.
-c file True if the file exists and is a character device file.
-b file True if the file exists and is a block devise file.
File access checks:
-r file True if the file exists and has read permission to it.
-w file True if the file exists and has a write permission to it.
-x file True if the file exists and has a execute permission to it.
String Test Operators
Unix commands often need to test the various properties of string variables as a part of the control flow.
Unix provides a number of options for this:
[ string1=string2 ] True if string1 and string2 are same.
[ string1!=string2 ] True if string1 is not equal to string2.
[ -n string ] True if the string is not zero.
[ -z string ] True if the string is zero.
[ string ] True if the string is not empty.
Special Variables
While running scripts, Unix provides a number of predefined variables that can be used to get information from the environment.
Unix also provides a number of special symbols with additional information:
$# Total number of positional parameters.
$@ Represents all the parameters i.e. $1 to the end.
$? Pass or fail status of the last command executed.
$$ Process id of the currently running shell.
$! Process id of the last run background process.
Hope you enjoyed all the tutorials on this entire Unix tutorial training series. I’m sure that you would have gained immense knowledge on the various concepts in Unix.
Overview of Unix File System: In this tutorial, we will dive deep into Unix File System. The file system is central to how Unix organizes information, and all the information that needs to be stored and retrieved uses the file system. In this tutorial, we cover Unix file structure, types…
Overview of Unix File Access Permissions: In this tutorial, we will get to know how to change access permissions based on individual ownership and group ownership. The commands covered here include chmod, chown, and chgrp. Unix Video #7: File Manipulation #1) chmod: Change file access permissions. Description: This command is used…
Overview of Unix Filename Wildcards or Special Characters or Metacharacters: In this tutorial, we will learn to use wildcards to specify and select multiple files for commands that manipulate files. Filename wildcards, which is also known as metacharacters, is a very helpful feature. Unix Video #9: Unix Filename Wildcards - Metacharacters…
In this tutorial, you will learn different basic and advanced Unix Commands. Unix commands are inbuilt programs that can be invoked in multiple ways. Here, we will work with these commands interactively from a Unix terminal. A Unix terminal is a graphical program that provides a command-line interface using a…
Introduction to Unix Shell Scripting: In Unix, the Command Shell is the native command interpreter. It provides a command line interface for the users to interact with the operating system. Unix commands may also be executed non-interactively in the form of a Shell Script. The script is a series of…
Introduction to Find Command in Unix: Search files and directories with Unix Find File Command The Unix find command is a powerful utility to search for files or directories. The search can be based on different criteria, and the matching files can be run through defined actions. This command recursively…
A beginner's guide to Unix - A complete Unix tutorial series of 20+ in-depth text and video tutorials with hands-on examples. By the end of this series, you will be able to understand the basic and advanced concepts of Unix Architecture, Unix Commands, and applications including File Management, Text Processing,…
Overview of Unix File Comparison Commands : In this tutorial, we will cover the different ways involved for comparing two files. The file comparison command helps us to compare the files and find the similarities and differences between these files. The different file comparison commands used in Unix are cmp,…