c - 从由同一脚本编译的 C 程序中退出 shell 脚本

标签 c shell conditional-statements exit

我编写了以下 shell 脚本来编译和执行 C 程序,然后执行一些其他操作。

#!/bin/sh
#
#FIRST SCRIPT
#
clear
echo "-----STARTING COMPILATION-----"
#echo $1 
name=$1
#echo $name
find . -iname $name -maxdepth 1 -exec cp {} $name \;
new_file="tempwithfile.adb"
cp $name $new_file
cp $name1 $name
echo "compiling"
dir >filelist.txt
gcc writefile.c
run_file="run_file.txt"
echo $name > $run_file
./a.out
echo ""
echo "cleaning"
echo ""
make clean
make -f makefile

如果c程序中满足特定条件,我可以停止shell脚本的执行吗?例如,如果我正在从程序中搜索文件但没有找到它,那么我不必执行 shell 脚本的其余部分。是否可以?

最佳答案

正如 Manül 所说,您需要从 C 代码中的 main() 函数返回一个值。

然后您可以在脚本中执行此操作:

./a.out
if [ $? -ne 0 ]
...

关于c - 从由同一脚本编译的 C 程序中退出 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24078980/

相关文章:

linux - 如何退出bash

python - 如何在一行中计算数据框中的并发事件?

plot - Gnuplot:使用线或线点仅绘制数据集的特定值

Python 井字游戏

linux - OS X 的 bash 脚本不能使用 --color=auto

c - 正常 block 后检测到堆损坏(#63)

c - 选择最大数并对其进行平方或立方时结果不正确

c - printf 怎么会打印一个非空终止字符串?

linux - 使用 sed 提取子表达式

c - 是否可以在编译时检测 C 中的范围错误