{"id":46,"date":"2024-02-18T23:20:01","date_gmt":"2024-02-18T23:20:01","guid":{"rendered":"http:\/\/courses.usnh.domains\/cms\/?p=46"},"modified":"2024-02-18T23:20:01","modified_gmt":"2024-02-18T23:20:01","slug":"conditional","status":"publish","type":"post","link":"https:\/\/courses.usnh.domains\/cms\/it-612\/conditional\/","title":{"rendered":"Conditional"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Bash Conditional Expressions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In computer science, conditional statements, conditional expressions, and conditional constructs are features of a programming language, which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Bash, conditional expressions are used by the <code>[[<\/code> compound command and the <code>[<\/code>built-in commands to test file attributes and perform string and arithmetic comparisons.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is a list of the most popular Bash conditional expressions. You do not have to memorize them by heart. You can simply refer back to this list whenever you need it!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/bobbyiliev\/introduction-to-bash-scripting\/blob\/main\/ebook\/en\/content\/009-bash-conditional-expressions.md#file-expressions\"><\/a><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">File expressions<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if file exists.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ -a ${file} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if file exists and is a block special file.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ -b ${file} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if file exists and is a character special file.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ -c ${file} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if file exists and is a directory.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ -d ${file} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if file exists.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ -e ${file} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if file exists and is a regular file.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ -f ${file} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if file exists and is a symbolic link.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ -h ${file} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if file exists and is readable.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ -r ${file} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if file exists and has a size greater than zero.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ -s ${file} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if file exists and is writable.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ -w ${file} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if file exists and is executable.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ -x ${file} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if file exists and is a symbolic link.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ -L ${file} ]]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/bobbyiliev\/introduction-to-bash-scripting\/blob\/main\/ebook\/en\/content\/009-bash-conditional-expressions.md#string-expressions\"><\/a><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">String expressions<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if the shell variable varname is set (has been assigned a value).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ -v ${varname} ]]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">True if the length of the string is zero. [[ -z ${string} ]]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">True if the length of the string is non-zero. [[ -n ${string} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if the strings are equal. <code>=<\/code> should be used with the test command for POSIX conformance. When used with the <code>[[<\/code> command, this performs pattern matching as described above (Compound Commands).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ ${string1} == ${string2} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if the strings are not equal.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ ${string1} != ${string2} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if string1 sorts before string2 lexicographically.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ ${string1} &lt; ${string2} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>True if string1 sorts after string2 lexicographically.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ ${string1} &gt; ${string2} ]]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/bobbyiliev\/introduction-to-bash-scripting\/blob\/main\/ebook\/en\/content\/009-bash-conditional-expressions.md#arithmetic-operators\"><\/a><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">Arithmetic operators<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Returns true if the numbers are <strong>equal<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ ${arg1} -eq ${arg2} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Returns true if the numbers are <strong>not equal<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ ${arg1} -ne ${arg2} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Returns true if arg1 is <strong>less than<\/strong> arg2<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ ${arg1} -lt ${arg2} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Returns true if arg1 is <strong>less than or equal<\/strong> arg2<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ ${arg1} -le ${arg2} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Returns true if arg1 is <strong>greater than<\/strong> arg2<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ ${arg1} -gt ${arg2} ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Returns true if arg1 is <strong>greater than or equal<\/strong> arg2<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ ${arg1} -ge ${arg2} ]]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As a side note, arg1 and arg2 may be positive or negative integers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As with other programming languages you can use <code>AND<\/code> &amp; <code>OR<\/code> conditions: [[ test_case_1 ]] &amp;&amp; [[ test_case_2 ]] # And [[ test_case_1 ]] || [[ test_case_2 ]] # Or<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/bobbyiliev\/introduction-to-bash-scripting\/blob\/main\/ebook\/en\/content\/009-bash-conditional-expressions.md#exit-status-operators\"><\/a><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">Exit status operators<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>returns true if the the command was successful without any errors<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ $? -eq 0 ]]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>returns true if the the command was not successful or had errors<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">[[ $? -gt 0 ]]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bash Conditional Expressions In computer science, conditional statements, conditional expressions, and conditional constructs are features of a programming language, which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. In Bash, conditional expressions are used by the [[ compound command and the [built-in commands to test file [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,3],"tags":[],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-bash","category-it-612"],"_links":{"self":[{"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/posts\/46","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/comments?post=46"}],"version-history":[{"count":1,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":47,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/posts\/46\/revisions\/47"}],"wp:attachment":[{"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}