Unix is the most popular and powerful multi-tasking and multi-user operating system developed from Bell laboratories in the 1970s. It is made up of series of shells. The outermost shell is for the users. Users command are entered on a command line interface. The second layer is called a shell which interprets commands and passes the instructions to an operating system. The third layer is the heart of an operating system known as kernel. It manages the operations between the shell and hardware and the fourth layer is a hardware. The three major types of shells are: Bourne shell, C shell and Korn shell. MacOS is built on UNIX operating system. And, of course, Linux is also an implementation of UNIX on the PC platform.
The benefits of using Unix are:
It is a multi-user system where different users can share same resources.
It is a multi-tasking system where users can execute many processes at the same time.
As it is written in high level language (C language), it is easy to port with other machines.
Due to it hierarchical file structure, it provides easier access and maintenance of data.
It comes with in-built support for networking so that different user can easily exchange the information.
Unix’s functionality can be extended with user programs built on a standard programming interface.
The benefits of learning Unix are as follows:
Its robust scope of shell scripting. It helps to learn command line better, saves time and file management tasks.
It provides unlimited career progression opportunities for developers and system administrators.
cd Documents and cd Applications\ Support/ and cd..
The cd command is used to change the current directory to another directory. For example: the cd Document command will direct into Document directory and cd Applications/Support/ command will change the current directory into support subdirectory. And cd .. command will take the current directory to previous directory.
more ~/.bash_profile and cat file2.txt
The command 'more ~/.bash_profile' will display content of the file .bash_profile that exist in current directory. 'cat' command enables to create multiple files, view the content of the file and concatenate files. This command 'cat file2.txt’ will view the contain of 'file2.txt'.
rmdir folder1 and rm -r folder (do not execute this!)
‘rmdir foler1’ command will delete the directory (folder1). rm -r recursively removes the directory and its subdirectories.
cp file1.txt file2.txt and mv hello.txt ../folder
The cp command is used for copying files. The command ‘cp file1.txt file2.txt’ will copy file1.txt as file2.txt. The mv command is for moving files from one folder to another. The command ‘mv hello.txt../folder’ will move hello.txt into the folder.
clear and ls -l
The command clear will clear the terminal. The ls command lists the content of the current directory.
ps and kill -9 36662
The 'ps' command displays a list of all current active processes. 'kill –9' Command send an immediate message for a program to shut down, without freeing memory. And '36662' is a code of the program that can be found using 'ps' command.
ls
This command displays files form the current working directory.
cat fox.txt
This command helps to see that the files exist and the contain of the file.
vi fox.txt
This command opens vi interface to edit the fox.txt file or create new file.
i
This command is used to enter insert mode to edit the file.
Esc key returns back to normal/command mode.
Command mode
There are various commands that can be used in command mode. Here are some useful ones:
6.1. o – Opens a line after current line.
6.2. dd – Deletes current line
6.3. cc – change the whole line
6.4. p – put after the position or the line.
6.5. a – Append after cursor
6.6. :x - This command is used to save the changes and exit.
6.7 :q – Exit as long as there have been no changes.
6.8 ZZ – Exit and save changes if any have been made.
6.9 :q! – Exit and ignore changes.
ls -l fox.txt
This command will display list files and directories and its contents in long format as
-rw-rw-r--. 1 Unisushant Unisushant 12 Nov 18 14:27 fox.txt
7.1. -rw-rw-r- permissions applied to this file.
7.2. 1: number of linked hard links.
7.3. Unisushant: owner of the file.
7.4. Unisushant: To which group this file belong to.
7.5. 12: size.
7.6. Nov 18 14:27 Modification/Creation date and time.
7.7. fox.txt: file/directory name
Here is the bash script that will look in the current directory, and all directories contained within, for files with extension *.java and invoke the javac command on each of them (to compile them). (See the figure A. below)
find . -name “*.java”
This command will look for java files in the current directory and all directories contained within the directory.
for file in $ (find epic2test -name “*.java”) do
For loop is used here to iterate all the java files.
echo “Compiling $file”
It displays the message Compiling along with the name of java file.
Javac $file
This javac command creates a compiled java class file.
echo “List of Compiled Files”
“List of Compiled Files” output will be displayed.
find . -name “*.class”
This command will find all the java class file that exists in the current directory and its subdirectories.
Each iteration sets the variable (file) to the java file found in current directory and subdirectory. Displays the name of variable and the “Compiling …” message and compiles the variable. After compiling all the java files, it displays the list of files that were compiled into java class files.
To test this bash script, a skeleton folder structure was created, and some empty java files, text files and also some java file with source code were inserted into current directory and its subdirectories. Here are the processes below:
mkdir -p ~/epic2test/file1/file2
This command was used to create an epic2test directory with file1 as subdirectory and file2 as subdirectory of file1.
touch A.java
Touch command was used to create some empty java files, text files and some java files with source code in current directory and its subdirectories. (See figure B below)
In the figure B above, it shows all the files that were created in directory and its subdirectories.
bash bashscript.sh
After executing the bash script, it shows only few of the java files from the current directory and subdirectory were compiled to java class file because the java file with no class declaration cannot be converted into class java file. (See figure C below)
Empty java files (f.java, f1.java and f2.java) were not compiled but the java files with class declaration were compiled to java class file. (See the figure D above)
ls command was executed to list all the files and the compiled java file were also present in the directories as seen in the figure E above.
All the figures above prove that the script runs as expected and also the java compiled files were executed at the end as seen the figure F above.
A bash script that takes two arguments on the command line, name and phone number, and appends the information to a file called phonebook. (See the figure G. below)
fname=$1
pnumber=$2
Two arguments from a command line can be found with positional parameters $1 and $2. And fname and pnumber variable were assigned respectively.
echo "Enter your Full Name"
read fname
echo "Enter your Phone Number"
read pnumber
The echo command displays a line of string and read command reads the contents of a line into a variable.
file = “phonebook.txt”
Creating file variable and assigning to phonebook.txt file.
if ([ -n “fname” ] && [ -z “$pnumber” ]);
then
echo “Its empty”
It checks if the value of both the variables is empty. And if it is empty then it will display the message”Its empty”.
else
echo “Name: $fname” >> ${file};
echo “Phone Number: $pnumber” >> ${file};
echo “Thank you! Your name and phone numbers been added to the phonebook.”
fi
These commands will be executed if the value of both variables is not empty. The >> redirection operator appends the value of variables ($fname & $pnumber) into the given file(phonebook.txt). After appending the variables into given file, the output message “Thank you! Your name and phone numbers been added to the phonebook.” is displayed.
The following steps were taken to test the script.
Passing the name and phone number from the command line. (As seen in figure H above)
In the figure I above, cat command was executed to view the contains of the phonebook file. It shows that the bash script appends the name and phone number to the file called phonebook.