Linux Shell-在IF-else中运行一组命令
我想准备一个shell脚本,它将执行我的所有命令如果我的所有命令都成功了,那么它将打印“成功”,任何一个命令失败,然后打印“失败” 我的shell脚本命令:Linux Shell-在IF-else中运行一组命令,linux,shell,if-statement,Linux,Shell,If Statement,我想准备一个shell脚本,它将执行我的所有命令如果我的所有命令都成功了,那么它将打印“成功”,任何一个命令失败,然后打印“失败” 我的shell脚本命令: cd /home/lin mkdir logs cp /tmp/filelog.log logs/ rm /tmp/log* touch /tmp/log cd /home/lin && mkdir -p logs && test -f /tmp/filelog.log && cp
cd /home/lin
mkdir logs
cp /tmp/filelog.log logs/
rm /tmp/log*
touch /tmp/log
cd /home/lin &&
mkdir -p logs &&
test -f /tmp/filelog.log &&
cp /tmp/filelog.log logs/ &&
rm -f /tmp/log* &&
touch /tmp/log || stop
保存此文件test.sh
我的问题是
在执行此命令时,如果我的任何一个命令失败,则应停止执行并打印“失败”
否则打印“成功”正确脚本的示例
#!/bin/sh
die() { echo >&2 "$0 Err: $@" ; exit 1 ;}
cd /home/lin || die "Can't change to '/home/lin' dir"
mkdir logs || die "Can't create '$PWD/logs' dir"
cp /tmp/filelog.log logs/ || die "Can't copy 'filelog.log' to '$PWD/logs'"
rm /tmp/log* || die "Can't remove '/tmp/log*'"
touch /tmp/log || die "Can't touch /tmp/log"
echo SUCCESS: All done!
适当脚本的示例
#!/bin/sh
die() { echo >&2 "$0 Err: $@" ; exit 1 ;}
cd /home/lin || die "Can't change to '/home/lin' dir"
mkdir logs || die "Can't create '$PWD/logs' dir"
cp /tmp/filelog.log logs/ || die "Can't copy 'filelog.log' to '$PWD/logs'"
rm /tmp/log* || die "Can't remove '/tmp/log*'"
touch /tmp/log || die "Can't touch /tmp/log"
echo SUCCESS: All done!
因为每个命令都依赖于它的前一个命令,所以这是
set-e
的完美用例。在子shell中执行所有工作,只需检查子shell的结果
set-e
将在遇到第一个错误时退出当前shell。
(即,当返回非零退出状态时。)
因为每个命令都依赖于它的前一个命令,所以这是
set-e
的完美用例。在子shell中执行所有工作,只需检查子shell的结果
set-e
将在遇到第一个错误时退出当前shell。
(即,当返回非零退出状态时。)
制作一个将打印可选参数的函数
stop()
{
if [ $# -gt 0 ]; then
echo "Failed: $@"
else
echo "Failed."
fi
exit 1
}
当您不想编写太多代码时,可以使用不带参数的函数
cd /home/lin || stop
mkdir logs || stop
cp /tmp/filelog.log logs/ || stop
rm /tmp/log* || stop
touch /tmp/log || stop
echo Success
你可以投入更多的精力。第一个命令显示如何获取stderr并在输出中使用它
errmsg=$(cd /home/lin 2>&1) || stop ${errmsg}
# You do not want an error when the dir already exists
mkdir -p logs || stop
# You can test in front
test -f /tmp/filelog.log || stop File filelog.log not found
cp /tmp/filelog.log logs/ || stop
rm -f /tmp/log* || stop
touch /tmp/log || stop
echo Success
其他可能性是使用set-e
(故障后将退出,但不会出现“故障”消息),这在@Kusalananda和@HenkLangeveld的答案中显示。或者制作一系列命令:
cd /home/lin
mkdir logs
cp /tmp/filelog.log logs/
rm /tmp/log*
touch /tmp/log
cd /home/lin &&
mkdir -p logs &&
test -f /tmp/filelog.log &&
cp /tmp/filelog.log logs/ &&
rm -f /tmp/log* &&
touch /tmp/log || stop
制作一个将打印可选参数的函数
stop()
{
if [ $# -gt 0 ]; then
echo "Failed: $@"
else
echo "Failed."
fi
exit 1
}
当您不想编写太多代码时,可以使用不带参数的函数
cd /home/lin || stop
mkdir logs || stop
cp /tmp/filelog.log logs/ || stop
rm /tmp/log* || stop
touch /tmp/log || stop
echo Success
你可以投入更多的精力。第一个命令显示如何获取stderr并在输出中使用它
errmsg=$(cd /home/lin 2>&1) || stop ${errmsg}
# You do not want an error when the dir already exists
mkdir -p logs || stop
# You can test in front
test -f /tmp/filelog.log || stop File filelog.log not found
cp /tmp/filelog.log logs/ || stop
rm -f /tmp/log* || stop
touch /tmp/log || stop
echo Success
其他可能性是使用set-e
(故障后将退出,但不会出现“故障”消息),这在@Kusalananda和@HenkLangeveld的答案中显示。或者制作一系列命令:
cd /home/lin
mkdir logs
cp /tmp/filelog.log logs/
rm /tmp/log*
touch /tmp/log
cd /home/lin &&
mkdir -p logs &&
test -f /tmp/filelog.log &&
cp /tmp/filelog.log logs/ &&
rm -f /tmp/log* &&
touch /tmp/log || stop
bash
(或ksh
)的解决方案:
当-e
(errexit
)shell选项由于返回非零退出状态的命令而导致shell退出时,将执行ERR
陷阱
在mkdir test/test
失败的目录中测试此脚本:
bash-4.4$ bash script.sh
mkdir: test/test: No such file or directory
FAILED
在mkdir test/test
成功的目录中测试此脚本:
bash-4.4$ bash script.sh
SUCCESS
bash
(或ksh
)的解决方案:
当-e
(errexit
)shell选项由于返回非零退出状态的命令而导致shell退出时,将执行ERR
陷阱
在mkdir test/test
失败的目录中测试此脚本:
bash-4.4$ bash script.sh
mkdir: test/test: No such file or directory
FAILED
在mkdir test/test
成功的目录中测试此脚本:
bash-4.4$ bash script.sh
SUCCESS
我测试了它,但它对我不起作用。而且(不是要求)子shell中的变量集将丢失。我测试了它,但它对我不起作用。并且(不是要求)子shell中的变量集将丢失。