什么是美元?(美元问号)shell 脚本中的变量? [英] What is the $? (dollar question mark) variable in shell scripting?

查看:70
本文介绍了什么是美元?(美元问号)shell 脚本中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习 shell 脚本,我需要了解其他人的代码.$? 变量是什么?我无法在 Google 上搜索答案,因为它们会屏蔽标点符号.

I'm trying to learn shell scripting, and I need to understand someone else's code. What is the $? variable hold? I can't Google search the answer because they block punctuation characters.

推荐答案

$? 用于查找上次执行命令的返回值.在 shell 中尝试以下操作:

$? is used to find the return value of the last executed command. Try the following in the shell:

ls somefile
echo $?

如果somefile存在(不管是文件还是目录),都会得到ls命令抛出的返回值,应该是0(默认成功"返回值).如果它不存在,您应该得到一个非 0 的数字.确切的数字取决于程序.

If somefile exists (regardless whether it is a file or directory), you will get the return value thrown by the ls command, which should be 0 (default "success" return value). If it doesn't exist, you should get a number other then 0. The exact number depends on the program.

对于许多程序,您可以在相应的手册页中找到编号及其含义.这些通常会被描述为退出状态",并且可能有自己的部分.

For many programs you can find the numbers and their meaning in the corresponding man page. These will usually be described as "exit status" and may have their own section.

这篇关于什么是美元?(美元问号)shell 脚本中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆