简体   繁体   English

如果使用rm Shell linux

[英]If else with rm Shell linux

I need help whith my problem. 我需要我的帮助。 I need make a shell script... 我需要制作一个shell脚本...

this it's my idea 这是我的主意

if [ rm -r -f /directorie ]; then    
  code     
else    
  code    
fi
...

how I can? 我怎么能?

This code may achieve what you want: 此代码可以实现您想要的:

#!/bin/bash

rm -r /directorie > /dev/null 2>&1; rc="$?" 
if [ "$rc" -eq "0" ]; then
   # code here
else
   # code here
fi

Note: rm -rf /directorie (with the option f ) would always return a return code = 0 (true), thus it is not suited for an if test. 注意: rm -rf /directorie (带有f选项)将始终返回返回码= 0(true),因此不适合if测试。

Edit : this answer can be condensed into a bash one-liner: 编辑 :这个答案可以浓缩成bash一线:

rm -r /home/owner/scripts > /dev/null 2>&1 && code here (true) || code here (false)
dir=/directory
test -d "${dir}" && rm -rf "${dir}" && ! test -d "${dir}" && echo OK || echo NOK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM