1

我有一个小的 bash 脚本build1c.sh

if [ "$1" = "" ]; then
    echo "You must give a .c file to compile."
    exit 1
fi

cfile=$1
stem=${cfile%.*}

set -o verbose

gcc -c -g -Wall $cfile
gcc -o $stem $stem.o common.o reentrant.o -lssl -lcrypto

set +o verbose # optional here

目的是仅回显正在执行的 gcc 命令。我在某种程度上工作。当我打电话时build1c.sh client2.c,我看到了输出

gcc -c -g -Wall $cfile
gcc -o $stem $stem.o common.o reentrant.o -lssl -lcrypto

set +o verbose # optional here

还是很古怪,对吧?那些 var reference( $cfile, $stem) 没有得到它们的最终形式,所以回显变得非常无用。

你知道,我喜欢看的是

gcc -c -g -Wall client2.c
gcc -o client2 client2.o common.o reentrant.o -lssl -lcrypto

有没有正确而简洁的方法来解决这个问题?

顺便说一句:小请求:我可以抑制set +o verbose自身的回声吗?

4

2 回答 2

2

替换set -o verboseset -x

于 2012-05-05T11:53:49.920 回答
2
function echo_and_execute {
    echo "$@"
    "$@"
}

echo_and_execute gcc -c -g -Wall $cfile
echo_and_execute gcc -o $stem $stem.o common.o reentrant.o -lssl -lcrypto
于 2012-05-05T12:42:21.920 回答