shell找到一个文件,执行它 - 退出,如果“错误”,并继续如果“没有错误” [英] shell to find a file , execute it - exit if 'error' and continue if ' no error'

查看:510
本文介绍了shell找到一个文件,执行它 - 退出,如果“错误”,并继续如果“没有错误”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须写一个shell脚本,我不知道如何去做。
基本上我必须写一个脚本,我会找到一个文件(它可能有不同的命名)。如果任何一个文件存在,那么它必须被执行,如果它返回0(没有错误),它应该继续构建,如果它不等于0(返回错误),它应该退出。如果找不到任何文件,它应该继续构建。

我必须找到的文件可以是file.1或file.2,因此它可以被命名为( file.1)或(file.2)。

一些条件使之更加清楚。
1)如果任何一个文件存在,它应该执行 - 如果它有任何错误,它应该退出,如果没有错误,它应该继续。
2)没有可能存在,如果是这种情况,那么它应该继续构建。
3)这两个文件不会同时出现(附加信息)

我试图写一个脚本,但我怀疑它更接近于什么我在寻找。

 
if [-f /home/(file.1)或(file.2)]
然后
-exec /home/(file.1)或(file.2)
if [$! -eq 0];那么
回显没有错误继续生成
fi
else
if [$! -ne 0];然后
退出;
fi
else
echo/home/(file.1)or(file.2)not found,continue build
fi

$ p
$ b $ p


h2_lin>解决方案

  DOIT =
for file1.sh file2.sh;做
如果[-x / home / $ f];那么DOIT =/ home / $ f;打破; fi
完成
如果[-z$ DOIT];然后回显文件未找到,继续建设; fi
if [-n$ DOIT];然后$ DOIT&&回声没有错误||出口1; fi

对于那些对我的语法感到困惑的人,试试这个:

  true&&回声是真的|| echois false
false&&回声是真的|| echois false


I have to write a shell script and i don't know how to go about it. Basically i have to write a script where i'd find a file ( it could be possibly named differently). If either file exists then it must be executed, if it returns a 0 ( no error), it should continue the build, if it's not equal to 0 ( returns with error), it should exit. If either file is not found it should continue the build.

the file i have to find could be either file.1 or file.2 so it could be either named (file.1), or (file.2).

some of the conditions to make it more clear. 1) if either file exists , it should execute - if it has any errors it should exit, if no errors it should continue. 2) none could exist, if that's the case then it should continue the build. 3) both files will not be present at the same time ( additional info)

I have tried to write a script but i doubt it's even closer to what i am looking for.

if [-f /home/(file.1) or (file.2)]
then 
  -exec /home/(file.1) or (file.2)
   if [ $! -eq 0]; then
   echo "no errors continuing build"
   fi
   else
   if [ $! -ne 0] ; then
   exit ;
   fi
else 
   echo "/home/(file.1) or (file.2) not found, continuing build"
fi

any help is much appreciated.

Thanks in advance

解决方案

DOIT=""
for f in file1.sh file2.sh; do
  if [ -x /home/$f ]; then DOIT="/home/$f"; break; fi
done
if [ -z "$DOIT" ]; then echo "Files not found, continuing build"; fi
if [ -n "$DOIT" ]; then $DOIT && echo "No Errors" || exit 1; fi

For those confused about my syntax, try running this:

true && echo "is true" || echo "is false"
false && echo "is true" || echo "is false"

这篇关于shell找到一个文件,执行它 - 退出,如果“错误”,并继续如果“没有错误”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆