Shell Scripting: Part 2
Shell scripting is automating our day to day linux activities. We write lines of shell commands for Unix based Operating System
We have covered the basics of shell scripting in our previous blog.
Vim
Vim is a text editor used to edit all kinds of plain text.
Vim is used to open the file/write inside the file
vim <file_name> // Above command is used to go in Vim editor mode for the particular file. // If that file doesn't exist then the above command will create the file
Below Image show VIM mode. To write inside the vim we need to press ESC key and then press 'i' to go in insert mode. If we want to save the changes after we edit in it we have to again press 'esc' and write ':wq!' . This will save and exit. TO just exit write ':q' .
Write a simple Shell Script
#!/bin/bash
########################
#Name : Vijay Patil
#About : This is a simple Shell script to get familiar with its structure
#Output the shell script in debug mode
set -x
#Let's make a directory
mkdir play
#Change directory to play
cd play
#create a file
touch name.txt
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
:wq!
when we execute the shell script
ubuntu@ip-172-31-16-221:~$ chmod 777 file.sh
ubuntu@ip-172-31-16-221:~$ ./file.sh // executing sh file
+ mkdir play
+ cd play
+ touch name.txt
//now lets see if the shell script is executed
ubuntu@ip-172-31-16-221:~$ ls
file.sh play
ubuntu@ip-172-31-16-221:~$ cd play
ubuntu@ip-172-31-16-221:~/play$ ls
name.txt
//as we can see name.txt is generated by above shell script.
ubuntu@ip-172-31-16-221:~/play$
CHMOD
chmod is a command that grants permissions
chmod <number according to permission> <File_name>
File Ownership and Permissions
OWNERSHIP: Every file and directory is assigned 3 types of owner.
1)User: User is owner of fle. By default , the person who creates the file/directory becomes its owner.
2)Group: A user group contains multiple users. All users belonging to the same group have same Linux group permissions to access the files.
3)Other : Any other user who can access the files. The person has neither created the files nor belongs to the user group.
PERMISSIONS: Every File and Directory has three types of permissions
1)Read: Only able to open and read the file. We can list the contents.
2)Write: We can add, remove and rename files stored in the directory.
3)Execute: In Unix/Linux we can not run the program unless the execute permission is set
When we run the command (ls -l) to see file permissions :
(-rwxrwxrwx) and (drwxrwxr-x) these are file permissions.
'-' means for file and 'd' means directory
r-read
w-write
x-execute
'-' = no permission
first three letters after '-' is User (rwx)
Next three are Group (rwx)
and last three are Others (rwx)
Numeric Mode for Permissions
Number | Permission Type | Symbol |
0 | No Permission | — |
1 | Execute | –x |
2 | Write | -w- |
3 | Execute + Write | -wx |
4 | Read | r– |
5 | Read + Execute | r-x |
6 | Read +Write | rw- |
7 | Read + Write +Execute | rwx |
ex: 457 : Owner can Read, User group can Read+ Execute and Others can Read + Write + Execute
More Commands
History command is used to know the commands used till now
~history
To know the disk space available
~df
To get a complete picture of the total Ram, how much is used, and its free space we use free command
~free
To know how many cpu;s on our machine we use
~nproc
To show all linux processes.
~top
To know active running processes.
ps -ef
Grep command is used to find only o/p required from the total o/p received
grep <what_you_want> <file_name>
AWK Command:
want to find specific word
If we want "learn" word as output
' | ' is called pipe. Output of first command is given to second command
Shell Script
Every shell script starts by mentioning executables using sh-bang. For ex: #!/bin/bash
What is the difference between #!l/bin/bash and #!/bin/sh ? First let's break down above script in two parts
1) #! :- This is called Shebang. This tell our operating system which interpreter to be used in place of what os already use.
2)/bin/sh and /bin/bash : These are the executables by which the commands are executed according to the interpreter mentioned. Here #!/bin/bash means shebang is telling operating system to use bash as the interpreter for remaining scripts in place of default system shell #!/bin/sh , it is symbolic link pointing to the executable for whichever shell is the system shell. It can be anything like Bash / Dash