Linux Classes
Linux Classes
Share This With a Friend
LINUX CLASSES - PROGRAMMING

Can I Debug a Shell Script?

Debugging Shell Scripts

Sometimes shell scripts just don't work the way you think they should, or you get strange error messages when running your script. Just remember: The computer is always right. It's easy to omit a significant blank, quotation mark, or bracket, or to mistakenly use a single quotation mark when you should have used double quotation marks or a backtick. When you get unexpected behavior from a shell script and you're thinking "I just know this is coded right . . . the computer is wrong!"--remember: The computer is always right. But fortunately, there is help. By prefixing your shell invocation with bash -x, you can turn on tracing to see what's really happening. Let's say we have the following listarg script:

count=1
until [ "$*" = "" ]
do
echo "Arg $count : 1ドル "
shift
count=$count+1
done

At first glance, it looks golden, but for some reason the counter is not incrementing properly. Try running it with the command

% bash -x listarg abc def

and look at the trace output as the shell executes. The lines prefixed with a plus sign show the progress of the running script, and the lines without the plus sign are the script's normal output.

+ count=1
+ [ abc def = ]
+ echo Arg 1 : abc
Arg 1 : abc
+ shift
+ count=1+1
Hmmm . . .
+ [ def = ]
+ echo Arg 1+1 : def
Arg 1+1 : def
Not Good!
+ shift
+ count=1+1+1
+ [ = ]

Instead of printing Arg 2 : def, we got Arg 1+1 : def. But the trace output line reading count=1+1 nails the problem. You forgot to use the expr command, so the shell is treating this as a string concatenate instead of a mathematical calculation.

Note: You can always press Ctrl-C to stop a running shell script. This is handy if you accidentally create a script with an infinite loop (one that will never end by itself).

Previous Lesson: Shell Script Looping
Next Lesson: Perl Basics

[ RETURN TO INDEX ]



Comments - most recent first
(Please feel free to answer questions posted by others!)

Boss (26 Sep 2012, 07:05)
Bob,

I got a script which when run, calls few other scripts conditionally, when executed. I would want to know how to debug the execution. Its a big no. of files and they dont have 'set -xv' also in them. How will i get to know which scripts(file name) are being executed. Also is there a way wherein i get to see the commands being executed by the various shell scripts that are called(executed) without having to change each of those scripts to add a set or print command.
Prat (19 Aug 2011, 09:42)
Hey Bob!!Many thanks for the great explanation of each and every topic.
Just wanted to suggest you one thing, if we get a lessons on database connectivity as well, would be really appreciated.
Ragz (20 Jul 2011, 12:59)
Lolz..good one! Now I know how to bust some one :)
Bob Rankin (20 Jul 2011, 12:56)
@Ragz - Look for the common elements in my answer, and his question. Then you'll discover the "secret" behind it. :-)
Ragz (20 Jul 2011, 12:51)
Lol @ Bob! What ever your reply was to Amit..I'm sure you meant every bit of it..that was really hilarious!
BTW..what did it mean? Would it even make sense if I learn a new language?
Bob Rankin (03 Feb 2011, 11:22)
@Amit - AIE YUM TAR CEUEO DAY ON A FUDD VMII I MESTY EO TTEI. :-)
Amit Singh (02 Feb 2011, 23:13)
hey bob rAktIm ,,i am really imprEssed bY Ur soo Much TecqniqAl wayssz to express uR CodEs..bUt i want ur rEspOnse sir <back>..vEry Fast!!nD doon gimme AnY kind Of excuse please mah sir..i Need A full support From Ur siDe..nD i too Very Much effecIent In codIng nd in hosting networking ModElszz soo eaSily!!.iTs really mY grEat honOur To attenTd ur tEaching guidlIneszz!!
John Ndambuki (29 Jul 2010, 16:41)

+ An alternative solution:

Solution #3
-----------

count=1
until [ "$*" = "" ]
do echo "Arg $count : 1ドル "
shift
count=$(($count+1))
done
John Ndambuki (29 Jul 2010, 04:41)
Just adding 2 more solutions for this:

Solution #1
-----------

count=1
until [ "$*" = "" ]
do echo "Arg $count : 1ドル "
shift
let count+=1
done

Solution #2
-----------

count=1
until [ "$*" = "" ]
do echo "Arg $count : 1ドル "
shift
let count=count+1
done
senthil (28 Apr 2010, 04:31)
'''sh -x listarg abc def ''' Also Works , thanks
Bob Rankin (07 Apr 2010, 13:29)
That doesn't work either. As I mentioned above, you need to use the expr command like this:
count=`expr $count + 1`
nolan (06 Apr 2010, 17:38)
minor tweak needed...
line 6 needs a space on each side of the "+".

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:




Copyright © by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.

Popular Linux Topics

Linux Intro
Linux Files
Linux Commands
Change Password
Copy Files
Linux Shell Basics

Linux Tutorial

Who is Doctor Bob?
What is Linux?
History of Unix
Operating Systems
What's Next?

Linux Basics

Living in a Shell
Root and Other Users
Virtual Consoles
Logoff and Shutdown
Choosing a Shell
The Command Prompt
Wildcards
Command History
Aliases
Redirection
Pipelines
Processes
Stopping a Program
Environment Variables
Help!

Linux Files

The Linux File System
Linux File Names
Linux Directories
Directory Terminology
Navigating the File System
Listing Linux Files
Displaying Linux Files
Copying and Renaming Files
Creating Files and Directories
Deleting Files and Directories
Linux Files - Wildcards
The Nine Deadly Keystrokes
Linux File Permissions
Changing File Permissions

Linux Commands

Important Linux Commands
Changing Your Password
Switching Users
Who is Logged In?
Date and Time
The Echo Command
Spell Checking
Printing Linux Files
Joining Files
Searching for Files
Comparing Files
Task Scheduling
Linking Files

Linux Editors

The Vi Editor
The Emacs Editor
The Pico Editor

Linux Data Manipulation

Slicing & Dicing
Heads or Tails?
Sorting Data
Eliminating Duplicates
Selecting Columns
Selecting Records
Search & Replace
Crunching Data
Finding Files
Pipe Fitting

Linux Shell Programming

Linux Shell Scripts
Executing a Script
Shell Script Variables
Shell Script Logic
Shell Script Looping
Shell Script Debugging

Perl Programming

Perl Basics
Perl Variables
Perl Arguments
Perl Logic
Perl Looping
Perl and Files
Perl Pattern Matching

Linux and Email

Sending Email
Reading Email
Other Mail Commands
Using Pine for Email
The Pine Inbox
Pine Email Basics
Pine Email Folders
Pine for Power Users

Compression and Encoding

Linux File Compression
Archiving With Tar
Compression With Gzip
Compress and Zcat
Zmore and Zless
Zip and Unzip
Encoding and Decoding
Encryption

Linux Does DOS

Accesing DOS Files
Accesing DOS Partitions
Running DOS Programs

Managing Linux

Updating Your Linux System
Installing Packages with RPM
Uninstalling Packages w/ RPM
Upgrading Packages with RPM
Querying Packages with RPM

AltStyle によって変換されたページ (->オリジナル) /