Course Hero Logo

Systemctl is active psacct devnull 21 if ne 0 then

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 783 - 786 out of 862 pages.

systemctl is-active psacct > /dev/null 2>&1if[ $? -ne 0 ]; thensystemctl start psacctfi
Chapter27.Bash Conditionals and Control Structures762RH299-RHEL7-en-1-20150420If/then/else statementTheif/thenconditional structure can be further expanded so that different sets of actions canbe taken depending on whether a condition is met. This is accomplished with theif/then/elseconditional construct.if <CONDITION>; then<STATEMENT>...<STATEMENT>else<STATEMENT>...<STATEMENT>fiThe following code section demonstrates the use of anif/then/elsestatement to start thepsacctservice if it is not active and to stop it if it is active.systemctl is-active psacct > /dev/null 2>&1if[ $? -ne 0 ]; thensystemctl start psacctelsesystemctl stop psacctfiIf/then/elif/then/else statementLastly, theif/then/elseconditional structure can be further expanded to test more than onecondition, executing a different set of actions when a condition is met. The construct for this isshown in the following example. In this conditional structure, Bash will test the conditions in theorder presented. Upon finding a condition that is true, Bash will execute the actions associatedwith the condition and then skip the remainder of the conditional structure. If none of theconditions are true, then Bash will execute the actions enumerated in theelseclause.if <CONDITION>; then<STATEMENT>...<STATEMENT>elif <CONDITION>; then<STATEMENT>...<STATEMENT>else<STATEMENT>...<STATEMENT>fiThe following code section demonstrates the use of anif/then/elif/then/elsestatementto run themysqlclient if themariadbservice is active, run thepsqlclient if thepostgresqlservice is active, or run thesqlite3client if both themariadbandpostgresqlservices are notactive.systemctl is-active mariadb > /dev/null 2>&1MARIADB_ACTIVE=$?systemctl is-active postgresql > /dev/null 2>&1
Using conditional structuresRH299-RHEL7-en-1-20150420763POSTGRESQL_ACTIVE=$?if[ "$MARIADB_ACTIVE" -eq 0 ]; thenmysqlelif[ "$POSTGRESQL_ACTIVE" -eq 0 ]; thenpsqlelsesqlite3fiCase statementUsers can add as manyelifclauses as they want into anif/then/elif/then/elsestatement to test as many conditions as they need. However, as more are added, the statementand its logic becomes increasingly harder to read and comprehend. For these more complexsituations, Bash offers another conditional structure known ascase statements. Thecasestatement utilizes the following syntax:case <VALUE> in<PATTERN1>)<STATEMENT>...<STATEMENT>;;<PATTERN2>)<STATEMENT>...<STATEMENT>;;esacThecasestatement attempts to match<VALUE>to each<PATTERN>in order, one by one.When a pattern matches, the code segment associated with that pattern is executed, with the;;syntax indicating the end of the block. All other patterns remaining in thecasestatement arethen skipped and thecasestatement is exited. As many pattern/statement blocks as needed canbe added.

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 862 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
N/A
Tags

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture