Professional Documents
Culture Documents
Example,
Since we learned basic commands to use Linux OS, we will now write our first
Shell script called hello.sh. You can use any editor of your choice such as vi, gedit, no,
and other similar editors. I prefer to use the vi editor.
1. Create a new hello.sh file as follows:
#!/bin/bash
# This is comment line
echo "Hello World"
ls
date
2. Save the newly created file.
The #!/bin/bash line is called the shebang line. The combination of the
characters #and ! is called the magic number. The shell uses this to call the intended
shell such as
/bin/bash in this case. This should always be the first line in a Shell script.
The next few lines in the Shell script are self explanatory.
• Any line starting with #, will be treated as a comment line. An exception to
this would be the first line with #!/bin/bash
• The echo command will print Hello World on the screen
• The ls command will display directory content on the console
• The date command will show the current date and time
We can execute the newly created file by the following commands:
• Technique one:
$ bash hello.sh
• Technique two:
$ chmod +x hello.sh
VARIOUS DIRECTORIES
We will explore the directory structure in Linux so that it will be useful later
on:
• /bin/: This contains commands used by a regular user.
• /boot/: The files required for the operating system startup are stored here.
• /cdrom/: When CD-ROM is mounted, the CD-ROM files are accessible
here.
• /dev/: The device driver files are stored in this folder. These device driver
files will point to hardware-related programs running in kernel.
• /etc/: This folder contains configuration files and startup scripts.
• /home/: This folder contains a home folder of all users except the
administrator.
• /lib/: The library files are stored in this folder.
• /media/: External media such as a USB pen drive is mounted in this folder.
• /opt/: The optional packages are installed in this folder.
• /proc/: This contains files which give information about kernel and every
process running in OS.
• /root/: This is the administrators home folder.
• /sbin/: This contains commands used by the administrator or root user.
• /usr/: This contains secondary programs, libraries, and documentation
about user-related programs.
• /var/: This contains variable data such as http, tftp, and similar other.
• /sys/: This dynamically creates the sys file
DRILLING DEEP INTO PROCESS MANAGEMENT, JOB CONTROL AND
AUTOMATION
INTRODUCING PROCESS BASICS
A running instance of a program is called as process. A program stored in the
hard disk or pen drive is not a process. When that stored program starts executing,
then we say that process has been created and is running.
Let's very briefly understand the Linux operating system boot-up sequence:
In PCs, initially the BIOS chip initializes system hardware, such as PCI bus,
display device drivers, and so on.
Then the BIOS executes the boot loader program.
The boot loader program then copies kernel in memory, and after basic
checks, it calls a kernel function called start_kenel().
The kernel then initiates the OS and creates the first process called init.
You can check the presence of this process with the following command:$ ps –
ef
Every process in the OS has one numerical identification associated with it. It
is called a process ID. The process ID of the init process is 1. This process
is the parent process of all user space processes.
In the OS, every new process is created by a system call called fork().
Therefore, every process has a process ID as well as the parent process ID.
We can see the complete process tree using the following command:$ pstree
You can see the very first process as init as well as all other processes with a
complete parent and child relation between them. If we use the $ps –ef command,
then e an see that the init process is owned by root and its parent process ID is 0. This
means that there is no parent for init:
Therefore, except the init process, all other processes are created by some other
process. The init process is created by the kernel itself
The following are the different types of processes:
• Orphan process: If by some chance the parent process is terminated, then the
child process becomes an orphan process. The process which created the parent
process, such as the grandparent process, becomes the parent of orphan child process.
In the last resort, the init process becomes the parent of the orphan process.
• Zombie process: Every process has one data structure called the process
control table. This is maintained in the operating system. This table contains the
nformation bout all the child processes created by the parent process. If by chance the
parent process is sleeping or is suspended due to some reason and the child process is
erminated, then the parent process cannot receive the information about the child
process termination. In such cases, the child process that has been terminated is alled
the zombie process. When the parent process awakes, it will receive a signal regarding
the child process termination and the process control block data structure ill be
updated. The child process termination is then completed.
• Daemon process: Until now, we had started every new process in a Bash
terminal. herefore, if we print any text with the $ echo "Hello" command, it will be
printed n the terminal itself. There re certain processes that are not associated with
any terminal. Such processes are called a daemon process. These processes are
running in ackground. An advantage of the daemon process is they are immune to
the changes happening to Bash shell, which has created it. When we want to run
certain background rocesses, such as DHCP server and so on, then the daemon
processes are very useful.
MONITORING PROCESSES USING PS
We have used the command ps in the introduction. Let's learn more about it:
To list the process associated with our current Bash shell terminal,
enter the following command:
$ ps
To list processes along with the parent process ID associated with the current
terminal, enter the following command:
$ ps –f
We can see the process ID in the PID column and the parent process ID in the
PPID column in the preceding output.
• To list processes with the parent process ID along with the process state,
enter the following command
$ ps –lf
TEXT PROCESSING
Text processing commands is comprehensive and time saving. Knowing even
their existence is enough to avoid the need of writing yet another script.
sort
uniq
comm
cmp
diff
tr
sed
awk
perl
cut
paste
column
pr
OPTIONS
-R random sort
-r reverse the sort order
-o redirect sorted result to specified filename, very useful to sort a file inplace
-n sort numerically
-V version sort, aware of numbers within text
-h sort human readable numbers like 4K, 3M, etc
-k sort via key
-u sort uniquely
-b ignore leading white-spaces of a line while sorting
-t use SEP instead of non-blank to blank transition
Examples
sort dir_list.txt display sorted file on standard output sort -bn numbers.txt -o
numbers.txt sort numbers.txt numerically (ignoring leading white-spaces) and
overwrite the file with sorted output
sort -R crypto_keys.txt -o crypto_keys_random.txt sort randomly and write to new
file shuf crypto_keys.txt -o crypto_keys_random.txt can also be used du -sh * | sort
-h sort file/directory sizes in current directory in human readable format.
$ echo -e 'Blue\nRed\nGreen\nBlue\nRed\nBlack\nRed' > colors.txt
$ uniq colors.txt
Blue
Red
Green
Blue
Red
Black
Red
$ uniq -d sorted_colors.txt
Blue
Red
$ uniq -u sorted_colors.txt