嗨我有一个关于为我写的这个简单的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
答案 0 :(得分:2)
您可以使用process substitution:
./Compile < <( echo myprogram.c -o myprogram -llibrary )
上面的行产生与原始命令相同的结果:
echo myprogram.c -o myprogram -llibrary | ./Compile
答案 1 :(得分:2)
只需将您的shell更改为:
#!/bin/bash
gcc -Wall -W -pedantic -std=c89 -g -O "$@"
然后你只能写(不需要重定向):
./Compile myprogram.c -o myprogram -llibrary
顺便说一句,不要在这个shell的末尾显式写exit 0
。 gcc
成功时是多余的,当gcc失败时出错(退出代码1将被覆盖)。