Changes

Jump to: navigation, search

OPS245 Scripting Exercises dev

24 bytes removed, 15:12, 14 January 2023
no edit summary
</pre>
== Runnning scripts ==
=== Running scripts from your current directory or another directory or a directory in the $PATH ===
You can run a script from your current directory with ./ followed by the script name (without a space). I.E. '''./script.bash''' or '''./script.py''' Alternatively, if the script is in a directory that is specified in your $PATH environment variable you can execute the script by simply typing the name of the script without the ./. You can view your $PATH variable by issuing the command '''echo $PATH'''.
=== Script Permissions ===
You need to make sure scripts have the execute permission for the user or groups you wish to be able to execute your script. You can add the execute permission with the '''chmod''' command, which you learned in ULI101. As a quick refresher, what is the difference between the following commands?
Both of the above commands will work. However, the first one gives execute permission to user, groups and other (in otherwords '''everyone'''). This is not the most secure way of allowing your scripts to be executed. The second one is a much better practice, which will give only your user execute permission.
== Variables ==
=== $PWD and pwd, what is the difference? ===
$PWD is an environment variable that contains the path to your present working directory as an '''absolute path'''. pwd is a command that lists your present working directory, as an '''absolute path'''.
=== Exit codes and $? ===
$? 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 $?'''.
=== Variables in Bash vs Python ======= 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.
</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.

Navigation menu