Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
退出用于expect脚本上循环的bash脚本expect错误_Bash_Expect - Fatal编程技术网

退出用于expect脚本上循环的bash脚本expect错误

退出用于expect脚本上循环的bash脚本expect错误,bash,expect,Bash,Expect,我有一个bash脚本,它在for循环中调用一个expect脚本。此循环在bash脚本中创建用户 预期脚本: # Define variables for arguments passed into the script set user [lindex $argv 0] set role [lindex $argv 1] set email [lindex $argv 2] set passwd [lindex $argv 3] # Run the CLI command for users

我有一个bash脚本,它在for循环中调用一个expect脚本。此循环在bash脚本中创建用户

预期脚本:

# Define variables for arguments passed into the script
set user [lindex $argv 0]
set role [lindex $argv 1]
set email [lindex $argv 2]
set passwd [lindex $argv 3]

# Run the CLI command for users and expect the required output
spawn cli users add -username $user -role $role -email $email
expect "*assword:"
send "$passwd\r"
expect "*assword:"
send "$passwd\r"
expect {
    default { send_user "\nERROR:  $user was NOT created successfully.  
Exiting script.\n"; exit 1 }
    "*added to the system successfully*"
}
interact
循环的BASH脚本:

for role in $user_roles
    do
            expect_scripts/users.exp $role"1" $role $user_email $password
    done
现在,我想做的是,如果用户不是在expect脚本中创建的,则退出expect脚本并出现错误,然后在FOR循环中失败。我希望FOR循环完全退出


我不知道怎么做,因为我的expect脚本似乎失败了,出现了所需的错误,但FOR循环仍在继续。任何帮助都将不胜感激。

如果bash for循环的一部分返回非零,则该循环不会失败。您必须显式地测试它,并处理它。例如:

for role in $user_roles
    do
            expect_scripts/users.exp $role"1" $role $user_email $password
            if [ $? -ne 0 ]; then 
                 exit 1; 
            fi
    done
当然,您可以将其缩短为一行,如下所示:

for role in $user_roles
    do
            expect_scripts/users.exp $role"1" $role $user_email $password || exit 1
    done

另外,如果不想退出脚本,可以将
exit 1
替换为
break
,这将导致for循环终止,但不会退出脚本。

在顶部,在expect脚本中使用
set-e
,不要建议set-e而不发出警告。