- Bash Cookbook
- Ron Brash Ganesh Naik
- 254字
- 2021-07-23 19:17:32
Retrieving return codes and output
Up until now, we have been using a command called exit intermittently to exit scripts. For those of you who are curious, you may have already scoured the web to find out what this command does, but the key concept to remember is that every script, command, or binary exits with a return code. Return codes are numeric and are limited to being between 0-255 because an unsigned 8-bit integer is used. If you use a value of -1, it will return 255.
Okay, so return codes are useful in which ways? Return codes are useful when you want to know whether you found a match when performing a match (for example), and whether the command was completely successfully or there was an error. Let's dig into a real example using the ls command on the console:
$ ls ~/this.file.no.exist
ls: cannot access '/home/rbrash/this.file.no.exist': No such file or directory
$ echo $?
2
$ ls ~/.bashrc
/home/rbrash/.bashrc
$ echo $?
0
Notice the return values? 0 or 2 in this example mean either success (0) or that there are errors (1 and 2). These are obtained by retrieving the $? variable and we could even set it to a variable like this:
$ ls ~/this.file.no.exist
ls: cannot access '/home/rbrash/this.file.no.exist': No such file or directory
$ TEST=$?
$ echo $TEST
2
From this example, we now know what return codes are, and how we can use them to utilize results returned from functions, scripts, and commands.
- Hyper-V 2016 Best Practices
- Node.js 10實戰
- GitLab Cookbook
- Learn to Create WordPress Themes by Building 5 Projects
- Power Up Your PowToon Studio Project
- PHP 7底層設計與源碼實現
- PyQt從入門到精通
- Oracle 12c中文版數據庫管理、應用與開發實踐教程 (清華電腦學堂)
- 技術領導力:程序員如何才能帶團隊
- Java EE 7 Development with NetBeans 8
- Linux操作系統基礎案例教程
- C語言程序設計
- C++面向對象程序設計習題解答與上機指導(第三版)
- iOS開發實戰:從入門到上架App Store(第2版) (移動開發叢書)
- AutoCAD 2009實訓指導