How to know which line has errored when running on Linux using PuTTY?

puttykeyinfoputtykeyinfo
2 min read

When executing commands or scripts on a Linux system through PuTTY, understanding how to identify errors and pinpoint the specific line or command causing the issue is essential for effective troubleshooting and debugging.

Errors are common when running individual commands or complicated scripts, and understanding the root cause is crucial for fast resolving difficulties.

Here are several methods to identify which line has errored:

View the Error Message:

When an issue occurs, Linux normally displays an error message with details about what went wrong. Look for error messages that indicate the problem and the line number, if available.

Check Exit Status:

Check the exit status after running a command or script. An exit status of non-zero usually implies a mistake. The following command can be used to determine the exit status:

echo $?

The value that appear will be the exit status of the last command executed.

Check Logs:

Check system logs for any error warnings if you are running a larger script or a series of commands. The logs may contain more information about the nature of the error.

Debugging with set -x:

To enable debugging, add the following line to your script, which will print each command and its arguments to the standard error output:

#!/bin/bash

set -x

This can help you see exactly which line is being executed when the error occurs.

Use trap for Signal Handling:

The trap command can be used to catch signals, including errors. To catch errors and print a custom error message, for example, add the following line to your script:

#!/bin/bash

trap 'echo "Error at line $LINENO"; exit 1' ERR

When any command returns a non-zero exit status, this will be triggered.

You can successfully identify the line or command that caused an error while running scripts or commands on Linux via PuTTY by using these methods. Make changes based on your individual needs and the type of the scripts you're working with.

you can also used SSH key authentication method to connect to Docker container by using PuTTY.

0
Subscribe to my newsletter

Read articles from puttykeyinfo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

puttykeyinfo
puttykeyinfo

puttykeyinfo is your full stop about putty.