当发生错误时,我使用exit 1来停止shell脚本的执行。
Shell脚本
test() {
mod=$(($1 % 10))
if [ "$mod" = "0" ]
then
echo "$i";
exit 1;
fi
}
for i in `seq 100`
do
val=`test "$i"`
echo "$val"
done
echo "It's still running"
为什么不起作
我找到了这个,它解释了如何将bash脚本的输出重定向到syslog。这正是我所需要的,但有一个问题。
#!/bin/bash
# Don't ignore any error and return when first error occurs.
exec 1> >(logger -s -t $(basename $0)) 2>&1
set -e
# a list of command(s) that can fail:
chown -R user1:user1 /tmp/myappData/*
chown -R user1:user1 /tmp/m
我见过在bash脚本中使用"exit“和"exec”来在发生错误时停止脚本执行。例如:
if [ ! -f file ]; then
echo "no file"
exit 1
fi
和:
if [ ! -f file ]; then
exec echo "no file"
fi
这里最好的实践是什么?为什么?也欢迎更广泛地讨论/解释"exec“和"exit”:)
我正在尝试在shell中实现这样的逻辑:
command 1 #a command will run for a long time, but sometimes it could be interrupted by error.
if [[ $? != 0 ]] #if command 1 failed with return value other than 0
then
clear command #trigger clear command
command 1 #and re-run command 1
fi
但是这个块只能检测到command 1的1轮错误,
我正在尝试执行一个简单的命令(最好使用set -e),以便在发生错误时停止我的脚本。因为它只使用set -e,所以它是有效的。但是你会遇到通用的"blah blah nto found or directory nto“错误信息。
有没有一种简单的方法可以让它至少输出脚本死掉的$LINENO行?任何其他的建议也是欢迎和欢迎的!谢谢大家!:)
我使用"set -e“使脚本在发生错误时退出,但我不希望它在函数内部出现错误时退出,而是希望函数返回错误
例如:
#!/bin/bash
set -e
func() {
echo 1
# code ...
cause_error
echo This should not print
}
func
if [ $? -ne 0 ]; then
echo I want this print
else
echo This should not print either
fi
此脚本的输出为:
$ /tmp/test.sh
1
但我希望它是:
1
我一直在寻找一种在函数内发生不可恢复的错误时终止PowerShell (PS1)脚本的方法。例如:
function foo() {
# Do stuff that causes an error
$host.Exit()
}
当然不存在$host.Exit()这样的东西。有$host.SetShouldExit(),但这实际上关闭了控制台窗口,这不是我想要的。我需要的是一个等同于Python的sys.exit()的东西,它可以简单地停止当前脚本的执行,而不会有更多的损失。
编辑:是的,就是exit。是啊。
我有以下简单的shell脚本:
# get all servers from config
sshservers=`node $scriptDir/readconfig.js --command cluster.sshservers`
#for each server
for server in sshservers
do
#run deployserver with server (server:port format) argument
if ! sh deployserver --server $server
then
exit 1 #fa
Jenkins构建管道作业有间歇性SSL错误,使用进行构建。原因是conan build文件夹没有刷新缓存的SSL。
如何检查管道脚本中的SSL错误,以便在出现SSL错误时删除文件夹,以便conan在重新尝试构建时从配置文件中获得新的证书。
更新
jenkins控制台日志上的错误如下:
ERROR: HTTPSConnectionPool(host='conan.bintray.com', port=443): Max retries exceeded with url: /v1/ping (Caused by SSLError(SSLError(1, '[SSL: C
根据的说法,使用set -e内置应该足以让bash脚本在第一个错误时退出。然而,下面的脚本:
#!/usr/bin/env bash
set -e
echo "a"
echo "b"
echo "about to fail" && /bin/false && echo "foo"
echo "c"
echo "d"
指纹:
$ ./foo.sh
a
b
about to fail
c
d
删除echo "foo" 会停止脚本,但为什么呢?
这是我的问题,我有一个脚本,它有很多步骤,基本上它打开一个文件,读取它,然后它将它写回文件中。当脚本完成时,一切都很好。当出现排序异常或脚本被中断时,就会出现问题。我以‘r+’模式打开文件,因为如果我以‘w’模式打开它,文件立即变为空白,如果脚本被中断,它将保持空白,而我希望它保留前一个值。下面是一个示例,但不是我正在运行的确切脚本--如果脚本被中断(或出现异常,即使被处理),test.txt中的值将是“myVar=13e”或“myVar=13ne”。不总是,但经常。为什么会发生这种情况,如何避免呢?
import time
from test import myVar
file_path =
我有一个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
如何将函数作为“测试命令”运行,并在失败时执行操作,同时在发生错误时仍然中止该函数?考虑下面的脚本
#!/bin/bash -e
function foo() {
echo Entering foo
false
echo Should not reach this line
}
foo || echo I want to see this line on failure in foo
foo
我得到的输出是
Entering foo
Should not reach this line
Entering foo
而我想要
Entering foo
I want to se