【发布时间】:2019-05-15 18:07:45
【问题描述】:
我正在开发一个简单的基于终端的应用程序,它有助于在一个命令中编译您的 C/C++ 和 Python 源文件。但是当我执行像'erun test.py'这样的函数时,它总是给出输出:ERun: file unknown file extension。
我认为 if 语句的问题。我尝试编辑这些语句,但没有任何改变。这是我的源代码:
#/bin/bash
# function ERun for C/C++ and python
# version 1.0
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
function erun {
if [ -z "$1" ]; then
#display usage if no paramters given
echo "Usage: erun file.c/.cpp/.py"
echo "Run: ./file"
else
for n in "$@"
do
if [ -f "$n" ] ; then
case "$n{n%,}" in
*.py)
chmod +x "$n" ;;
*.c|*.cpp)
gcc "$n" -o "$n" ;;
*)
echo "ERun: '$n' unknown file extension"
return 1
;;
esac
else
echo "'$n' - file does not exist."
return 1
fi
done
fi
}
IFS=$SAVEIFS
我的预期输出是获得一个可执行文件。如果你能帮助我,我会很高兴。顺便说一句,如果你想贡献我的这个小项目,这里是项目链接:https://github.com/lvntky/ERun/ :)
【问题讨论】:
-
修复你的shebang。
-
故障排除的第一步是在错误的脚本上运行ShellCheck。在这种情况下,它会自动标记坏的 shebang 和 case 模式永远不会匹配的事实
标签: linux bash macos shell if-statement