We are building EduLadder(ELADR) - Protocol

The Eladr Protocol is a decentralized, security and efficiency enhanced Web3 noSQL database powered by IPFS as the data storage layer https://ipfs.io/, and the Cardano block chain as the rewards token platform, https://cardano.org/. It provides a JSON based, IPFS layer 2 solution for data indexing and retrieval in an 'append only' file system built with open source Node.js API libraries.

The ELADR token was designed to incentivize and reward community members as a proof of contribution. Token holders are also granted access to EduLadder.com premium features as well as associated ELADR token enabled apps.

WHITE PAPER Buy Now Try BETA

Real Problems! Real Experts!

Join Our Telegram Channel !


The Eduladder is a community of students, teachers, and programmers. We help you to solve your academic and programming questions fast.
In eduladder you can Ask,Answer,Listen,Earn and Download Questions and Question papers.
Watch related videos of your favorite subject.
Connect with students from different parts of the world.
Apply or Post Jobs, Courses ,Internships and Volunteering opportunity. For FREE
See Our team
Wondering how we keep quality?
Got unsolved questions? Ask Questions

Shell-Script-interview-questions-intermediate-->View question

Are you familiar with the methods that are useful in debugging a problem related to the shell script?

Are you familiar with the methods that are useful in debugging a problem related to the shell script?


Asked On2019-06-18 16:14:36 by:gokilapriya

Taged users:


Likes:
Be first to like this question

Dislikes:
Be first to dislike this question
Talk about this  Like  Dislike
View all questions
Answers
The debugging options available in the Bash shell can be switched on and off in multiple ways. Within scripts, we can either use the set command or add an option to the shebang line. However, another approach is to explicitly specify the debugging options in the command-line while executing the script.

Answerd on:2022-07-01 Answerd By:nalimela-vamshi

Likes:
|nalimela-vamshi

Dislikes:
Be first to dislike this answer
Talk about this  Like  Dislike

several techniques for debugging shell scripts, with a concentration on the following:

  • Syntax checking

  • Shell tracing

Enabling Debugging

By now, you are quite familiar with the basic syntax for executing a shell script:

$ script arg1 arg2 ... argN

Here script is the name of the script and arg1 through argN are the arguments to the script.

An alternative method to execute a shell script is

$ /bin/sh opt script arg1 arg2 ... argN

This invokes the shell, in this case /bin/sh, with the debugging option specified by opt and instructs the shell to execute script. Table 20.1 lists the various debugging options.

A second way to enable debugging is to change the first line of script. Usually, the first line of a script is

#!/bin/sh

UNIX uses this line to determine the shell you can use to execute a script. This indicates that the shell /bin/sh should be used to execute the script. You can modify this line, as follows, in order to specify a debugging option:

#!/bin/sh opt

These methods for enabling debugging modes take effect when a script is invoked, so they are sometimes referred to as invocation activated debugging modes.

Table 20.1 Debugging Options for Shell Scripts

Name

Option

Description

No Exec

-n

Reads all commands, but does not execute them.

Verbose

-v

Displays all lines as they are read.

Execution Trace

-x

-Displays commands and their arguments as they are executed. Often referred to as shell tracing.


Debugging and $-

When one of the debugging options is activated, a letter corresponding to that option is added to the variable $-. For example, if the -v (verbose) option is used, the letter v is added to $-. Similarly when the -x is used, the letter x is added to $-.

You can detect if one of these options is active by using a case statement similar to the following:

case $- in
  *v*) : # verbose mode 
    ;;
  *x*) : # shell tracing mode
    ;;
esacdebugging modes

Using the set command

In the invocation activated debugging modes, the debugging mode takes effect at the start of the script and remains in effect until the script exits. Most of the time you need to debug just one function or a small section of your script. In these cases, enabling debugging for the entire script is overkill.

As you will see later in this chapter, the debugging output is quite extensive, and it is often hard to sort out the real errors from the noise. You can address this problem by using the set command to enable the debugging modes just in the parts of the script where you need debugging information.

Enabling Debugging Using set

The basic syntax of the set command is

set opt

Here opt is one of the options listed in Table 20.1.

The set command can be used anywhere in a shell script, and many scripts use it to change the debugging flags as part of the normal execution of the script. Because these debugging modes are activated only when the shell script programmer uses the set command, they are sometimes referred to as programmer activated modes.

Consider the following excerpt from a shell script (the line numbers are provided for your reference):

  1 #!/bin/sh
  2 set -x
  3 if [ -z "$1" ] ; then
  4   echo "ERROR: Insufficient Args." 
  5   exit 1
  6 fi

This script enables shell tracing (the -x option) on line 2:

set -x

Because this command occurs before the if statement (lines 3 through 6), shell tracing will be active while the if statement executes. Unless explicitly disabled later in the script, shell tracing will remain in effect until the script exits. You will look at the effect of shell tracing on the output of a script in the "Shell Tracing" section of this chapter.

Disabling Debugging Using set

You can use the set command to disable a debugging mode as follows:

set +opt

Here opt is a letter corresponding to one of the options given in Table 20.1. For example, the following command disables shell tracing:

$ set +x

To deactivate any and all the debugging modes that have been enabled, you can use the command:

$ set -

Enabling Debugging for a Single Function

One of the most common uses of the set command is to enable a particular debugging mode before a function executes and then disable debugging when the function finishes.

For example, say you have a problematic function named BuggyFunction() and you want to enable shell tracing only while that function executes. You could use the following command:

set -x ; BuggyFunction; set +x ;

Here the debugging mode is enabled just before the function is called and is disabled after the function completes. This method is favored over explicitly using the set command inside a function to enable debugging because it enables the implementation of the function to remain unchanged.


Answerd on:2019-08-29 Answerd By:Tanisha-Garg

Likes:
Be first to like this answer

Dislikes:
Be first to dislike this answer
Talk about this  Like  Dislike

Type your answer here in no less than 50 words :



Lets together make the web is a better place

We made eduladder by keeping the ideology of building a supermarket of all the educational material available under one roof. We are doing it with the help of individual contributors like you, interns and employees. So the resources you are looking for can be easily available and accessible also with the freedom of remix reuse and reshare our content under the terms of creative commons license with attribution required close.

You can also contribute to our vision of "Helping student to pass any exams" with these.
Answer a question: You can answer the questions not yet answered in eduladder.How to answer a question
Career: Work or do your internship with us.Work with us
Create a video: You can teach anything and everything each video should be less than five minutes should cover the idea less than five min.How to upload a video on eduladder