Open main menu

CDOT Wiki β

Changes

OPS245 Scripting Exercises dev

1,677 bytes added, 15:08, 14 January 2023
Variables
$? is a special variable that contains the exit code of the last executed command. Linux uses an exit code of 0 to represent success. Any non-zero value represents failure. To check the exit status of the last command use '''echo $?'''.
* How === Variables ======= Bash Variables ====In ULI101 you learned about Bash variables. You can create a variable in bash at any time. You do not have to assign variable types to variables in Bash. Bash variables are character strings by default, but act as other data types based on the context you use them. For example, when you perform a math operation on two variables in bash they are treated as numbers. This is how to create a variable in Bash and set assign it a value in :<pre>number=1</pre> There should be no space between the variable and it's value. Additionally, variable names are alpha-numeric and cannot begin with a number.* How To refer to get the value from stored in a bash variable, you simply use the variable name with a $ in front of it. The $ means '''the value of''' that variable. The following example will print out the value of the variablenumber (which we assigned above) using the echo command:* Differences <pre>echo $number</pre> === Python Variables ===Python variables can be used in the same way as Bash variables (ie you do not need to assign a type when creating the variable). However, Python allows you the option to specify a data type when creating a variable. This is called casting. Python data types and casting is something we may look at later in the course. This is how bash to create a variable in Python and python handle assign it a value:<pre>number = 1</pre>Note the difference in spacing when compared to Bash variables. Python variablesrequire a space between the equal sign and the content on either of it. To refer to the value stored in a Python variable, you do not need a $ in front of it. You simply refer to the variable by name. The following example with print out the value of the variable number (which we assigned above) using the print function:<pre>print(number)</pre>
== Getting input from the user ==