技术笔记:Linux命令的返回值

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 技术笔记:Linux命令的返回值

在 Linux 下,不管你是启动一个桌面程序也好,还是在控制台下运行命令,所有的程序在结束时,都会返回一个数字值,这个值叫做返回值,或者称为错误号 ( Error Number )。


在控制台下,有一个特殊的环境变量 $?,保存着前一个程序的返回值,我们可以试试:


$ ls *.png


Diagram1.png eqn.png peazip.png


eqn4.png Mandelbrot.png x2.png


$ echo $?


0


先随便执行个命令,比如像上面的 ls 某些文件,然后通过 echo $?,打印 $? 的值~


我们发现返回值是 0,这是什么意思呢?


只要返回值是 0,就代表程序执行成功了~


也就是说,如果 $? 变量的值不是 0 的话,就表示上一个命令在执行的过程中出错了。我们可以试着 ls 一个不存在的文件:


$ ls linuxgem


ls: 无法访问 linuxgem: 没有那个文件或目录


$ echo $?


2


这样,对于某些打印出一堆不熟悉的英文信息的命令,可以方便的看出它是否成功执行结束。


不过,每次都 echo $? 一下太累了,没关系,你可以把 $? 放在提示符里去:


$ export PS1="【\$?】${PS1}"


【0】test@test:$


export PS1="【\$?】${PS1}" 这个命令把 $? 的值放在的提示符的最前面,这样每次执行完命令,这个值都会自动更新,这下就一目了然了~


那么如果返回的值不是 0,我们要怎么知道是那里出错了呢? 大多数的程序出错都会给出提示,如果没有提示的话,可以用 perror 这个程序来查看错误的信息,比如返回值是 2,我们可以运行:


$ perror 2


OS error code 2: No such file or directory


这样就清楚了,原来是文件不存在~


错误对照表:


"OS error code 1: Operation not permitted"


"OS error code 2: No such file or directory"


"OS error code 3: No such process"


"OS error code 4: Interrupted system call"


"OS error code 5: Input/output error"


"OS error code 6: No such device or address"


"OS error code 7: Argument list too long"


"OS error code 8: Exec format error"


"OS error code 9: Bad file descriptor"


"OS error code 10: No child processes"


"OS error code 11: Resource temporarily unavailable"


"OS error code 12: Cannot allocate memory"


"OS error code 13: Permission denied"


"OS error code 14: Bad address"


"OS error code 15: Block device required"


"OS error code 16: Device or resource busy"


"OS error code 17: File exists"


"OS error code 18: Invalid cross-device link"


"OS error code 19: No such device"


"OS error code 20: Not a directory"


"OS error code 21: Is a directory"


"OS error code 22: Invalid argument"


"OS error code 23: Too many open files in system"


"OS error code 24: Too many open files"


"OS error code 25: Inappropriate ioctl for device"


"OS error code 26: Text file busy"


"OS error code 27: File too large"


"OS error code 28: No space left on device"


"OS error code 29: Illegal seek"


"OS error code 30: Read-only file system"


"OS error code 31: Too many links"


"OS error code 32: Broken pipe"


"OS error code 33: Numerical argument out of domain"


"OS error code 34: Numerical result out of range"


"OS error code 35: Resource deadlock avoided"


"OS error code 36: File name too long"


"OS error code 37: No locks available"


"OS error code 38: Function not implemented"


"OS error code 39: Directory not empty"


"OS error code 40: Too many levels of symbolic links"


"OS error code 42: No message of desired type"


"OS error code 43: Identifier removed"


"OS error code 44: Channel number out of range"


"OS error code 45: Level 2 not synchronized"


"OS error code 46: Level 3 halted"


"OS error code 47: Level 3 reset"


"OS error code 48: Link number out of range"


"OS error code 49: Protocol driver not attached"


"OS error code 50: No CSI structure available"


"OS error code 51: Level 2 halted"


"OS error code 52: Invalid exchange"


"OS error code 53: Invalid request descriptor"


"OS error code 54: Exchange full"


"OS error code 55: No anode"


"OS error code 56: Invalid request code"


"OS error code 57: Invalid slot"


"OS error code 59: Bad font file format"


"OS error code 60: Device not a stream"


"OS error code 61: No data available"


"OS error code 62: Timer expired"


"OS error code 63: Out of streams resources"


"OS error code 64: Machine is not on the network"


"OS error code 65: Package not installed"


"OS error code 66: Object is remote"


"OS error code 67: Link has been severed"


"OS error code 68: Advertise error"


"OS //代码效果参考:http://www.jhylw.com.cn/112226483.html

error code 69: Srmount error"

"OS error code 70: Communication error on send"


"OS error code 71: Protocol error"


"OS error code 72: Multihop attempted"


"OS error code 73: RFS specific error"


"OS error code 74: Bad message"


"OS error code 75: Value too large for defined data type"


"OS error code 76: Name not unique on network"


"OS error code 77: File descriptor in bad state"


"OS error code 78: Remote address changed"


"OS error code 79: Can not access a needed shared library"


"OS error code 80: Accessing a corrupted shared library"


"OS error code 81: .lib section in a.out corrupted"


"OS error code 82: Attempting to link in too many shared libraries"


"OS error code 83: Cannot exec a shared library directly"


"OS error code 84: Invalid or incomplete multibyte or wide character"


"OS error code 85: Interrupted system call should be restarted"


"OS error code 86: Streams pipe error"


"OS error code 87: Too many users"


"OS error code 88: Socket operation on non-socket"


"OS error code 89: Destination address required"


"OS error code 90: Message too long"


"OS error code 91: Protocol wrong type for socket"


"OS error code 92: Protocol not available"


"OS error code 93: Protocol not supported"


"OS error code 94: Socket type not supported"


"OS error code 95: Operation not supported"


"OS error code 96: Protocol family not supported"


"OS error code 97: Address family not supported by protocol"


"OS error code 98: Address already in use"


"OS error code 99: Cannot assign requested address"


"OS error code 100: Network is down"


"OS error code 101: Network is unreachable"


"OS error code 102: Network dropped connection on reset"


"OS error code 103: Software caused connection abort"


"OS error code 104: Connection reset by peer"


"OS error code 105: No buffer space available"


"OS error code 106: Transport endpoint is already connected"


"OS error code 107: Transport endpoint is not connected"


"OS error code 108: Cannot send after transport endpoint shutdown"


"OS error code 109: Too many references: cannot splice"


"OS error code 110: Connection timed out"


"OS error code 111: Connection refused"


"OS error code 112: Host is down"


"OS error code 113: No route to host"


"OS error code 114: Operation already in progress"


"OS error code 115: Operation now in progress"


"OS error code 116: Stale NFS file handle"


"OS error code 117: Structure needs cleaning"


"OS error code 118: Not a XENIX named type file"


"OS error code 119: No XENIX semaphores available"


"OS error code 120: Is a named type file"


"OS error code 121: Remote I/O error"


"OS error code 122: Disk quota exceeded"


"OS error code 123: No medium found"


"OS error code 124: Wrong medium type"


"OS error code 125: Operation canceled"


"OS error code 126: Required key not available"


"OS error code 127: Key has expired"


"OS error code 128: Key has been revoked"


"OS error code 129: Key was rejected by service"


"OS error code 130: Owner died"


"OS error code 131: State not recoverable"


"MySQL error code 132: Old database file"


"MySQL error code 133: No record read before update"


"MySQL error code 134: Record was already deleted (or record file crashed)"


"MySQL error code 135: No more room in record file"


"MySQL error code 136: No more room in index file"


"MySQL error code 137: No more records (read after end of file)"


"MySQL error code 138: Unsupported extension used for table"


"MySQL error code 139: Too big row"


"MySQL error code 140: Wrong create options"


"MySQL error code 141: Duplicate unique key or constraint on write or update"


"MySQL error code 142: Unknown character set used"


"MySQL error code 143: Conflicting table definitions in sub-tables of MERGE table"


"MySQL error code 144: Table is crashed and last repair failed"


"MySQL error code 145: Table was marked as crashed and should be repaired"


"MySQL error code 146: Lock timed out; Retry transaction"


"MySQL error code 147: Lock table is full; Restart program with a larger locktable"


"MySQL error code 148: Updates are not allowed under a read only transactions"


"MySQL error code 149: Lock deadlock; Retry transaction"


"MySQL error code 150: Foreign key constraint is incorrectly formed"


"MySQL error code 151: Cannot add a child row"


"MySQL error code 152: Cannot delete a parent row"

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
29天前
|
Linux Shell
Linux 10 个“who”命令示例
Linux 10 个“who”命令示例
53 14
Linux 10 个“who”命令示例
|
9天前
|
Linux
linux查看目录下的文件夹命令,find查找某个目录,但是不包括这个目录本身?
通过本文的介绍,您应该对如何在 Linux 系统中查看目录下的文件夹以及使用 `find` 命令查找特定目录内容并排除该目录本身有了清晰的理解。掌握这些命令和技巧,可以大大提高日常文件管理和查找操作的效率。 在实际应用中,灵活使用这些命令和参数,可以帮助您快速定位和管理文件和目录,满足各种复杂的文件系统操作需求。
32 8
|
18天前
|
Ubuntu Linux
Linux 各发行版安装 ping 命令指南
如何在不同 Linux 发行版(Ubuntu/Debian、CentOS/RHEL/Fedora、Arch Linux、openSUSE、Alpine Linux)上安装 `ping` 命令,详细列出各发行版的安装步骤和验证方法,帮助系统管理员和网络工程师快速排查网络问题。
104 20
|
11天前
|
安全 Linux KVM
Linux虚拟化技术:从Xen到KVM
Xen和KVM是Linux平台上两种主要的虚拟化技术,各有优缺点和适用场景。通过对比两者的架构、性能、安全性、管理复杂性和硬件依赖性,可以更好地理解它们的适用场景和选择依据。无论是高性能计算、企业虚拟化还是云计算平台,合理选择和配置虚拟化技术是实现高效、稳定和安全IT环境的关键。
60 8
|
19天前
|
网络协议 Linux 应用服务中间件
kali的常用命令汇总Linux
kali的常用命令汇总linux
47 7
|
2月前
|
监控 网络协议 Linux
Linux netstat 命令详解
Linux netstat 命令详解
|
6月前
|
Linux
Linux部署04-ls命令的参数和选项,主体,参数,选项,ls / 查看根目录下的文件夹,-a的意思是列出全部选项 ls -a home全部文件,.代表着隐藏的文件夹,-l 选项,以列表竖向的形式展
Linux部署04-ls命令的参数和选项,主体,参数,选项,ls / 查看根目录下的文件夹,-a的意思是列出全部选项 ls -a home全部文件,.代表着隐藏的文件夹,-l 选项,以列表竖向的形式展
|
6月前
|
Linux
Linux部署 cd-pwd命令,cd 不写参数 就直接回到用户的HOME目录,pwd 查看当前的工作目录,pwd是常看当前目录的路径,无参数
Linux部署 cd-pwd命令,cd 不写参数 就直接回到用户的HOME目录,pwd 查看当前的工作目录,pwd是常看当前目录的路径,无参数
|
6月前
|
Linux
Linux02---命令基础 Linux命令基础, ls命令入门,ls命令参数和选项,命令行是一种以纯字符操作系统的方式,command命令本身,options命令的细节行为,parameter命令的
Linux02---命令基础 Linux命令基础, ls命令入门,ls命令参数和选项,命令行是一种以纯字符操作系统的方式,command命令本身,options命令的细节行为,parameter命令的
|
8月前
|
安全 网络协议 Linux
【专栏】Linux系统中ping命令的使用,包括其基本语法、输出信息、常用参数及高级用法
【4月更文挑战第28天】本文详细介绍了Linux系统中ping命令的使用,包括其基本语法、输出信息、常用参数及高级用法。通过ping,用户可测试网络连通性、诊断故障及评估性能。此外,文章还讨论了ping在不同协议、模拟网络环境及与其他命令结合使用时的场景。注意防火墙和网络环境可能影响ping结果,理解错误信息有助于网络问题排查。熟练掌握ping命令,能助你成为Linux网络专家。不断学习和实践,提升网络技能,为构建稳定网络环境贡献力量。
587 0