什么" $"给我们正好在shell脚本? [英] What does "$?" give us exactly in a shell script?

查看:112
本文介绍了什么" $"给我们正好在shell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了code写的某个地方,我想知道究竟是什么$? DO /给我们。
谷歌搜索并没有帮助。

I saw the code written somewhere online, and I wanted to know what exactly does "$?" do/give us. Googling did not help.

这里的code我看到它:

Here's the code I saw it in:

#!/bin/sh

ping -c 2 localhost
if [ $? != 0 ] ; then
    echo "Couldn't ping localhost, weird"
    fi

ping -c 2 veryweirdhostname.noend 
if [ $? != 0 ] ; then
    echo "Surprise, Couldn't ping a very weird hostname.."
    fi

echo "The pid of this process is $$"

来自:<一href=\"http://efod.se/writings/linuxbook/html/shell-scripts.html\">http://efod.se/writings/linuxbook/html/shell-scripts.html

推荐答案

$?是一个变量抱着你运行的最后一个命令的返回值。

$? is a variable holding the return value of the last command you ran.

例C程序( example.c

int main() { return 1; }

Bash实例:

Example Bash:

gcc -o example example.c
./example
echo $? # prints 1

这篇关于什么&QUOT; $&QUOT;给我们正好在shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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