在Linux脚本中,你可以使用exit
命令来退出脚本
#!/bin/bash echo "This is a sample script." # Check if a condition is true if [ $condition -eq 1 ]; then echo "Condition is true. Exiting the script." exit 1 # Exit with a general status code of 1 else echo "Condition is false. Continuing with the script." fi echo "This line will not be executed if the script exits early."
在这个示例中,我们首先检查一个条件(这里是一个示例条件,你可以根据需要替换为实际条件)。如果条件为真,我们使用exit 1
命令退出脚本,其中1是一个通用的退出状态码,表示有错误发生。如果条件为假,脚本将继续执行后续的代码。