[LINUX] Conditional expression grouping

Hello. I will post to Kita for the first time. Thank you.

It is a memo writing of learning of the new linux textbook (SB Creative). Share what you've found where you're stuck or what you've researched to solve a problem.

You learned P295 "Grouping conditional expressions".

[Miss] When I wrote a conditional expression and tried to refer to a variable, I made a mistake of pulling out the dollar sign. When you refer to a variable, you tend to omit the dollar sign. I want to be careful.

This is an error -d "datadir".

if [ -d "datadir" -a \( "$datadir" = "/home/pc/myapp/295_data_group" -o "$datadir" = "/home/pc/295_data_group" \)  ]; then

Correctly this -d "$ datadir"

if [ -d "$datadir" -a \( "$datadir" = "/home/pc/myapp/295_data_group" -o "$datadir" = "/home/pc/295_data_group" \)  ]; then

[English] The conditional expression is "conditional expression" in English. I tried to execute the conditional expression grouping learned on this page with a shell script. The file name is "295_conditional_expression_group.sh". It is easy to review if you put the number of pages "295" in the file name.

Looking up technical keywords in English, I thought it would be good to improve my English reading ability. In this book, there are many situations where you create a file and try to execute it, so if you look up the technical keywords that are the theme in English and actively use them with file names, variable names, and path names. You can also improve your English.

[Grouping conditional expressions] What is conditional expression grouping in the first place? Here's a simple analogy.

This is the conditional expression and the processing when it is true.

if your wallet has more than 10,000 yen. And I'm hungry. If this is true echo Let's buy rice balls. Execute.

So When you only have 900 yen in your wallet. I don't buy rice balls. When you are not hungry. I don't buy rice balls.

What is conditional expression grouping here?

if you have 1000 yen or more in your wallet. And (I'm hungry, or there's nothing in the fridge.) echo Let's buy rice balls. Execute.

This.

Use parentheses to group.

When you only have 800 yen in your wallet. I don't buy rice balls. I have 1100 yen in my wallet, and when I'm hungry, I buy rice balls. I buy rice balls when I have 1100 yen in my wallet and nothing in the refrigerator. I don't buy rice balls when I have 1100 yen in my wallet and something in the refrigerator.

[code] This is the shell script I made.

The contents are as follows. Associate a directory with a variable. If the directory exists and the directory matches either of the two specified paths It prints a string that says there is a directory and says it doesn't.

295_conditional_expression_group.sh


#!/bin/bash

datadir=/home/pc/myapp/295_data_group

if [ -d "$datadir" -a \( "$datadir" = "/home/pc/myapp/295_data_group" -o "$datadir" = "/home/pc/295_data_group" \)  ]; then
	echo "$datadir directory exist"
else
        echo "$datadir directory does not exist"
fi	

Recommended Posts

Conditional expression grouping
Lambda expression