if-statement

how to trap errors inside the if statement

丶灬走出姿态 提交于 2021-02-10 03:12:30
问题 Running the following code: #!/bin/bash set -o pipefail set -o errtrace set -o nounset set -o errexit function err_handler () { local error_code="$?" echo "TRAP!" echo "error code: $error_code" exit } trap err_handler ERR echo "wrong command in if statement" if xsxsxsxs then echo "if result is true" else echo "if result is false" fi echo -e "\nwrong command directly" xsxsxsxs exit produces the following output: wrong command in if statement trap.sh: line 21: xsxsxsxs: command not found if

how to trap errors inside the if statement

戏子无情 提交于 2021-02-10 03:06:45
问题 Running the following code: #!/bin/bash set -o pipefail set -o errtrace set -o nounset set -o errexit function err_handler () { local error_code="$?" echo "TRAP!" echo "error code: $error_code" exit } trap err_handler ERR echo "wrong command in if statement" if xsxsxsxs then echo "if result is true" else echo "if result is false" fi echo -e "\nwrong command directly" xsxsxsxs exit produces the following output: wrong command in if statement trap.sh: line 21: xsxsxsxs: command not found if

C# Is there any better way to use Switch-Case / If-Else on generic types

主宰稳场 提交于 2021-02-08 11:38:03
问题 I am currently trying to get some casting on generic types done. So the base idea is to have a method which accepts a generic Type and does different stuff depending on which type gets passed in. For simplicity reasons I will just showcase the use of float , bool and default The setup looks something like this (where T is a generic type defined by the class itself): protected T DoStuff(T value) { switch (value) { case float floatValue: float result = DoFloatStuff(floatValue); switch (result)

C# Is there any better way to use Switch-Case / If-Else on generic types

拥有回忆 提交于 2021-02-08 11:37:47
问题 I am currently trying to get some casting on generic types done. So the base idea is to have a method which accepts a generic Type and does different stuff depending on which type gets passed in. For simplicity reasons I will just showcase the use of float , bool and default The setup looks something like this (where T is a generic type defined by the class itself): protected T DoStuff(T value) { switch (value) { case float floatValue: float result = DoFloatStuff(floatValue); switch (result)

Execute command and compare it in a if statement

你说的曾经没有我的故事 提交于 2021-02-08 10:35:12
问题 I am using bash scripting to check whether two specific directories are containing some specific number of files. lets assume 20. Is it possible to check it in a line inside a if statement? #!/bin/bash if [ [ls /path/to/dir1 | wc -l ] != 20 || [ ls /path/to/dir2 | wc -l ] != 50 ]; then echo "do this!" elif echo "do that!" fi 回答1: The syntax is incorrect: if [[ $(ls /path/to/dir1 | wc -l) != 20 || $(ls /path/to/dir2 | wc -l) != 50 ]] then echo "do this!" else echo "do that!" fi (I move the

What is the correct way to use the bash if else && || shortcut?

守給你的承諾、 提交于 2021-02-08 09:56:53
问题 I have come across some strange behavious (well probably not strange but I don't understand it !) I want to write some if / e,se statements using the bash shorthand syntax: [[ 1 -eq 1 ]] && echo "true" || echo "false" The output of the above code gives: true Now the code above actually works fine. But this next code does not: [[ 1 -eq 1 ]] && infMsg "true" || infMsg "false" infMsg is just a function. The output from the above code gives: true false I only want it to say 'true'. Is there

How to execute something only once within if-loop?

房东的猫 提交于 2021-02-08 08:13:28
问题 EDIT (more specific): I have the following situation. I am building an experimental synthesizer interface with JavaScript. I am using the howler.js plugin to handle audio. The idea is that a sound is played if a certain condition is met. I have a number stored variable whose value changes via the heading of an iPhone compass . I use compass.js. It has a variable called heading. $(function(){ Compass.watch(function (heading) { $('.degrees').text(Math.floor(heading) + '°'); $('#rotate').css('

How can I write “if button clicked” in an If statement in android studio?

一曲冷凌霜 提交于 2021-02-08 07:34:44
问题 I am building a memory game with four cards(2x2). These four cards have an onClick named "cards". This onClick consists of an If statement that flips the cards back if they are not the same, and keeps them if they are the same. The front image of the card is the same for the 4, but the back has different images.My problem is that I want the cards to flip, but they already have an onClick. So how can I write "if button clicked" in an If statement or is there another solution? EDIT: button1

Is it possible to find out which number is bigger without using an if statement?

三世轮回 提交于 2021-02-08 05:44:34
问题 I'm doing some small program for a beginners programming course and in my program I have 2 variables which hold numbers. Anyway I need to find out which number is bigger and print the appropriate message according to it, for example I have: int x = 5; int y = 10; I need to print: "it is true that y is bigger than x"; Now the thing is that I know I can use a simple if statement but I'm not allowed to use it, now it makes me wonder, is it even possible? If so, how can I do that? How can I check

R: Generate a dummy variable based on the existence of one column' value in another column

扶醉桌前 提交于 2021-02-08 03:39:36
问题 I have a data frame like this: A B 2012,2013,2014 2011 2012,2013,2014 2012 2012,2013,2014 2013 2012,2013,2014 2014 2012,2013,2014 2015 I wanted to create a dummy variable, which indicates whether the value in column B exists in column A. 1 indicates the existence, and 0 indicates non-existant. Such that, A B dummy 2012,2013,2014 2011 0 2012,2013,2014 2012 1 2012,2013,2014 2013 1 2012,2013,2014 2014 1 2012,2013,2014 2015 0 I have tried to use %in% to achieve this: df$dummy <- ifelse(df$B %in%