In UNIX, the Command Shell is the native command interpreter. It provides a command line interface for the users to interact with the operating system. UNIX commands may also be executed non-interactively in the form of a Shell Script. The script is a series of commands that will be run together. Shell scripts can be used for a variety of tasks from customizing your environments to automating your daily tasks. A shell script is a text file that contains a sequence of commands for a UNIX-based operating system. It’s called a shell script because it combines into a “script” in a single file a sequence of commands that would otherwise have to be presented to the system from a keyboard one at a time.
What is Shell Scripting?
Shell scripting is writing a series of command for the shell to execute. It can combine lengthy and repetitive sequences of commands into a single and simple script, which can be stored and executed anytime. This reduces the effort required by the end user.
Let us understand the steps in creating a Shell Script
Create a file using a vi editor (or any other editor). Name script file with extension .sh
Start the script with #! /bin/sh
Write some code.
Save the script file as filename.sh
For executing the script type bash filename.sh
“#!” is an operator called shebang which directs the script to the interpreter location. So, if we use”#! /bin/sh” the script gets directed to the bourne-shell.
What are the different File test operators?
File Test Operator: These operators are used to test a particular property of a file.
-b operator: This operator check weather a file is a block special file or not. It returns true, if the file is a block special file otherwise false.
-c operator: This operator checks weather a file is a character special file or not. It returns true if it is a character special file otherwise false.
-d operator: This operator checks if the given directory exists or not. If it exits then operators returns true otherwise false.
-e operator: This operator checks weather the given file exits or not. If it exits this operator returns true otherwise false.
-r operator: This operator checks weather the given file has read access or not. If it has read access then it returns true otherwise false.
-w operator: This operator check whether the given file has write access or not. If it has write then it returns true otherwise false.
-x operator: This operator check whether the given file has execute access or not. If it has executed access then it returns true otherwise false.
-s operator: This operator checks the size of the given file. If the size of given file is greater than 0 then it returns true otherwise it false.
In Linux Shell Scripting, there are two types of variable
System variables: Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS.
User-defined variables: Created and maintained by the user. This type of variable defined in lower letters.
System variables can be used in the script to show any information these variables are holding.
Like few important System variables are:
- BASH — Holds our shell name
- BASH_VERSION — Holds our shell version name
- HOME — Holds home directory path
- OSTYPE — Holds OS type
- USERNAME – Holds username who is currently logged in to the machine
Can you explain nano?
nano is a free clone of the text editor supplied with the pine email program. nano is very easy to use but is very short on features. I recommend nano for first-time users who need a command line editor.
Can you explain vi, vim?
The granddaddy of Unix text editors, vi, is infamous for its difficult, non-intuitive command structure. On the bright side, vi is powerful, lightweight, and fast. Learning vi is a Unix write of passage, since it is universally available on Unix-like systems. On most Linux distributions, an enhanced version of the traditional vi editor called vim is used.
What are the common prinf controls?
Control Usage
- \” Double quote
- \\ Backslash
- \b Backspace
- \c Produce no further output
- \e Escape
- \n New Line
- \r Carriage Return
- \t Horizontal tab
- \v Vertical Tab
Please enter a title attribute
pwd stands for Print Working Directory. pwd is shell built-in command(pwd) or an actual binary(/bin/pwd).The pwd
command is a command line utility for printing the current working directory. It will print the full system path of the current working directory to standard output. By default the pwd
command ignores symlinks, although the full physical path of a current directory can be shown with an option. The pwd
command is normally a shell builtin meaning it is part of the code that runs the shell rather than an external executable.
Explain find command ‘Find files with different file extensions’
The syntax to find multiple filename extensions with one command looks like this:
find . -type f \( -name “*.c” -o -name “*.sh” \)
Just keep adding more “-o” (or) options for each filename extension.
Explain the find command ‘Find directories with the Unix find command’?
Every option you just saw for finding files can also be used on directories. Just replace the -f option with a -d option. For instance, to find all directories named build under the current directory, use this command:
find . -type d -name build
Explain the Find and delete command?
Be very careful with these next two commands. If you type them in wrong, or make the wrong assumptions about what you’re searching for, you can delete a lot of files very fast. Make sure you have backups and all that, you have been warned.
Here’s how to find all files beneath the current directory that begin with the letters ‘Foo’ and delete them.
find . -type f -name “Foo*” -exec rm {} \;
This one is even more dangerous. It finds all directories named CVS, and deletes them and their contents. Just like the previous command, be very careful with this command, it is dangerous(!), and not recommended for newbies, or if you don’t have a backup.
find. -Type d -name CVS -exec rm -r {} \;
What are zombie processes?
These are the processes which have died but whose exit status is still not picked by the parent process. These processes even if not functional still have its process id entry in the process table.
How to run the script in the background?
Add “&” to the end of the script.
What difference between ‘and “quotes?
– ‘- : we use it when do not want to evaluate variables to the values.
-”-–: all variables will be evaluated and its values will be assigned instead.
Write a script to print the first 10 elements of Fibonacci series.
#!/bin/sh
a=1
b=1
echo $a
echo $b
for I in 1 2 3 4 5 6 7 8
do
c=a
b=$a
b=$(($a+$c))
echo $b
done
How will you copy a file from one machine to other?
We can use utilities like “ftp,” “scp” or “rsync” to copy a file from one machine to other.
E.g., Using ftp:
FTP hostname
>put file1
>bye
Above copies, file file1 from the local system to destination system whose hostname is specified.
What are control instructions and how many types of control instructions are available in a shell? Explain in brief.
Control Instructions are the ones, which enable us to specify the order in which the various instructions in a program/script are to be executed by the computer. Basically, they determine a flow of control in a program.
There are 4 types of control instructions that are available in a shell:
Sequence Control Instruction –: This ensures that the instructions are executed in the same order in which they appear in the program.
Selection or Decision Control Instruction –: It allows the computer to take a decision as to which instruction is to be executed next.
Repetition or Loop Control Instruction –: It helps a computer to execute a group of statements repeatedly.
Case-Control Instruction –: This is used when we need to select from several alternatives.
What are loops and explain three different methods of loops in brief?
Loops are the ones, which involve repeating some portion of the program/script either a specified number of times or until a particular condition is being satisfied.
3 methods of loops are:
For loop: This is the most commonly used loop. For loop allows specifying a list of values which the control variable in the loop can take. The loop is then executed for each value mentioned in the list.
While loop: This is used in a program when we want to do something for a fixed number of times. While loop gets executed till it returns a zero value.
Until loop: This is similar to while loop except that the loop executes until the condition is true. Until loop gets executed at least once till it returns a non-zero value.
What are the 3 standard streams in Linux?
0 – Standard Input1 – Standard Output2 – Standard Error
What difference between & and &&?
&: we use it when we want to put the script to background
&&: when we want to execute command/script if the first script was finished successfully
How can you find out how long the system has been running?
We can find this by using the command “uptime”.
What is a boot block in Linux file system?
A bootblock – contains a primary boot program for the operating system.This block contains a small program called “Master Boot record”(MBR) which loads the kernel during system boot up.
What is a super block in Linux file system ?
A superblock – static parameters of the file system, like total size, block and fragment sizes of data
blocks.super block contains all the information about the file system like the size of file system, block size used by its number of free data blocks and list of free inodes and data blocks.
What is the difference between $$ and $!?
$$ gives the process id of the currently executing process whereas $! Shows the process id of the process that recently went into the background.
What are the different commands available to check the disk usage?
There are three different commands available to check the disk usage.
And they are:
df – This command is used to check the free disk space.
du – This command is used to check the directory wise disk usage.
dfspace – This command is used to check the free disk space in terms of MB.
What will happen to my current process when I execute a command using exec?
“exec” overlays the newly forked process on the current process; so when I execute the command using exec, the command gets executed on the current shell without creating any new processes.
E.g., Executing “exec ls” on command prompt will execute ls and once ls exits, the process will shut down.
What is IFS?
IFS stand for Internal Field Separator. And it is one of the system variables. By default, its value is space, tab, and a new line.
How will you find the total disk space used by a specific user?”]
du -s /home/user1 ….where user1 is the user for whom the total disk space needs to be found.
What is the difference between diff and cmp commands?
diff: Basically, it tells about the changes which need to be made to make files identical.
cmp: Basically it compares two files byte by byte and displays the very first mismatch
What is an inode block?
This block contains the inode for every file of the file system along with all the file attributes except its name.
What is the difference between grep and egrep?
egrep is extended grep that supports added grep features like “+” (1 or more occurrence of a previous character),”?”(0 or 1 occurrence of a previous character) and “|” (alternate matching)
What is the importance of writing shell scripts?
The points given below explain the importance of writing shell scripts.
• Shell script takes input from the user, file and displays it on the screen.
• Shell scripting is very useful in creating your own commands.
• It is helpful in automating some tasks of the day to day life.
• It is useful for automating system administration tasks.
• Mainly it saves time.
How many fields are present in a crontab file and what does each field specify?
The crontab file has six fields. The first five fields tell cron when to execute the command: minute (0-59), hour (0-23), day (1-31), month (1-12), and day of the week (0-6, Sunday = 0).
And the sixth field contains the command to be executed.
What are the default permissions of a file when it is created?
666 i.e. rw-rw-rw- is the default permission of a file when it is created.
What are Shell Variables?
Shell variables are the main part of shell programming or scripting. They mainly provide the ability to store and manipulate information within a shell program.
What is the lifespan of a variable inside a shell script?
The lifespan of a variable inside shell script is only until the end of execution.
What are positional parameters?
Positional parameters are the variables defined by a shell. And they are used whenever we need to convey information to the program. And this can be done by specifying arguments at the command line.
There are totally 9 positional parameters present i.e. from $1 to $9
What are the three different security provisions provided by UNIX for a file or data?
Three different security provisions provided by UNIX for a file or data are:
- It provides a unique user id and password to the user, so that unknown or unauthorized person should not be able to access it.
- At file level, it provides security by providing read, write & execute permissions for accessing the files.
- Lastly, it provides security using file encryption. This method allows encoding a file in an unreadable format. Even if someone succeeds in opening a file, but they cannot read its contents until and unless it is decrypted