Programming Tutorials

(追記) (追記ここまで)

sh -x and sh -v in Linux

By: Dorris in Linux Tutorials on 2011年01月15日 [フレーム]

While programming shell sometimes you need to find the errors (bugs) in shell script and correct the errors (remove errors - debug). For this purpose you can use -v and -x option with sh or bash command to debug the shell script. General syntax is as follows:

Syntax:

sh  option  { shell-script-name }

OR
bash  option  { shell-script-name }

Option can be

  • -v Print shell input lines as they are read.
  • -x After expanding each simple-command, bash displays the expanded value of PS4 system variable, followed by the command and its expanded arguments.

Example:

$ cat > dsh1.sh
#
# Script to show debug of shell
#
tot=`expr 1ドル + 2ドル`
echo $tot

Press ctrl + d to save, and run it as

$ chmod 755 dsh1.sh
$ ./dsh1.sh 4 5
9
$ sh -x dsh1.sh 4 5
#
# Script to show debug of shell
#
tot=`expr 1ドル + 2ドル`
expr 1ドル + 2ドル
++ expr 4 + 5
+ tot=9
echo $tot
+ echo 9
9

See the above output, -x shows the exact values of variables (or statements are shown on screen with values).

$ sh -v dsh1.sh 4 5

Use -v option to debug complex shell script.




(追記) (追記ここまで)


Add Comment

JavaScript must be enabled for certain features to work
* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Linux )

Latest Articles (in Linux)

(追記) (追記ここまで)
(追記) (追記ここまで)

Related Tutorials

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