Open main menu

CDOT Wiki β

Changes

ULI101 Week 10

3 bytes removed, 21:32, 31 August 2017
no edit summary
<code>alias clearfile=”cat /dev/null &gt;”</code>
== Variables ==
Shell variables a classified in 2 groups:
Variables can be read/write or read-only. Name of a variable can be any sequence of letters and numbers, but it must not start with a number.
=== Common Shell Variables ===
Shell environment variables shape the working environment whenever you are logged in. Common shell variables include:
The <code>set</code> command will display all available variables
=== The <code>PATH</code> variable ===
* <code>PATH</code> is an environment variable present in Unix/Linux operating systems, listing directories where executable programs are located
* Use <code>./</code> prefix or modify the <code>PATH</code> as needed
=== Assigning a Value ===
Syntax: <code>name=value</code>
</source>
=== Read-Only Variables ===
* Including the keyword <code>readonly</code> before the command assignment prevents you from changing the variable afterwards. For example: <code>readonly phone=“123-4567”</code>
* If no variable name is supplied a list of defined read only variables will be displayed
=== Removing Variables ===
<code>variable=</code>
Read-only variables cannot be removed, you must log out for them to be cleared.
=== Variable Substitution ===
Whenever you wish to read a variable (its contents), use the variable name preceded by a dollar sign ($). This is commonly called ''variable substitution''.
* <code>$#</code> represents the number of parameters (not including the script name)
== <code>echo</code> command ==
* Displays messages to the terminal followed by a newline. Use the <code>-n</code> option to suppress the default newline.
* Arguments are usually double quoted.
== <code>read</code> command ==
* The <code>read</code> command allows obtaining user input and storing it in a variable. Everything is captured until the Enter key is pressed.
echo Hello $name
</source>
 == Using Logic ==
The purpose of the if statement is execute a command or commands based on a condition. The condition is evaluated by a test command, represented below by a pair of square brackets
</source>
== <code>test</code> Command ==
The <code>test</code> command can be used in two ways:
Check <code>man test</code> for more details
== Using Loops ==
A for loop is a very effective way to repeat the same command(s) for several arguments such as file names. The syntax and example of for command is shown below:
221
edits