& Amp; and | in shell, if the execution result of the previous command in linux is correct, a $? 0. So how can we determine whether to execute subsequent commands through this return value? This requires the help of & amp; and |! [Html] Example 1: use ls to check the directory... shell & | if the execution result of the previous command in linux is correct, a $? Value = 0. So how can we determine whether to execute subsequent commands through this return value? This requires the help of & |! [Html] Example 1: Use ls to check whether the/tmp/abc directory exists. If yes, use touch to create/tmp/abc/hehe [root @ www ~]. # Ls/tmp/abc & touch/tmp/abc/hehe ls:/tmp/abc: No such file or directory # ls indicates that this directory cannot be found,? Yueda touch? Error, indicating that touch has not executed [root @ www ~] # Mkdir/tmp/abc [root @ www ~] # Ls/tmp/abc & touch/tmp/abc/hehe [root @ www ~] # Ll/tmp/abc-rw-r -- 1 root 0 Feb 7 hehe saw it? If/tmp/abc does not exist, touch will not be executed. if/tmp/abc exists, touch will start to execute ??? /Div> [html] Example 2: Test whether/tmp/abc exists. if not, create it. if not, [root @ www ~] # Rm-r/tmp/abc <= [root @ www ~] # Ls/tmp/abc | mkdir/tmp/abc ls:/tmp/abc: No such file or directory <= really does not exist! [Root @ www ~] # Ll/tmp/abc total 0 <== the result is displayed! If I want to create the/tmp/abc/hehe file, but I don't know whether/tmp/abc exists, how can I create mkdir? [html] Example 3: I don't know whether/tmp/abc exists, but I want to create the/tmp/abc/hehe file [root @ www ~] # Ls/tmp/abc | mkdir/tmp/abc & touch/tmp/abc/hehe example three results are analyzed: (1) if/tmp/abc does not exist, return $? 0, then (2) because | $? Therefore, mkdir/tmp/abc is started. because mkdir/tmp/abc is successfully executed, $? is returned? = 0 (3) Because & Encounters $? If it is set to 0, touch (2) will be executed. if/tmp/abc exists, it will be returned. $? = 0, then (2) because | 0? $? Not executed. $? = 0 continue to pass back, so (3) as & Encounters $? If it is set to 0,/tmp/abc/hehe is created! Finally,/tmp/abc/hehe is created.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.