Bash Shell Scripting

Bash Shell scripting refers to using the Bash shell for creating scripts and executing commands in UNIX or Linux for fulfilling the user requirements. Scripts also help automate a number of repetitive tasks taking backups, etc.


Q.1 What are some of the benefits of Shell Scripting?
The following are the most significant advantages of Shell Scripting: It lets you to design your own operating system to meet your specific needs. It allows you to create software applications that are tailored to the platform you're using. It is time-saving because it assists in the automation of system management activities.
Q.2 What are Shell variables and how can I use them?
Shell variables are the most important aspect of a Shell script or application. Within a Shell application, variables allow Shell to store and change data. String variables are commonly used to hold shell variables.
Q.3 List the several kinds of variables that can be used in Shell Scripting.
A Shell Script usually has two types of variables: System-defined variables are defined in capital letters and are created by the operating system (Linux). The Set command can be used to see them. User-defined variables — These are variables that are created and specified by users of the system. Using the Echo command, you can see the variable values.
Q.4 What is the best way to make a variable unchangeable?
With read-only, you can make a variable unchangeable. Let's imagine you wish to keep the value of the variable 'a' constant at five, therefore you use readonly as follows: $ a=5 $ readonly a
Q.5 Identify the many types of shells.
Shells are divided into four categories: Shell Bourne (sh) Shell C (csh) Shell Korn (ksh) Shell's Return to Bourne (bash)
Q.6 Define Positional Parameters.
Variables declared by a Shell are known as positional parameters. By specifying arguments in the command line, they are utilised to pass information to the programme.
Q.7 In a UNIX environment, how many Shells and Kernels are available?
In most UNIX environments, there is just one Kernel. However, there are a variety of Shells to choose from.
Q.8 Is it necessary to use a different compiler to run a Shell programme?
No, a Shell programme does not require a separate compiler to run. Shell is a command-line in the shell programme that executes commands.
Q.9 In Shell Scripting, how can you change file permissions?
Umask can be used to change file permissions.
Q.10 What does the “.” (dot) at the start of a file name mean?
A hidden file has a name that begins with a “.” When you try to list files in a Shell, it usually lists all files except those that are hidden. The hidden files, on the other hand, are present in the directory. If you want to see hidden files, use the –a flag with the Is command.
Q.11 In Linux, how do you make a shortcut?
In Linux, there are two ways to make shortcuts: Hard links are those that are linked to the file's inode. They are always stored in the same directory as the file. The hard link will not be affected if the original file is deleted. Soft links are those that are associated with the file name. They could be on the same file system as the file or not. The soft link goes inactive if the original file is deleted.
Q.12 Is there a command that replaces "echo"?
Yes, the tput command can be used instead of the echo command. You can use the tput command to control how the output is presented on the screen.
Q.13 What is the maximum number of blocks in a file system?
There are four blocks in a file system: Superblock - This block contains information about the condition of a file system, including block size, block group size, usage information, empty/filled blocks and their counts, inode table size and position, and so on. Bootblock — This block contains the bootstrap loader programme, which runs when the host machine is booted. Datablock — This block contains the file system's file contents. Inode table — UNIX treats all components as files, and the inode table stores all file-related information.
Q.14 Explain Control Instructions.
Control instructions are commands that let you determine how a script's various instructions should be carried out. As a result, their principal function is to determine the control flow in a Shell programme.
Q.15 Name the three ways in which the vi editor can be used.
The following are the three modes of operation: Any key tapped by a user is treated and interpreted as editor commands in command mode. Insert mode - This mode allows you to insert new text, alter current content, and so on. Ex-command mode — A user can utilise a command line to enter all commands.
Q.16 In a Shell, how many different sorts of control instructions are there?
There are four different sorts of control instructions in a Shell: The sequence control instruction compels all instructions in the programme to be executed in the same order. The computer uses a selection/decision control instruction to determine which instruction should be run next. A repetition/loop control instruction allows the computer to repeatedly execute a set of statements. When you have a number of options to pick from, case-control instruction is employed.
Q.17 Define the term "IFS."
Internal Field Separator (IFS) stands for Internal Field Separator. It's a system variable that has the default value of space, tab, and new line. IFS marks the point of a line where one field or word ends and another begins.
Q.18 Define the term "metacharacters."
Metacharacters are special characters in a data field or programme that provide information about other characters, and they make up a Shell. In a Shell, for example, the “ls s*” command lists all files that begin with the letter ‘s.'
Q.19 Distinguish between $* and $@.
$@ interprets each quoted argument as a separate argument, whereas $* treats a set of positional parameters as a single string.
Q.20 In Shell Scripting, write the while loop syntax.
The while loop is used in Shell Scripting when you wish to repeat a block of commands numerous times. The “while” loop has the following syntax: while [test condition] do commands… done
Q.21 What's the difference between the break and continue commands?
To get out of a loop of execution, use the break command. Any loop command, including until and while loops, can be exited with the break command. The continue command, on the other hand, is used to exit the loop's current iteration without exiting the entire loop.
Q.22 Is it possible to run numerous scripts in a Shell?
Yes, many scripts can be run simultaneously in a Shell. You can invoke one script from another by executing multiple scripts. To do so, you must mention the script's name when you want it to be invoked.
Q.23 To find out how long a system has been running, what command should you use?
To find out how long a system has been running, use the uptime command. An example of the uptime command is as follows: u/user1/Shell_Scripts_2018> uptime
Q.24 What exactly do you mean by Crontab?
Crontab is an abbreviation for cron table, where Cron is a task scheduler. Crontab is a set of tasks that you want to run on a regular basis, as well as the command to manage that list.
Q.25 What exactly is a file system?
A file system is a collection of files and the data associated with them. It is in charge of how data is retrieved and saved. Without file systems, data in storage would just be a big mess with no way to determine where one piece of data ends and another starts.
Q.26 What is meant by a shell in UNIX or Linux?
A shell is a command line user interface between the end user and the kernel. When the user types command in shell, the command is sent to kernel for execution and command output is displayed to
Q.27 List some commonly used shells in Linux or UNIX
Some commonly used shells in Linux or UNIX are Bash, csh, ksh and Bourne.
Q.28 What are soft and hard links in Linux/UNIX?
Soft links are like shortcuts to file in Windows, it is a link to the file and can be placed anywhere on the disk or file system. Hard links link to the inode of the file hence, should be on same file system as the linked file. If the liked file is deleted, soft link becomes inactive as it is now a broken link and linked file cannot be accessed by the path stored in soft link. In case of hard links, as it links to inode hence it still access copy of the linked file, on its deletion.
Q.29 What will pass argument to a script in Bash shell?
Arguments to a script in Bash shell are passed by listing them after script name in double quotes and separated by space as - “Arg1” “Arg2” “Arg3” etc.
Q.30 How does a script in Bash shell will access the command line arguments to the script
Script in Bash shell can access the command line arguments by prefixing $ to the argument number like $1 or $4
Q.31 Specify the utility of $# in a Bash shell script
The $# is used to provide the count of the command line arguments passed to the Bash shell script.
Q.32 How does the shell character: $* and $@ differ in Bash shell
In Bash shell, $* and $@ differ in their treatment towards positional parameters, $*regards them as single string and $@ considers every argument which is quoted as separate argument
Q.33 Which command on Bash shell is used to change group ownership?
chgrp command changes group ownership
Q.34 Which command on Bash shell will show the UNIX or Linux version?
uname -r command is used to show the UNIX or Linux version
Q.35 Which character is used to separate multiple commands entered on the same command line in Bash shell?
The (;) character separates multiple commands on the same command line
Q.36 List the command to be given on Bash shell to end a process with ID as 408 without a graceful exit
The kill –9 command will end a process without graceful exit, as: kill –9 408
Q.37 Which regex pattern will match the following words: and, made, named, standard?
a.*d is the correct regex pattern for searching: and, made, named, standard.
Q.38 List the Bash shell command which would search lines 2 through 20 of the file records for the characters 1st and replace them with the characters first?
sed ‘2,20s/1st/first/’ records , is the correct command.
Q.39 List the Bash shell command would separate the file file01 into multiple files, each containing 60 lines
The split command is used to fulfil the task as: split -60 file01.
Q.40 How to send the data from the ls command to the file file01 in Bash shell?
The ls command in Bash shell is used with a pipe for the task as: ls | file01.
Q.41 What redirects stdout and stderr to a file?
|& is used to redirect stdout and stderr to a file on Bash shell.
Q.42 Which Bash shell command, shows the length of time, the system has been running?
The uptime command is used for the purpose.
Q.43 Differentiate between $$ and $! in Bash shell
In Bash shell, $$ and $! differ on process for which process ID is shown by them. $$ lists process ID of current executing process and $! Display process ID of process which just went to background.
Q.44 Which Bash sell command change access permissions
The chmod command on Bash shell is used to change access permissions
Q.45 List the Bash shell command to change file owner and group
The chown command on Bash shell changes file owner and group.
Q.46 Define a zombie process
A zombie process is not active and has died but their exit status has not been updated by their parent process. Zombie process have retains their process ID but are not functional.
Q.47 Which command in used to copy files from a remote machine?
Copying files from remote machines can be done by in-built commands or utilities on Bash shell like – ftp, scp or rync.
Q.48 How to securely connect to a remote machine over HTTP?
Secure connection to a remote machine over HTTP can be done by using the ssh command on Bash shell. The ssh command is used as: ssh username@serverIP -p port , then password is to be provided
Q.49 How will you monitor log files by Bash shell, which store user activity and are continuously updated?
We can use the tail command in Bash shell to monitor log files. The tail command lists the data from end of the file and by default last 10 lines are shown. The syntax for the tail command is: tail –f filename. The output of the tail command can be shown on screen or saved to another file for analysis.
Q.50 How to print text common in two text files: text01 and text02
We can use the comm command in Bash shell to print common text amongst text files: text01 and text02. The command on Bash shell will be as: comm -12 text01 text02 The -1 and -2 are joined, which suppress the display of lines in text01 and text02, and text common to both is shown.
Q.51 How will you print the 7th line of file01, in Bash shell but without the tail and head command
We can use the sed command in Bash shell as: sed –n '7p' file01
Q.52 Which command makes the scripts interactive?
Read command makes the script interactive as it stops the script execution for user input
Q.53 Which command should be executed to make the script executable?
The chmod +x

Get Govt. Certified

Certificate in Bash Shell Scripting

Know More
Get Govt. Certified Take Test
 For Support