This post provides a very brief notes on Unix Shell Scripting. As this topic is very well described in many text books,we are not going much deep into the details of each point. This post is for quick review/revision/reference of common Unix commands or Unix Shell Scripting.
Unix Shell Scripting
The kernel is the heart of the UNIX system. It provides utilities with a means of accessing a machine’s hardware. It also handles the scheduling and execution of commands.
Note: When the computer is booted, the kernel is loaded from disk into memory. The kernel remains in memory until the machine is turned off. Utilities, on the other hand, are stored on disk and loaded into memory only when they are executed.
shell is an interface to the UNIX system. It collects input from user and executes programs based on that input. Once program finishes executing, shell displays that program’s output.
The different Bourne-type shells follow:
- Bourne shell ( sh)
- Korn shell ( ksh)
- Bourne Again shell ( bash)
- POSIX shell ( sh)
The #!/bin/sh must be the first line of a shell script in order for sh to be used to run the script. If this appears on any other line, it is treated as a comment and ignored by all shells.
The PATH specifies the locations in which the shell will look for commands. Usually it is set as follows:PATH=/bin:/usr/bin
Each of the individual entries separated by the colon character, :, are directories.
A compound command consists of a list of simple and complex commands separated by the semicolon character ( ;). An example of a complex command is
Here hostname and date are simple commands and who am i is complex command.
In shell scripts, comments start with the # character. Everything between the # and end of the line are considered part of the comment and are ignored by the shell.
Counting Words in a file
wc command can be used to get a count of the total number of lines, words, and characters contained in a file. The syntax of this command is
- -l Counts the number of lines
- -w Counts the number of words
- -c Counts the number of characters
File Types in Unix
-rwxr-xr-x 1 siva users 2368 Jul 11 15:57 /home/siva/test.txt
Here first character is a hyphen (-). This indicates that the file is a regular file. For special files, the first character will be one of the letters given in below table.
Character File Type
- – Regular file
- l Symbolic link
- c Character special
- b Block special
- p Named pipe
- s Socket
- d Directory file
A symbolic link is a special file that points to another file on the system.A symbolic link is similar to a shortcut or an alias.
Creating Symbolic Links
We can create symbolic links using the ln command with the -s option. The syntax is as follows:
Here, source is either the absolute or relative path to the original version of the file, and destination is the name we want the link to have.
Changing File and Directory Permissions
We can change permissions of a file/directory with the chmod command. Chmod options are below
Here (user) options are:
- u Owner
- g Group
- o Other
- a All
- + Adding permissions to the file
- – Removing permission from the file
- = Explicitly set the file permissions
- r Read
- w Write
- x Execute
- s SUID or SGID
By changing permissions with an octal expression, we can only explicitly set file permissions.
The values of the individual permissions are the following:
- Read permission has a value of 4
- Write permission has a value of 2
- Execute permission has a value of 1
The simplest way to start a process in background is to add an ampersand (&) at the end of the command.
Moving a Foreground Process to the Background
While a foreground process is running, the shell does not process any new commands. Before we can enter any commands, we have to suspend the foreground process to get a command prompt. The suspend key is Ctrl+Z.
When a foreground process is suspended, a command prompt enables us to enter more commands; the original process is still in memory but is not getting any CPU time. To resume the foreground process, we have two choices–background and foreground. The bg command enables us to resume the suspended
process in the background; the fg command returns it to the foreground.
Some useful unix tips:
COPY & PASTE (WITHIN A TERMINAL):
COPY: CONTROL + SHIFT + C
PASTE: CONTROL + SHIFT + V
SETTING UP A VARIABLE GLOBALLY:
/etc/profile (one time per session on logon)
/etc/bash.bashrc (every time you close and open a terminal)
COMMAND TO REFRESH PROFILE CHANGES:
[cloudera@localhost ~]$ sudo gedit /etc/profile
[cloudera@localhost ~]$ sudo gedit /etc/bashrc
[cloudera@localhost ~]$ cd s + TAB key –> will take you into scripts directory
CLEAR THE SCREEN:
CONTROL + L
CUSTOMIZE COMMAND PROMPT:
[cloudera@localhost ~]$ export PS1=’$ ‘
TO SPAN A COMMAND INTO MULTIPLE LINES: (use \)
TO LIST ALL JAVAE PROGRAMS (demons) RUNNING: