首页 > 其他分享 >bash:"IF command"命令作为条件

bash:"IF command"命令作为条件

时间:2023-01-29 00:11:23浏览次数:33  
标签:shell rocky 命令 command ls root bash

bash:"IF command"命令作为条件

 

 

 

 

一、说明

 

  1、IF的条件部分,可以使用linux“命令”。

1 if command
2 then
3     command if $?=0
4 else
5     command if $?!=0
6 fi

 

 

 

 

二、代码

 

 1 [root@rocky shell]# cat  if_command 
 2 #!/usr/bin/env bash
 3 
 4 
 5 # file_name = if_command
 6 
 7 
 8 run()
 9 {
10         if $@
11         then
12                 echo "os_running: ${@}"
13         fi
14 }
15 
16 
17 list()
18 {
19         if  ls -lh      # command as condition of if
20         then
21                 echo
22         fi
23 }
24 
25 
26 
27 
28 run $@
29 
30 list
31 
32 
33 [root@rocky shell]# 
34 [root@rocky shell]# 

 

 

 

 

 

 

三、运行

 

 

 1 [root@rocky shell]# cat  if_command 
 2 #!/usr/bin/env bash
 3 
 4 
 5 # file_name = if_command
 6 
 7 
 8 run()
 9 {
10         if $@
11         then
12                 echo "os_running: ${@}"
13         fi
14 }
15 
16 
17 list()
18 {
19         if  ls -lh      # command as condition of if
20         then
21                 echo
22         fi
23 }
24 
25 
26 
27 
28 run $@
29 
30 list
31 
32 
33 [root@rocky shell]# 
34 [root@rocky shell]# 
35 [root@rocky shell]# ls
36 if_command
37 [root@rocky shell]# 
38 [root@rocky shell]# 
39 [root@rocky shell]# ./if_command "mkdir big"
40 os_running: mkdir big
41 total 4.0K
42 drwxr-xr-x 2 root root   6 Jan 29 00:01 big
43 -rwx--x--x 1 root root 192 Jan 28 23:59 if_command
44 
45 [root@rocky shell]# 
46 [root@rocky shell]# 
47 [root@rocky shell]# ls
48 big  if_command
49 [root@rocky shell]# 
50 [root@rocky shell]# 
51 [root@rocky shell]# rm -rf big
52 [root@rocky shell]# 
53 [root@rocky shell]# 
54 [root@rocky shell]# ls
55 if_command
56 [root@rocky shell]# 
57 [root@rocky shell]# 

 

 

 

 

 

 

四、参考资料

 

  1、  Bash - Conditional structures if and case:  https://docs.rockylinux.org/books/learning_bash/06-conditional-structures/

 

标签:shell,rocky,命令,command,ls,root,bash
From: https://www.cnblogs.com/lnlidawei/p/17071564.html

相关文章

  • linux基础命令
    linux基础命令前期必备知识1.命令提示符[root@jasonNB~]#root表示⽤户名信息@为分隔符jasonNB为主机名称~表示所处⽬录位置......
  • Anaconda和pip常用命令汇总:简单,通俗易懂
    前言在学习anaconda的常用命令之前要先安装好AnacondaforWindows,AnacondaforUbuntu,这里我总结了Windows环境下和Ubuntu环境下的常用命令,包含了Anaconda本身......
  • flutter 常用命令
    相关项目操作查看Flutter版本查看当前版本flutter--version查看所有版本flutterversion打印所有命令行用法信息 flutter-h或者flutter......
  • linux用户组管理命令详细介绍
    学习目标1、了解用户和用户组的概念2、可以添加useradd和删除userdel用户,修改用户密码passwd3、可以添加groupadd和删除groupdel用户组4、了解管道符|的作用5、会使用ifcon......
  • 常用命令(cmd和dos)
    CMD:运行win+rshift+右键打开命令行窗口文件夹地址栏中输入cmd管理员方式打开常用的dos命令:#盘符切换:d:e:...#查看当前目录下所有文件:dir#切换目录:cd......
  • git常用命令
    1.开始新项目步骤如下:1.克隆远程仓库到本地仓库(首先建远程仓库,再克隆到本地仓库。注意这个只需要cd到你要的文件夹下,执行下面的命令就直接创建了本地仓库并关联了远程......
  • unknow or unsupported command install
    错误原因:今天使用pip下载labelimg时,出现了“unknoworunsupportedcommandinstall”的错误,这是由于电脑有多个pip文件路径所导致。如图,在cmd命令行中输入“wherepip”命......
  • vnc登录centos虚拟机 命令行显示sh-4.2#处理
    一、需求centos操作系统安装软件有些需要图形化界面,比如安装oracle数据库。二、问题解决执行source/root/.bashrc~/.bashrc:该文件包含专用于你的bashshell的bash信......
  • linux 导出日志 命令
    1、连接上相应的linux主机,进入到等待输入shell指令的linux命令行状态下。  2、在命令行下输入shell指令:sz/var/log/boot.log,此时会弹出下载位置选择。3、点击确定......
  • 关于ADB命令工具
    android提供了不少命令行工具,方便我们调试和查看信息.下面是frameworks/base/cmds(android6.0.1)中的命令.$treecmds-L1cmdsam├──appops├──app_process├......