#!/bin/bash
# exitlab
#
# example of exit status
# check for non-existent file
# exit status will be 2
# create file and check it
# exit status will be 0
#
ls xyzzy.345 > /dev/null 2>&1
status='echo $?'
echo "status is $status"
# create the file and check again
# status will not be 0
touch xyzzy.345
ls xyzzy.345 > /dev/null 2>&1
status='echo $?'
echo "status is $status"
#remove the file
rm xyzzy.345
edx.org有一个实验室,这是脚本。当我运行它时,输出如下:
status is echo $?
status is echo $?
我认为输出应该是0或2.我尝试使用status='(echo $?)
之类的括号,但结果为status is echo $?
。然后,我尝试将括号放在单引号status=( 'echo $?' )
之外,但这给了我相同的输出status is echo $?
。
有什么想法吗?
答案 0 :(得分:1)
您正在寻找命令替换(status=$(echo $?)
),尽管这不是必需的。您可以将$?
的值直接指定给status
:
status=$?
答案 1 :(得分:-1)