2014-01-17 64 views
1

我写了一个简单的脚本,我写了一个函数来检查返回值并相应地退出。bash error“:= 0:command not found”

#!/bin/bash 

function error_exit { 
    echo "$1" 1>&2 
    exit 1 
} 

function check_return_exit() { 
    $OUT=$1 
    if [[ $OUT != 1 ]]; then 
     error_exit $2 
    fi 
} 

function install_dependencies { 
    apt-get install -y lxc expect 
    EXIT_VAL=$? 
    EXIT_MSG="Failed to install dependencies (lxc, expect)!!" 
    check_return_exit $EXIT_VAL $EXIT_MSG 
} 

install_dependencies 

我启动脚本时出现此错误。有人可以帮我解决这个错误吗?

Reading package lists... Done 
Building dependency tree 
Reading state information... Done 
expect is already the newest version. 
lxc is already the newest version. 
The following package was automatically installed and is no longer required: 
    python-urllib3 
Use 'apt-get autoremove' to remove it. 
0 upgraded, 0 newly installed, 0 to remove and 90 not upgraded. 
./launch_lxc.sh: line 9: =0: command not found 
Failed 
+2

尝试OUT =“$ 1”,实际上,巧妙地引用了大部分美元解除引用。 –

+2

仅供参考 - 不需要'function'关键字 - 只需要'error_exit(){'用于兼容POSIX的函数声明; 'function error_exit'只会破坏POSIX兼容性,但不会给您带来任何好处。 –

+1

...另外,all-caps对于环境变量和内置变量是常规的,而不是shell本地变量。遵循这个约定可以防止命名空间冲突。 –

回答

7
$OUT=$1 
^ 

你可能想砸$

+0

谢谢你解决了我的问题 – Santhosh

相关问题