2018年11月17日 星期六

[Linux 常見問題] How to echo shell commands as they are executed?

Source From Here 
Question 
In a shell script how do I echo all shell commands called and expand any variable names? For example, given the following line: 
I would like the script to run the command and display the following 
ls /full/path/to/some/dir

The purpose is to save a log of all shell commands called and their arguments. Perhaps there is a better way of generating such a a log? 

How-To 
set -x or set -o xtrace expands variables and prints a little + sign before the line. 
set -v or set -o verbose does not expand the variables before printing. 

Use set +x and set +v to turn off the above settings. 

On the first line of the script, one can put #!/bin/sh -x (or -v) to have the same effect as set -x (or -v) later in the script. Below is a sample bash script to show the effect: 
- test.sh 
  1. #!/bin/sh  
  2. set -x  
  3. echo "Repeat command"  
  4.   
  5. set +x  
  6. echo "No repeat command"  
Execution result: 
# ./test.sh
+ echo 'Repeat command'
Repeat command
+ set +x
No repeat command

Supplement 
set - Linux Command - Unix Command

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...