1

As a (Debian) Linux newbie I was wondering if there is a way to track the execution of a bash shell (.sh) script ? Like in Windows you trigger that with a echo on command in a batch file.

My first 'unix' script ever, but I'm wondering if it will run as expected :

#!/bin/bash  
# switch maintenance mode off and go live with your ownCloud  
occpath='/var/www/owncloud'  
htuser='www-data'  
alias occ-do=sudo -u $htuser php $occpath/occ  
echo info: Switching to live-mode ..  
occ-do maintenance:mode --on`  

the line I'm trying to create is like sudo -u www-data php /var/www/owncloud/occ (occ is what should be executed, let's assume I don't need a path for the php part).

Will this work; any mistakes in the syntax? And let's not forget my question about the execution following ..

  • Hmm, not entirely happy with the way my script is shown but I guess it's clear, right? – Supr Mar 16 '16 at 21:15
  • I fixed it to use code markdown. Markdown help – DavidPostill Mar 16 '16 at 21:20
1

Is there is a way to track the execution of a bash shell (.sh) script?

You can use set -x, set +x and set -v as described below.

  • set -x : Display commands and their arguments as they are executed.
  • set +x : Turn off debugging
  • set -v : Display shell input lines as they are read.

HowTo: Debug a Shell Script Under Linux or UNIX

1. Use -x option to debug a shell script

Run a shell script with -x option.

$ bash -x script-name
$ bash -x domains.sh

2. Use of set builtin command

Bash shell offers debugging options which can be turn on or off using set command.

  • set -x : Display commands and their arguments as they are executed.
  • set -v : Display shell input lines as they are read.

You can use above two commands in shell script itself:

#!/bin/bash
clear
# turn on debug mode
set -x
for f in *
do
   file $f
done
# turn OFF debug mode
set +x
ls
# more commands

You can replace the standard Shebang line:

#!/bin/bash

with the following (for debugging) code:

#!/bin/bash -xv

Source HowTo: Debug a Shell Script Under Linux or UNIX


Further Reading

0

Put

set -x

at the point where you want debug info start to showand

set +x

where you want it off. I don't promise what you see will always be straightforward but you can get used to id after a while.

0

This is the simple way to check the syntax of your shell scripts :

https://www.shellcheck.net/

Here in this website, the online syntax checker marks the line & displays the error which is caused due to incorrect syntax

As per the suggestion from other answers, you can try set -x & set +x to enable & disable debug mode respectively for tracking the execution of script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.