标签: conditional-statements

使用 SYSTEMCTL 状态的 grep 搜索结果到 IF CONDITION

首先,感谢您对此问题的调查!

我是 bash 脚本的新手,我想要实现的是:我目前必须经常在工作中重新启动一些服务,我正在尝试创建一个脚本来自动化它,我设法创建了所有内容,但我也想创建一个条件,其中如果 systemctl status = inactive 则 echo“好的,很好,一切都很好”,所以让我们继续,但如果没有,则 echo“将在 10 秒内重新尝试重新启动”;我使用 grep 从服务的 systemctl 状态中查找文本“active: active (running)”或“active: inactive (dead)”(由下面的 bluetooth.service 表示):

#!/bin/bash
#Restarting  bluetooth.service

clear
active_stop="Active: stopped (dead)"    
echo  "This script will RESTART the following service: Bluetooth.Service"
echo -e  "State the REASON for the RESTART: " >> /home/sjadmin/SYS_RESTART/ARCHIVE/sysrestart.log
read -p  "Press ENTER to continue or CTRL + C to cancel"

sudo systemctl stop bluetooth.service |grep active IF [[grep -q=$active_stop]] 

then read -p  "Service STOPPED, please confirm status bellow, …
Run Code Online (Sandbox Code Playgroud)

grep if-statement conditional-statements systemctl

0
推荐指数
1
解决办法
5543
查看次数

Bash:通过分组命令和布尔运算符实现不带 IF 的条件语句

我正在尝试从 Bash 脚本中消除“if”语法:

N=
MD='title|1.md' 
Run Code Online (Sandbox Code Playgroud)

这按预期工作(打印“1”和“title_1”):

if [ $(echo "$MD" | grep '|') ]; then N="${MD#*|}"; ID=${MD%|*}'_'$N; fi
echo "$N" ; echo "$ID" 
Run Code Online (Sandbox Code Playgroud)

这不会(打印空):

[[ $(echo $MD | grep '|') ]] && $(N="${MD#*|}" ; ID=${MD%|*}'_'$N)
echo "$N" ; echo "$ID" 
Run Code Online (Sandbox Code Playgroud)

使用它运行脚本bash +x会返回:

+ N=
++ echo 'title|1'
++ grep '|'
+ [[ -n title|1 ]]
++ N=1
++ ID=title_1
+ echo ''

+ echo ''

+ exit
Run Code Online (Sandbox Code Playgroud)

有谁能告诉我我做错了什么?谢谢。

bash conditional-formatting conditional-statements

0
推荐指数
1
解决办法
4248
查看次数

Flutter:条件底部导航栏,根据if条件显示页面

我已经实现了一个具有下面列出的逻辑的底部导航栏,我想检查用户是否已登录。如果用户在第三个选项卡上登录,我想显示个人资料页面而不是登录页面。这是我显示底部导航栏的代码,我被困在 checkIsLoggedIn() 异步函数之后要做什么。

强文本

之后,我构建小部件列表以在 3 个选项卡中显示页面。其代码如下,

在此输入图像描述

因此,我需要显示,如果用户已登录,则代替 SignIn(),它会显示登录,否则,它会显示个人资料页面。请帮我一下。

conditional-statements flutter flutter-layout

0
推荐指数
1
解决办法
3762
查看次数

Python 中的 if 语句,其中 if 5 个条件中的 3 个为 true

我想知道如何在 python 中使用一个 if 语句,该语句将对 5 个条件中的任何 3 个条件为真做出反应,以使 if 语句为真。

    if (a>2) and (b>3) and (c>2) and (d>6) and (e>4):
       list.append(True)
Run Code Online (Sandbox Code Playgroud)

如果满足所有 5 个条件,此代码会将 true 添加到数组“列表”,但我想知道如果 5 个条件中的任何 3 个为 true,然后将 true 附加到“列表”,如何让它工作?

python if-statement conditional-statements

0
推荐指数
1
解决办法
1584
查看次数

如何使用三元运算符执行此 IF

如何使用三元运算符执行此 IF 操作?

if (img != null) {
  return img;
} else if (!user) {
  return 'img.svg';
} else if (user) {
  if (user.img && !camera) {
    return `imgServer`;
  } else if (!user.img && !camera) {
    return 'img.svg';
  } else if (user.img && camera) {
    return camera;
  } else if (!user.img && camera) {
    return camera;
  }
} 
Run Code Online (Sandbox Code Playgroud)

我已经尝试过多种方法,但没有一种对我有用。

javascript if-statement conditional-operator conditional-statements

0
推荐指数
1
解决办法
111
查看次数

如何检查某些内容是否可以是 TypeScript 中的字符串?

我想创建一个 TypeScript 类型来检查元素是否可以是字符串。

这意味着该元素可以具有“string”或“any”类型,但不能具有“number”、“boolean”、“number[]”、“Person”等类型。

我尝试使用条件类型,但我找不到如何排除“所有不可能是字符串的内容”。

我也尝试了 Exclude<any, string> 类型,但这并不完全是我想要的。

编辑

我更好地解释了我的问题:我创建了一个静态方法,如果数据是 a ,foo(data)它将执行某些操作并返回 a ,或者如果数据不是 a ,则执行其他操作并返回 a 。stringstringnumberstring

为此,很简单,我可以像这样进行重载:

class FooClass {

    static foo(data: string): string
    static foo(data: any): number
    static foo(data: any): string | number {
        if (typeof data === 'string') {
            return 'a';
        } else {
            return 2;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,假设如果不是 a ,该foo()方法将抛出错误。我有两个案例:datastring

  • 如果该方法的用户不知道数据的类型(可能是 a string),我希望他可以使用此方法,如果不是 a ,我将返回一个错误string

  • 如果用户知道数据的类型(number例如 a ),我想告诉他这是不可能的,它不可能是 …

types conditional-statements typescript

0
推荐指数
1
解决办法
2812
查看次数

CSS中的条件?

是否可以使用某种形式的条件?

用代码构建新的设计风格。

通常<h1>标签下面有一条小线。标签<h2>也可以,但是当<h1>存在 时,<h2>标签下面不应该有一行。

h1:after {
    display: block;
    content: "";
    background-color: green;
    height: 3px;
    border-radius: 6px;
    width:50px
}

h2:after {
    display: block;
    content: "";
    background-color: orange;
    height: 3px;
    border-radius: 6px;
    width:30px
}
Run Code Online (Sandbox Code Playgroud)
<h1>H1 title</h1> 

<h2>So now this H2 title should not have a line since there is a h1 above it.</h2> 
Run Code Online (Sandbox Code Playgroud)

html css conditional-statements

0
推荐指数
1
解决办法
158
查看次数

如何将(字符串)条件作为函数参数传递?

我想将 3 个条件传递给函数“check”以应用于 str1,在这种情况下输出应该是 [False, False, True]:

def check(conditions):
    str1 = '/'
    print(conditions)

check(conditions=[str1.find('//www.fao.org')!=-1, str1.find('//fao.org')!=-1, str1[0]=='/'])
Run Code Online (Sandbox Code Playgroud)

然而,在调用该函数之前,它会运行一个错误:

NameError: name 'str1' is not defined
Run Code Online (Sandbox Code Playgroud)

因为这意味着甚至在执行函数检查之前就执行了条件,所以我如何将这些条件作为参数传递?

python function conditional-statements

0
推荐指数
1
解决办法
564
查看次数

我想禁用基于 auth()-&gt;user()-&gt;id 的删除按钮 (LARAVEL 8)

我已经创建了用户索引,在该表上显示除密码之外的所有用户数据,并在最后一个字段中设置了删除用户的操作。问题是我只想为当时登录的用户禁用删除按钮。但我所做的是禁用所有用户删除按钮,请帮助我如何解决这个问题。我试图找到教程,但还没有找到。下面是我的条件代码:

@if (auth()->user()->id)
    <button class="btn btn-outline-danger
    btn-xs" disabled><i class="fe fe-trash"></i>Delete</button>
@else
    <button class="btn btn-outline-danger
    btn-xs"><i class="fe fe-trash"></i>Delete</button>
@endif
Run Code Online (Sandbox Code Playgroud)

在 UserController 中显示所有用户数据我使用以下代码:

public function index(){
    $users = User::all();
    return view('admin.users.index', ['users'=> $users]);
}
Run Code Online (Sandbox Code Playgroud)

在index.blade中,我应用此代码来显示所有数据:

<table id="dataTableBasic" class="table" style="width:100%">
    <thead>
    <tr>
        <th>{{ __('Username') }}</th>
        <th>{{ __('Name') }}</th>
        <th>{{ __('Email') }}</th>
        <th>{{ __('Role') }}</th>
        <th>{{ __('Posts') }}</th>
        <th>{{ __('Action') }}</th>
    </tr>
    </thead>
    <tbody>
    @foreach($users as $user)
    <tr>
        <td><a href="{{route('user.profile.show', $user->id)}}" class="text-inherit link-primary">{{$user->username}}</a></td>
        <td>{{$user->name}}</td>
        <td><a href="mailto:{{$user->email}}" class="text-inherit link-primary">{{$user->email}}</a></td>
        <td>Administrator</td>
        <td><a href="#" class="text-inherit">100</a></td>
        <td>
            <form …
Run Code Online (Sandbox Code Playgroud)

php conditional-statements laravel

0
推荐指数
1
解决办法
1094
查看次数

python 函数没有正确返回它们的值?

我正在尝试学习 python,并且刚刚了解了条件语句,并且正在努力创建自己的函数。

您介意告诉我我做错了什么吗?我必须在最后编写复杂的打印语句来一一调用我的所有函数?

此外,任何风格提示也将不胜感激。

预先感谢您可以提供的任何信息。

import random

#VARIABLES
topChamps = ['Nasus', 'Garen', 'Nautilus']
botChamps = ['Draven', 'Caitlyn', 'Jhin']
midChamps = ['Lux', 'Veigar', 'Karthus']
supChamps = ['Sona', 'Tahm Kench', 'Alistar']
jgChamps = ['Amumu', 'Rammus', 'Malphite']
selectNum = 0
roleNum = 0
champArr = []
champ = "wrong"
role = str(input("what role did you get? "))

#function to make the role choice easier for computer to read
def roleChoose():
    if role == "top":
        roleNum = 1
    elif role == "bot":
        roleNum = …
Run Code Online (Sandbox Code Playgroud)

python function conditional-statements

0
推荐指数
1
解决办法
132
查看次数