2013-02-18 74 views
1

嗨我有一个关于喂养输入这个简单的bash脚本我写的问题。它所做的就是在我的编译操作中添加一组标志,以免我每次都必须自己写入它们。我可以使用echo myprogram.c -o myprogram -llibrary | ./Compile运行它。 但我找不到一种方式来运行它,我期望能够,./Compile < myprogram.c -o myprogram -llibrary 我试过一些引号和括号的组合无济于事,谁能告诉我如何提供相同的输入作为使用重定向输入命令由echo生成。重定向输入与空格分隔

#!/bin/bash 
# File name Compile 
#Shortcut to compile with all the required flags, name defaulting to 
#first input ending in .c 
echo "Enter inputs: " 
read inputs 
gcc -Wall -W -pedantic -std=c89 -g -O $inputs 
exit 0 

回答

2

你的shell只是更改为:

#!/bin/bash 
gcc -Wall -W -pedantic -std=c89 -g -O "[email protected]" 

然后你只能写(无需重定向):

./Compile myprogram.c -o myprogram -llibrary 

BTW,没有明确地在这个外壳的结束写exit 0。当gcc成功时它是多余的,并且在gcc失败时退出(退出代码1将被覆盖)。

2

您可以使用process substitution

./Compile < <(echo myprogram.c -o myprogram -llibrary) 

上述行产生相同的结果作为原始命令:

echo myprogram.c -o myprogram -llibrary | ./Compile