Artwork

תוכן מסופק על ידי HPR Volunteer and Hacker Public Radio. כל תוכן הפודקאסטים כולל פרקים, גרפיקה ותיאורי פודקאסטים מועלים ומסופקים ישירות על ידי HPR Volunteer and Hacker Public Radio או שותף פלטפורמת הפודקאסט שלהם. אם אתה מאמין שמישהו משתמש ביצירה שלך המוגנת בזכויות יוצרים ללא רשותך, אתה יכול לעקוב אחר התהליך המתואר כאן https://he.player.fm/legal.
Player FM - אפליקציית פודקאסט
התחל במצב לא מקוון עם האפליקציה Player FM !

HPR3713: Bash snippet - short-circuit evaluation in Bash Boolean expressions

 
שתפו
 

Fetch error

Hmmm there seems to be a problem fetching this series right now. Last successful fetch was on December 04, 2025 14:13 (10d ago)

What now? This series will be checked again in the next day. If you believe it should be working, please verify the publisher's feed link below is valid and includes actual episode links. You can contact support to request the feed be immediately fetched.

Manage episode 446211216 series 2795599
תוכן מסופק על ידי HPR Volunteer and Hacker Public Radio. כל תוכן הפודקאסטים כולל פרקים, גרפיקה ותיאורי פודקאסטים מועלים ומסופקים ישירות על ידי HPR Volunteer and Hacker Public Radio או שותף פלטפורמת הפודקאסט שלהם. אם אתה מאמין שמישהו משתמש ביצירה שלך המוגנת בזכויות יוצרים ללא רשותך, אתה יכול לעקוב אחר התהליך המתואר כאן https://he.player.fm/legal.
Preamble This is a case where I came upon a thing in Bash I had never considered before and was pleased and surprised that there was a way of doing what I wanted to do! If this is completely obvious to you, apologies, but it wasn’t to me! Overview Many programming languages have the concept of short-circuit evaluation in Boolean expressions. What this means is that in an expression such as: A AND B if A is false then the whole expression must be false, and B doesn’t have to be evaluated. That is because both arguments to AND have to be true for the overall result to be true. If A is true on the other hand, then B has to be evaluated to determine if the overall result is true. Similarly with: A OR B if A is true then the whole expression must be true and B can be skipped without evaluation. This is because only one argument to OR needs to be true to return a true result. If A is false on the other hand, then B has to be evaluated to determine if the overall result is false. Both of these expressions are evaluated from left to right. This is not a given in all languages. Some use special operators such as 'and_then' and 'or_else' which explicitly perform short-circuiting and left-to-right evaluation. Definition In simple terms, short-circuiting is where the evaluation of an expression is stopped as soon as its outcome is determined. The Wikipedia article Short-circuit evaluation defines it as: Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true. This article contains a table entitled Boolean operators in various languages which shows details of how various programming and scripting languages cater for this feature. Use case I was writing a Bash script in which I wanted to ask questions about various steps - should they be done or not? Alternatively, I wanted to be able to set an option to run without interaction and assume the answer is 'yes' to all questions. I’d encountered short-circuit evaluation before in Pascal and Perl so I wondered if I could use it in Bash. The expression I was trying to write was: if [[ $YES -eq 1 ]] || yes_no 'Create directory? %s ' 'N'; then # Create directory fi Variable YES is being set through an option '-Y'; it’s normally set to zero but is set to 1 if the option is used. yes_no is a function I wrote, and talked about in HPR episode 2096: “Useful Bash functions - part 2”. The requirement was that if YES was set to 1 I didn’t want the function
  continue reading

116 פרקים

Artwork
iconשתפו
 

Fetch error

Hmmm there seems to be a problem fetching this series right now. Last successful fetch was on December 04, 2025 14:13 (10d ago)

What now? This series will be checked again in the next day. If you believe it should be working, please verify the publisher's feed link below is valid and includes actual episode links. You can contact support to request the feed be immediately fetched.

Manage episode 446211216 series 2795599
תוכן מסופק על ידי HPR Volunteer and Hacker Public Radio. כל תוכן הפודקאסטים כולל פרקים, גרפיקה ותיאורי פודקאסטים מועלים ומסופקים ישירות על ידי HPR Volunteer and Hacker Public Radio או שותף פלטפורמת הפודקאסט שלהם. אם אתה מאמין שמישהו משתמש ביצירה שלך המוגנת בזכויות יוצרים ללא רשותך, אתה יכול לעקוב אחר התהליך המתואר כאן https://he.player.fm/legal.
Preamble This is a case where I came upon a thing in Bash I had never considered before and was pleased and surprised that there was a way of doing what I wanted to do! If this is completely obvious to you, apologies, but it wasn’t to me! Overview Many programming languages have the concept of short-circuit evaluation in Boolean expressions. What this means is that in an expression such as: A AND B if A is false then the whole expression must be false, and B doesn’t have to be evaluated. That is because both arguments to AND have to be true for the overall result to be true. If A is true on the other hand, then B has to be evaluated to determine if the overall result is true. Similarly with: A OR B if A is true then the whole expression must be true and B can be skipped without evaluation. This is because only one argument to OR needs to be true to return a true result. If A is false on the other hand, then B has to be evaluated to determine if the overall result is false. Both of these expressions are evaluated from left to right. This is not a given in all languages. Some use special operators such as 'and_then' and 'or_else' which explicitly perform short-circuiting and left-to-right evaluation. Definition In simple terms, short-circuiting is where the evaluation of an expression is stopped as soon as its outcome is determined. The Wikipedia article Short-circuit evaluation defines it as: Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true. This article contains a table entitled Boolean operators in various languages which shows details of how various programming and scripting languages cater for this feature. Use case I was writing a Bash script in which I wanted to ask questions about various steps - should they be done or not? Alternatively, I wanted to be able to set an option to run without interaction and assume the answer is 'yes' to all questions. I’d encountered short-circuit evaluation before in Pascal and Perl so I wondered if I could use it in Bash. The expression I was trying to write was: if [[ $YES -eq 1 ]] || yes_no 'Create directory? %s ' 'N'; then # Create directory fi Variable YES is being set through an option '-Y'; it’s normally set to zero but is set to 1 if the option is used. yes_no is a function I wrote, and talked about in HPR episode 2096: “Useful Bash functions - part 2”. The requirement was that if YES was set to 1 I didn’t want the function
  continue reading

116 פרקים

כל הפרקים

×
 
Loading …

ברוכים הבאים אל Player FM!

Player FM סורק את האינטרנט עבור פודקאסטים באיכות גבוהה בשבילכם כדי שתהנו מהם כרגע. זה יישום הפודקאסט הטוב ביותר והוא עובד על אנדרואיד, iPhone ואינטרנט. הירשמו לסנכרון מנויים במכשירים שונים.

 

מדריך עזר מהיר

האזן לתוכנית הזו בזמן שאתה חוקר
הפעלה