Skip to content

lynxestudio/UsefulShellScripts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Introduction to shell Scripting.

Because Linux has its conceptual roots in the older Unix operating system, many applications are designed to be executed from a command line. Linux has two user interfaces: the Graphical User Interface (GUI) and The command Line, the Shell.

What is a Shell?

In UNIX, the parent operating system of Linux, a command interpreter is a Shell. A shell is an environment where we can run commands, programs, and others shell scripts. The UNIX operating system popularised many of the ideas about shell scripting.

Linux offers many different shells; however, Bash has become the default standard on Linux. Bash understands a whole of standard Linux commands or can run any command stored in an executable file. In addition, it can also execute shell scripts (text files that contain Linux commands).

You build a small and simple utility and we use it as one link in a string of others to form a command. The shell acts as the interface between you and the Linux System, allowing you to enter commands for the operating system to execute.

Fig 1. Linux architecture

The purpose of the shell is to start processes running your commands. The shell stays between the user (you) and the system. The command is not directly executed; rather, the shell reads the input, interprets the command and then creates the process that executes the command.

Why Use the Command Line?

Linux provides thousands of shell utilities that range from simple programs for creating, examining, and modifying files and hardware configurations to complex utilities that enable low-level capabilities such as file systems, security and networking.

The basic concept of a shell script is a list of commands, which are listed in the order of execution. Shell scripts are interpreted. They are ASCII text that is read and executed by the shell interpreter. When we execute a shell script, the shell interpreter goes through the ASCII text line by line, and executes each statement, as each line is reached from the top to the bottom.

All shell commands have this general format:

command option1 option2... optionN

Processes

Linux is a multiprocessing operating system. This means that several processes can be executed simultaneously. By definition, a process as an instance of an executing program. If the same program is being executed twice, it requires two processes. Everything done on a Linux system is done by a process. It is important to emphasize that a process is not a program. A process is an environment within which a program executes. By default, every process, when created, is connected to three default streams: standard input, standard output, and standard error. We can see it in the following image.

Fig 2. Every process has three default streams.

All three standard streams are connected to the process’s controlling terminal, by default the input stream is the keyboard and the screen is the output and error streams respectively.

Pipelines

Because process is independent of the program it executes and the data it processes. The process does not care how the data it is using is generated or consumed. This mechanism allows the output of one process to be connected to the input of another.

Fig 3. Pipeline using one process.

Anything done by a pipeline can also be done by storing the output of process A into a file or using a file as input for another process.

Fig 4. Pipeline using a file as an input.

You can get the following scripts in the src folder:

  1. countFolders.sh: Counts the total number of folder in a given path.
  2. countFiles.sh: Counts the total of files in a given path (same logic that countFolder).
  3. convertLower2Upper.sh: convert the files names of a given path to Uppercase.
  4. covertUpper2lower.sh: convert the files names of a given path to Lowercase.
  5. burnfolder2iso.sh: makes the iso image of a given folder.
  6. PgAgent.sh: starts or stops a PostgreSQL database.
  7. killmyprocess.sh: kills a process running by the user.
  8. showFrequencyWords.sh: Displays the word frequency in a text file.
  9. showFileSize.sh: Displays the size of the files of a given path.
  10. backupDir.sh: Backups and compress all directories of a given path, and adds the datetime to the name of each backup file.
  11. imageConverter.sh: Uses some options in ImageMagick convert command to convert images from one format to another.

References:

  1. Swart,R.[1990],UNIX Applications Programming: Mastering the Shell,USA,SAMS
  2. Waingrow, K.[1999],UNIX Hints & Hacks,USA,QUE
  3. Rosen K,Rosinki R, farber J,Host D[1997], UNIX SYSTEM V an introduction 2 edition,Spain,McGrawHill

Releases

No releases published

Packages

No packages published

Languages