请注意,exit codes != 0被用于报告错误。所以,最好这样做: retVal=$? #include #include int main() { int i = 0; for (i = 0;... use industry_finance; DELIMITER // drop procedure. How do I iterate over a range of numbers defined by variables in Bash? For some reason reqular "! Nice, but I prefer the Mr. shellter answer because is "self documented", is more "readable" the programmer intention. 本文翻译自:How to represent multiple conditions in a shell if statement? 正则表达式中还有一个较为常用的运算符,即否定符 “[^]”。与我们前文所介绍的定位符 “^” 不同,否定符 “[^]”规定目标对象中不能存在模式中所规定的字符串。例如: /[^a-c]/ 上述字符串将会与目标对象中除a,b,和c之外的任何字符相匹配。 :-) Some people will jump up and down and cry gothca! On Unix systems that supports it (not macOS it seems): This has the advantage that it will query any directory service that may be in use (YP/NIS or LDAP etc.) Bash文件测试-b filename-阻止特殊文件 -c filename特殊字符文件 -d directoryname-检查目录是否存在 -e filename检查文件是否存在,与类型(节点,目录,套接字等)无关 -f filename检查常规文件是否存在而不是目录 -G filename-检查文件是否存在并由有效的组ID拥有 -G filename set-group-id id-如果文件存在 … 4. All Rights Reserved. 请注意, -a和-o运营商的POSIX规范的一部分test ,又名[ ,主要用于向后兼容性(因为他们的部分test在第7版UNIX等),但它们被明确标记为“过时由POSIX。 Bash (see conditional expressions ) seems to preempt the classic and POSIX meanings for -a and -o with its own alternative operators that take arguments. grep returns true if it finds the search target, and false if it doesn't. 我猜想() 。, 参考:https://stackoom.com/question/g3qx/如何在if语句中表示多个条件, Be careful if you have spaces in your string variables and you check for existence. and the local password database file. Please edit your answer to explain what this code does and how it improves on the other upvoted answers this question already has, so that it is useful to other users with similar issues. '和' && '—是他们的建议。 I suppose you could even go so far as: 我想您甚至可以做到:, Where the actions are as trivial as echoing, this isn't bad. i just through i would throw in a properly bounded check on username, else if the OP really does search on "sys" or somesuch, he'll get quite the surprise. However, in this specific case, it does indeed work because sysa will be printed which makes the test statement come true. BUT, you could use the newest shell features (arithmetic evaluation) like. "||"和"&&"在SHELL里可以用吗?也就是第一个写成if [ a>b && ab && a/dev/null then echo "SUBJECT: SOLAR is not responding to ping" | ssmtp abc@def.com echo "SOLAR is not responding to ping" | ssmtp 4151112222@txt.att.com else echo "SOLAR is up" fi # if ! 当要重复执行的动作块为多行时,重复会很痛苦,并且较早的版本之一是可取的-否则您需要将动作包装到在不同的then块中调用的函数中。, I want to represent multiple conditions like this: 我想代表, 参见英文答案 > How to test multiple variables against a ...21个我试图从. 可用, 但是要两个独立的 [ ] , [[ ]] 或 (( ))      看 1, 5 -ne 比较数字 (numberic) ; != 比较字符 (string), 但后者拿来, 者 逻缉操作符 [[ ]] 才可使用, -lt , -eq , -gt , -ge. For a grep solution to be correct, you will need to properly parse the /etc/passwd file: ... or any other similar test against the first of the :-delimited fields. Welcome to Stack Overflow. For example, in my passwd file, there is a line saying. 严格来说,不需要括号,因为-a和-o的优先级使得即使没有它们也正确。, Note that the -a and -o operators are part of the POSIX specification for test , aka [ , mainly for backwards compatibility (since they were a part of test in 7th Edition UNIX, for example), but they are explicitly marked as 'obsolescent' by POSIX. -o = or , -a = and , 但我一向只用 || 或者 && 4. In this case, per a comment by Orwellophile, the arithmetic evaluation can be pared down even further, like, Finally, there is an award called the Useless Use of Cat (UUOC). What did Pete Stewart think he knew about efficient implementation of floating point denormals? @0zkrPM But the shellter version does not work in Bourne shell. 我想知道在if语句中检查退出状态的最佳方法是什么,以便响应特定的输出。 我想的是: if [ $? Align equivalence arrows and equal signs without weird spacing. How to get the source directory of a Bash script from within the script itself? Help identify, Can someone re-license my BSD-3-licensed project under the MIT license, remove my copyright notices, and list me as a "collaborator" without consent, I'm looking for a way to emulate addressable LED strip using LED diodes in a custom arrangement. Stack Overflow for Teams is a private, secure spot for you and Why does Ray Bradbury use "flounder" for an action with a positive outcome? How can I check if a directory exists in a Bash shell script? ;-), $ grep sysa /etc/passwd || { echo "ERROR - The user sysa could not be looked up"; exit 2; }. To learn more, see our tips on writing great answers. Trying to identify an aircraft from a photo. your coworkers to find and share information. Also FYI, note that since cron jobs run as root there is no need to use sudo ping in a cron script. 否定 参数 ! Thanks for contributing an answer to Stack Overflow! I want to represent multiple conditions like this: 我想代表多个条件,例如:, but when I execute the script, it shows 但是当我执行脚本时,它显示. 腾讯云 版权所有 京公网安备 11010802017518 粤B2-20090059-1. 我会这么想的。 However, there is another alternative, namely: 但是,还有另一种选择,即:, Indeed, if you read the 'portable shell' guidelines for the autoconf tool or related packages, this notation — using ' || 确实,如果您阅读了autoconf工具或相关软件包的“便携式外壳”指南,则使用- ||表示。 ' and ' && ' — is what they recommend. 在动作像回声一样琐碎的情况下,这还不错。 When the action block to be repeated is multiple lines, the repetition is too painful and one of the earlier versions is preferable — or you need to wrap the actions into a function that is invoked in the different then blocks. How can I break the cycle of taking on more debt to pay the rates for debt I already have? Does a bronze dragon's wing attack work underwater? I would like to have the echo command executed when cat /etc/passwd | grep "sysa" is not true. 如果字符串变量中有空格并检查是否存在,请格外小心。 Be sure to quote them properly. 这种情况怎么了? I guess something is wrong with the () . Great! Try echo $(cat /etc/passwd | grep "sysa") to see what I mean. 1955: When Marty couldn't use the time circuits anymore was the car still actually driveable? -name "*.txt" -print 不以.txt 结尾的 ... bash黑客都喜欢单行命令 ... 科技小先锋 2017-11-22 02:14:00 浏览1045 I'll just say that grep can take a file name on its cmd-line, so why invoke extra processes and pipe constructions when you don't have to? An alternative would be cat /etc/passwd | grep "sysa" || echo error. Can a druid use Wild Shape in mid-air to survive being dropped? Is this odd device some kind of lightning arrestor or primitive fuse? -eq 1 ] then echo "blah blah blah" fi 我还面临的问题是,Exit语句位于if语句之前,它必须有退出代码,如何解决? site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. 你并不需要引用变量[[如同你[因为它不是一个单独的命令,在同样的方式, [是。, Isn't it a classic question? How can I manage a remote team member who appears to not be working their full hours? Here is an answer by way of example: In order to make sure data loggers are online a cron script runs every 15 minutes that looks like this: #!/bin/bash # if ! Good luck to all. It's all rather silly really, from my answer to a much harder (question)[. https://stackoom.com/question/g3qx/如何在if语句中表示多个条件. 这不是一个经典的问题吗?, I would have thought so. How can I check if a program exists from a Bash script? @acraig5075 it's valid either way, but there's no need for a test command (which is what the brackets are) in this statement at all. You will get. Thanks for your ideas. 一般来说,这是一个好习惯。 Strictly, the parentheses aren't needed because the precedence of -a and -o makes it correct even without them. not picking on your answers, quite enjoyed them. This could happen if there is a partial or exact match somewhere else in the file. How to specify the private SSH-key to use when executing shell command on Git? which also gives you the benefit of using the c-lang based comparison operators, ==,<,>,>=,<=,% and maybe a few others. To subscribe to this RSS feed, copy and paste this URL into your RSS reader. The Overflow #47: How to lead with clarity and empathy in the remote world, Creating new Help Center documents for Review queues: Project overview, Feature Preview: New Review Suspensions Mod UX, have to determine all users home directories - tilde scripting problem. 一定要小心,您可以使用更现代的[[运算符,但是请注意,例如Bash和Korn Shell中的版本不必相同。, Example run, using Bash 3.2.57 on Mac OS X: 在Mac OS X上使用Bash 3.2.57运行示例:, You don't need to quote the variables in [[ as you do with [ because it is not a separate command in the same way that [ is. if ( ‘001’ == FormId ||'002’FormId||‘003’ FormId ){…}, 格式如下,在比较时,数字和字符串用不同的比较符号 1.如果a>b且a b )) && (( a < c ))      或者    if [[ $a > $b ]] && [[ $a < $c ]]     或者          if [ $a -gt $b -a $a -lt $c ] 2.如果a>b或a b )) || (( a < c ))              或者       if [[ $a > $b ]] || [[ $a < $c ]]        或者        if [ $a -gt $b -o $a -lt $c ] 3. Making statements based on opinion; back them up with references or personal experience. [ $(true) ]; then echo false; fi would always print false because the true command does not write anything to stdout (even though the exit code is 0). at http://www.SDsolarBlog.com/montage, FYI, using &>/dev/null redirects all output from the command, including errors, to /dev/null, (The conditional only requires the exit status of the ping command). However, if ! rev 2020.11.13.38000, Stack Overflow works best with JavaScript enabled, Where developers & technologists share private knowledge with coworkers, Programming & related technical career opportunities, Recruit tech talent & build your employer brand, Reach developers & technologists worldwide. grep ...; then. Why is "hand recount" better than "computer rescan"? Is there objective proof that Jo Jorgensen stopped Trump winning, like a right-wing Ralph Nader? 可用, 但是要两个独立的 [ ] , [[ ]] 或 (( ))      看 1 5 -ne 比较数字 (numberic) ; != 比较字符 (string), 但后者拿来    比较数字也可,只是不是标准用法    -lt 是等同 < , 但 < 只能在 shell 的数值操作符 (( )) 或    者 逻缉操作符 [[ ]] 才可使用, -lt , -eq , -gt , -ge    -le , 这些是 test , 就是 [ ] 这个内建命令使用的条件操    作符, 数字用, = , != 字符用, == 这个该是 [[ ]] 用的,    可用来比对正规表示式, 但用在 [ ] 也可,只是不太正统用法. ! grep -qs..." did not work with /proc/mounts and trying to find out if a regularly dropping USB-disk was mounted on Raspbian 4.9 kernel. In order to make sure data loggers are online a cron script runs every 15 minutes that looks like this: ...and so on for each data logger that you can see in the montage By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Use of "eben" – does it mean just, also or even? Bash(请参阅条件表达式 )似乎以其自己的带参数的替代运算符抢占了-a和-o的经典含义和POSIX含义。 With some care, you can use the more modern [[ operator, but be aware that the versions in Bash and Korn Shell (for example) need not be identical.

Âブリミック Ãミノフォース Âャンプー 7, Âア Âフト 97 Ȫ整 žち 17, Zoom Âラーコード 100000502 14, B450 Steel Legend Led Ȩ定 8, Ʋ口 Á魚 ɣべれる 11, Âテンザ Ãッシュボード ŏり外し 7, ȶ快適マスク 4枚入り ľ格 4, Ɗり紙 Áぶと ĸ級 Ɗり方 4, Ãリオカートツアー Áんなで Âり方 5, Ƀ立国立高校 Ň身中学 2019 8,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.