获取 bash 脚本以回显正在执行的命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9823246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:50:05  来源:igfitidea点击:

getting bash script to echo the command being executed

bash

提问by scphantm

i put together a bunch of alias commands in a folder. mainly ssh, so instead of having to type

我将一堆别名命令放在一个文件夹中。主要是 ssh,所以不必输入

ssh [user]@[server]

ten times a day, i can just type

一天十次,我只能打字

server

and the alias script fires. the script is simply the command

并且别名脚本触发。脚本只是命令

ssh [user]@[server]

this is all working fine. but i was wondering if there was a way in bash where instead of firing the ssh command quietly, it would display the command that is being executed?

这一切正常。但我想知道 bash 中是否有一种方法,而不是安静地触发 ssh 命令,它会显示正在执行的命令?

回答by Rob Hruska

You can debug the scriptwith -x, which will echo the commands and arguments.

您可以调试脚本-x,这将呼应的命令和参数。

bash -x script.sh

You can do this for specific portions of the file, too (from section 2.3.2 linked above):

您也可以对文件的特定部分执行此操作(来自上面链接的第 2.3.2 节):

set -x          # activate debugging from here
ssh [email protected] ...
set +x          # stop debugging from here

The output from -xmight be a bit too verbose for what you're looking for, though, since it's meant for debugging (and not logging).

但是,从的输出-x对于您要查找的内容来说可能有点过于冗长,因为它用于调试(而不是日志记录)。

You might be better off just writing out your own echostatements - that'd give you full control over the output.

您最好只写出自己的echo语句 - 这会让您完全控制输出。

回答by nooj

Put "-x" at the top of your script instead of on the command line:

将“-x”放在脚本的顶部而不是命令行上:

$ cat ./server
#!/bin/bash -x
ssh user@server

$ ./server
+ ssh user@server
user@server's password: ^C
$