Bash Script Error Catching

前端 未结 3 1699
庸人自扰
庸人自扰 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:57

    Although you can test $?, you can also test the exit status of a command directly:

    if cp -rv /path/to/lab$1 /path/to/dropbox
    then echo Submission successful
    fi
    exit $?
    

    The errors were already reported to standard error by cp. The exit shown will exit with status 0 (success) if cp was successful; otherwise, it exits with the status that cp exited with.

    Clearly, you can wrap that in a loop if you want to, or you can make it non-interactive (but any exit terminates the script).

提交回复
热议问题