Bash Script Error Catching

前端 未结 3 1698
庸人自扰
庸人自扰 2021-02-05 16:28

I am very new to bash scripts, and for my first script attempt I am submitting files to my professor\'s dropbox within the same server.

The code is this:



        
3条回答
  •  抹茶落季
    2021-02-05 16:58

    To check the return code from the previous command, you can use the $? special variable as follows:

    if [ "$?" -ne "0" ]; then
      echo "Submission failed"
      exit 1
    fi
    
    echo "Submission successful."
    

    Additionally, if you want a clean prompt, you can redired stderr as explained here.

    Finally, you can also use this in the script that uses scp, since it works regardless of the command you're using.

提交回复
热议问题