create script variable-page
git-svn-id: svn+ssh://atelier.inf.usi.ch/home/bevilj/group-1@172 a672b425-5310-4d7a-af5c-997e18724b81
This commit is contained in:
parent
8f8e268088
commit
0a1a64766e
|
@ -8,7 +8,7 @@ title: Redirection
|
|||
---
|
||||
<p>
|
||||
|
||||
<h3>Output as input</h3>
|
||||
<h3>output as input</h3>
|
||||
<br>
|
||||
To redirect a certain output of the command-line we have to use the symbol ">".<br>
|
||||
|
||||
|
@ -39,7 +39,7 @@ cat hello.txt
|
|||
</pre>
|
||||
|
||||
<!-- Input redirection text -->
|
||||
<h3>Input as output</h3>
|
||||
<h3>input as output</h3>
|
||||
<br>
|
||||
To redirect an input from a file for a command, the symbol "<" is used.
|
||||
|
||||
|
@ -51,7 +51,7 @@ echo < $(cat hello.txt)
|
|||
<p>This is particularly useful when chaining commands.</p>
|
||||
<br>
|
||||
<!-- Chaining command text -->
|
||||
<h3>Chaining (or Piping)</h3>
|
||||
<h3>chaining (or piping)</h3>
|
||||
<br>
|
||||
Chaining, also called Piping because it works with the pipe symbol "|", takes the output of a certain command-line and feeds it to another command in a direct way.
|
||||
<!-- Table for the example of the piping command -->
|
||||
|
@ -61,7 +61,7 @@ Moon
|
|||
</pre>
|
||||
|
||||
<!-- A simple example -->
|
||||
<h3>Simple Example</h3>
|
||||
<h3>simple example</h3>
|
||||
<br>
|
||||
Now let's say that we want to combine those commands in a more complex operation. Let's say we want to take some contents from a certain file and put it into another file.<br>
|
||||
|
||||
|
|
99
site/pages/scripts/variables.html
Normal file
99
site/pages/scripts/variables.html
Normal file
|
@ -0,0 +1,99 @@
|
|||
---
|
||||
layout: page
|
||||
category-page: scripts
|
||||
category-title: Scripting
|
||||
tags: variables, defining, deleting, naming
|
||||
author: Dario Rasic
|
||||
title: Script Variables
|
||||
---
|
||||
<!-- Intro -->
|
||||
<p>
|
||||
A variable is simply a string to which we assign a certain type of data, which could be a text, a number, a filename and other types of data.
|
||||
<br>
|
||||
<!-- How to name a variable - text -->
|
||||
<h3>Naming a variable</h3>
|
||||
<!-- Explaination -->
|
||||
To name a variable in Unix we have to use only letters, numbers or the underscore character (_).<br>
|
||||
Other characters can't be used because they have a special meaning in Unix Shell.<br>
|
||||
<br>
|
||||
<!-- Examples of naming -->
|
||||
Some simple examples are:
|
||||
|
||||
<pre>
|
||||
VAR_1
|
||||
VAR_2
|
||||
NAME_3
|
||||
name_4
|
||||
</pre>
|
||||
|
||||
<br>
|
||||
<!-- How to define a variable - text -->
|
||||
<h3>Defining a variable</h3>
|
||||
|
||||
To define a certain variable, we could use the following basecase:
|
||||
|
||||
<pre>
|
||||
variable_name=variable_value
|
||||
</pre>
|
||||
<!-- Examples of defining -->
|
||||
Let me show you a simple example:
|
||||
|
||||
<pre>
|
||||
VAR_1=Strawberry
|
||||
</pre>
|
||||
<!-- How to access the variables - text -->
|
||||
To access a variable we have to use the dollar sign ($). So if I want to access VAR_1, I have to write:
|
||||
<!-- Examples of accessing -->
|
||||
<pre>
|
||||
VAR_1=Strawberry
|
||||
echo $VAR_1
|
||||
</pre>
|
||||
And shell will give us the following result:
|
||||
<pre>
|
||||
Strawberry
|
||||
</pre>
|
||||
<br>
|
||||
<!-- How to delete a variable - text -->
|
||||
<h3>Deleting a variable</h3>
|
||||
<!-- Explaination -->
|
||||
Deleting a variable means that shell will remove a certain variable from the list of those that it tracks.<br>
|
||||
To delete a variable we use the following command:
|
||||
<!-- Examples of deleting -->
|
||||
<pre>
|
||||
unset variable_name
|
||||
</pre>
|
||||
|
||||
which in our case would be:
|
||||
|
||||
<pre>
|
||||
unset VAR_1
|
||||
</pre>
|
||||
<br>
|
||||
<!-- How to protect a variable - text -->
|
||||
<h3>Protecting variables</h3>
|
||||
<!-- Explaination -->
|
||||
To protect a certain variable, we can set them as read-only so that it can't be changed or deleted.<br>
|
||||
So, if we try to change the value of VAR_1, the result will be the following:
|
||||
<!-- Examples of protection -->
|
||||
<pre>
|
||||
VAR_1=Strawberry
|
||||
readonly VAR_1
|
||||
VAR_1=Blueberry
|
||||
</pre>
|
||||
|
||||
which will give us:
|
||||
|
||||
<pre>
|
||||
VAR_1: This variable is read only.
|
||||
</pre>
|
||||
|
||||
If we try to delete the variable, shell will give us the following value:
|
||||
|
||||
<pre>
|
||||
VAR_1=Strawberry
|
||||
unset VAR_1
|
||||
echo $VAR_1
|
||||
</pre>
|
||||
|
||||
As VAR_1 is read-only, shell will not give us any output, as you can't use the unset command with read-only files.
|
||||
</p>
|
Loading…
Reference in New Issue
Block a user