0

I have a file like this with .sh extension..

clear
echo -n "Enter file name: "
read FILE
gcc -Wall -W "$FILE" && ./a.out
echo

When I can execute this file, it asks for a .c file and when given, it compiles and gives output of the .c file.

For this, everytime I have to first execute this .sh file and then give it the .c file name when asked. Is there anyway, so that, I can just give the .c file in the command line itself, so that it takes that file and does the work...

What I mean is, if I give "./file.sh somecommand cfile.c", then it takes cfile.c as input, compiles it and gives the output...

4

2 回答 2

4

使用“$1”变量:

clear
gcc -Wall -W $1 && ./a.out
echo

$1 表示“命令行的第一个参数”。

或者,如果您想使用脚本一次编译多个文件,您可以使用 $@ 变量,例如:

gcc -Wall -W $@ && ./a.out

您将按如下方式调用您的脚本(假设它被称为“script.sh”):

./script.sh file.c

请参阅下文第3.2.5 节。

如果您的项目变得更大,您可能还需要考虑使用指定用于构建的工具,例如automake

于 2013-03-04T10:01:52.787 回答
0

你也可以让它做任何事情:

if [ -n "$1" ] ; then
    FILE="$1"
else
    echo -n "Enter file name: "
    read FILE
fi
gcc -Wall -W "$FILE" && ./a.out

如果它存在,这将使用命令行参数,否则它会询问文件名。

于 2013-03-04T16:20:48.723 回答