echo命令,然后运行它? (类似于make)(Echo command, and then r

2019-07-31 08:07发布

有没有办法让庆典变成了一种过于冗长模式,其中,这样,当它运行一个shell脚本,它呼应了它会运行之前运行的命令? 也就是说,以便它可以看到已运行的命令(以及它们的输出),类似的输出make

也就是说,如果运行像一个shell脚本

echo "Hello, World"

我想下面的输出

echo "Hello, World"
Hello, World

或者,是否有可能写称为bash函数echo_and_run将输出一个命令,然后运行它?

$ echo_and_run echo "Hello, World"
echo "Hello, World"
Hello, World

Answer 1:

你可以让自己的功能, echo之前调用命令eval

击也有一个调试功能。 一旦您set -x bash将执行前显示的每个命令。

cnicutar@shell:~/dir$ set -x
cnicutar@shell:~/dir$ ls
+ ls --color=auto
a  b  c  d  e  f


Answer 2:

为了回答你问题的第二部分,这里有你想要做什么shell函数:

echo_and_run() { echo "$*" ; "$@" ; }

我用类似这样:

echo_and_run() { echo "\$ $*" ; "$@" ; }

它打印$在命令前(它看起来像一个shell提示,并使其更清晰,它是一个命令)。 当我想展示一些(但不是全部),它在执行命令我有时在脚本中使用此。

正如其他人所说,它失去了引号:

$ echo_and_run echo "Hello, world"
$ echo Hello, world
Hello, world
$ 

但我不认为有,以避免任何好办法; 外壳去掉引号之前echo_and_run都有机会看到它们。 你可以写一个脚本,将检查包含空格和其他shell元字符的参数,并根据需要(这仍然不一定匹配引号,你实际键入)加上引号。



Answer 3:

这是可能的使用bash的printf结合%q格式说明逃跑,使空格都被保留的参数:

function echo_and_run {
  echo "$" "$@"
  eval $(printf '%q ' "$@") < /dev/tty
}


Answer 4:

要添加到别人的实现,这是我的基本的脚本样板,包括参数解析(如果你切换冗长水平,这是非常重要的)。

#!/bin/sh

# Control verbosity
VERBOSE=0

# For use in usage() and in log messages
SCRIPT_NAME="$(basename $0)"

ARGS=()

# Usage function: tells the user what's up, then exits.  ALWAYS implement this.
# Optionally, prints an error message
# usage [{errorLevel} {message...}
function usage() {
    local RET=0
    if [ $# -gt 0 ]; then
        RET=$1; shift;
    fi
    if [ $# -gt 0 ]; then
        log "[$SCRIPT_NAME] ${@}"
    fi
    log "Describe this script"
    log "Usage: $SCRIPT_NAME [-v|-q]" # List further options here
    log "   -v|--verbose    Be more verbose"
    log "   -q|--quiet      Be less verbose"
    exit $RET
}

# Write a message to stderr
# log {message...}
function log() {
    echo "${@}" >&2
}

# Write an informative message with decoration
# info {message...}
function info() {
    if [ $VERBOSE -gt 0 ]; then
        log "[$SCRIPT_NAME] ${@}"
    fi
}

# Write an warning message with decoration
# warn {message...}
function warn() {
    if [ $VERBOSE -gt 0 ]; then
        log "[$SCRIPT_NAME] Warning: ${@}"
    fi
}

# Write an error and exit
# error {errorLevel} {message...}
function error() {
    local LEVEL=$1; shift
    if [ $VERBOSE -gt -1 ]; then
        log "[$SCRIPT_NAME] Error: ${@}"
    fi
    exit $LEVEL
}

# Write out a command and run it
# vexec {minVerbosity} {prefixMessage} {command...}
function vexec() {
    local LEVEL=$1; shift
    local MSG="$1"; shift
    if [ $VERBOSE -ge $LEVEL ]; then
        echo -n "$MSG: "
        local CMD=( )
        for i in "${@}"; do
            # Replace argument's spaces with ''; if different, quote the string
            if [ "$i" != "${i/ /}" ]; then
                CMD=( ${CMD[@]} "'${i}'" )
            else
                CMD=( ${CMD[@]} $i )
            fi
        done
        echo "${CMD[@]}"
    fi
    ${@}
}

# Loop over arguments; we'll be shifting the list as we go,
# so we keep going until $1 is empty
while [ -n "$1" ]; do
    # Capture and shift the argument.
    ARG="$1"
    shift
    case "$ARG" in
        # User requested help; sometimes they do this at the end of a command
        # while they're building it.  By capturing and exiting, we avoid doing
        # work before it's intended.
        -h|-\?|-help|--help)
            usage 0
            ;;
        # Make the script more verbose
        -v|--verbose)
            VERBOSE=$((VERBOSE + 1))
            ;;
        # Make the script quieter
        -q|--quiet)
            VERBOSE=$((VERBOSE - 1))
            ;;
        # All arguments that follow are non-flags
        # This should be in all of your scripts, to more easily support filenames
        # that start with hyphens.  Break will bail from the `for` loop above.
        --)
            break
            ;;
        # Something that looks like a flag, but is not; report an error and die
        -?*)
            usage 1 "Unknown option: '$ARG'" >&2
            ;;
        #
        # All other arguments are added to the ARGS array.
        *)
            ARGS=(${ARGS[@]} "$ARG")
            ;;
    esac
done
# If the above script found a '--' argument, there will still be items in $*;
# move them into ARGS
while [ -n "$1" ]; do
    ARGS=(${ARGS[@]} "$1")
    shift
done

# Main script goes here.

后来...

vexec 1 "Building myapp.c" \
    gcc -c myapp.c -o build/myapp.o ${CFLAGS}

注意:这将不包括管道命令; 你需要在bash -c那些各种各样的东西,或者将它们分开成中间变量或文件。



Answer 5:

可以添加到两个有用的shell选项bash命令行或通过set在脚本或交互式会话命令:

  • -v打印外壳的输入行被读取。
  • -x展开每个简单的命令,之后for命令, case命令, select命令,或算术for命令,显示扩展后的值PS4 ,接着所述命令和它的膨胀的参数或相关联的单词列表。


Answer 6:

对于额外的时间戳和I / O信息,考虑annotate-outputDebian的devscripts软件包的命令:

annotate-output echo hello

输出:

13:19:08 I: Started echo hello
13:19:08 O: hello
13:19:08 I: Finished with exitcode 0

现在找一个不存在 ,并注意E中的文件用于STDERR输出:

annotate-output ls nosuchfile

输出:

13:19:48 I: Started ls nosuchfile
13:19:48 E: ls: cannot access 'nosuchfile': No such file or directory
13:19:48 I: Finished with exitcode 2


Answer 7:

创建可执行文件(+ x)​​的命名与下文提到的简单的shell脚本“echo_and_run”基地脚本!

#!/bin/bash
echo "$*"
$@

$ ./echo_and_run“回声你好,世界”

echo Hello, World
Hello, World

或者干脆用一个衬垫bash函数

echo_and_run() { echo "\$ $*" ; "$@" ; }

甚至这种变异会工作:

function echo_and_run {
  echo "$" "$@"
  eval $(printf '%q ' "$@") < /dev/tty
}

然而,cnicutar的计算策略来set -x是可靠的,强烈推荐。



文章来源: Echo command, and then run it? (Like make)