Difference between revisions of "Introduction to Linux in HPC/Shell scripting"

From HPC Wiki
Introduction to Linux in HPC/Shell scripting
Jump to navigation Jump to search
m (Tweak page sorting)
 
(30 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
[[Category:Tutorials|Shell Scripting]]<nowiki />
 +
{{DISPLAYTITLE:Shell Scripting}}<nowiki />
 +
{{Syllabus Introduction to Linux}}<nowiki />
 
__TOC__
 
__TOC__
 +
 +
In this video the concept of shell scripting is introduced. Commands entered into the console can also be written into a text file to create an executable script. The tutorial will describe how to create such a script, explain its basic structure and how to execute it in the console.
  
 
=== Video === <!--T:5-->
 
=== Video === <!--T:5-->
  
<youtube width="600" height="400" right>IfD9IPixgpo</youtube>
+
<youtube width="600" height="340" right>aNybqywxt9Y</youtube>
  
[https://git-ce.rwth-aachen.de/hpc.nrw/ap2/tutorials/linux/-/blob/master/Slides/Linux_Intro/Linux_Intro.pdf Linux Introduction] Slides 102 - 114 (13 pages)
+
([[Media:HPC.NRW_Introduction_to_Linux_in_HPC_09_Shell_scripts.pdf | Slides as pdf]])
  
 +
=== Quiz ===
  
=== Slide Layout === <!--T:5-->
+
{{hidden begin
 
+
|title = Which command rund the executable script "test.sh" located in your home folder?
 
+
}}
 
+
<quiz display=simple>
    page 1:
+
{
        Linux shell script is a text file containing a series of commands.
+
|type="[]"}
        Advantage: automate tasks.
+
-  <code>~$ test.sh</code>
        Execute a shell script is like a program.
+
||
    page 2:
+
+  <code>~$ ./test.sh</code>
        How to execute a shell script?
+
|| Explanation: The <code>./</code> refers to the current directory.
        Safety feature: not everyone can run everything.
+
+  <code>~$ ~/test.sh</code>
        Script needs execute permissions.
+
|| Explanation: The <code>~/</code> refers to the path of your home directory. This is equivalent to the full path of <code>/home/<USERNAME>/</code>.
    page 3 - 8:  
+
</quiz>
        animation for example shell script
+
{{hidden end}}
            shebang
 
            comment
 
            echo
 
            list of commands
 
            line break
 
    page 9:
 
        Command line arguments for shell script
 
        Loops and if statements in shell script
 
    page 10:
 
        What shell script is good for?
 
        What shell script is not good for?
 
        Alternatives to bash.
 
    page 11:
 
        How to define a bash variable?
 
        How to use a defined bash variable?
 
    page 12:
 
        Environment variables are defined and available for a process or a shell.
 
        Example: $HOME
 
        Advantage: different computers may set up different values for an environment variable.
 
    page 13:  
 
        Predefined environment variables: $USER etc.
 
        env command
 
        export command to define your own environment variables.
 
 
 
 
 
 
 
=== Quiz === <!--T:5-->
 
 
 
  
{{hidden begin  
+
{{hidden begin
 
|title = Which bash command below assigns "value" to variable var?
 
|title = Which bash command below assigns "value" to variable var?
 
}}
 
}}
Line 62: Line 40:
 
||  
 
||  
 
-  <code>var=="value"</code>
 
-  <code>var=="value"</code>
||  
+
||
 
</quiz>
 
</quiz>
 
{{hidden end}}
 
{{hidden end}}
  
 +
{{hidden begin
 +
|title = What do you have to add to the header of a script to make it an '''executable''' bash script?
 +
}}
 +
<quiz display=simple>
 +
{
 +
|type="()"}
 +
- <code>#BASH</code>
 +
||
 +
- <code>/bin/bash</code>
 +
||
 +
+ <code>#!/bin/bash</code>
 +
|| Explanation: This is referred to as the ''shebang''.
 +
</quiz>
 +
{{hidden end}}
  
{{Warning|mode=info|text= '''no tips in this section'''}}
+
{{hidden begin
 +
|title = How can you de-reference the second command-line argument in a script when calling <code>~$ ./test.sh arg1 arg2 arg3</code>?
 +
}}
 +
<quiz display=simple>
 +
{
 +
|type="()"}
 +
- <code>$arg2</code>
 +
||
 +
- <code>$1</code>
 +
||
 +
- <code>$arg1</code>
 +
||
 +
+ <code>$2</code>
 +
|| Explanation: Command-line arguments start to count at 0, however the 0th element is the script-name itself therefore the second argument is arg2 in this case which can be accessed/de-referenced in the script with <code>$2</code>
 +
- <code>$0</code>
 +
||
  
{{Warning|mode=warn|text= '''no warnings in this section'''}}
+
</quiz>
 +
{{hidden end}}
  
=== Exercises in Terminal (slides 115 - 116) and solution (slide 117) === <!--T:5-->   
+
=== Exercises in Terminal === <!--T:5-->   
  
  
  1. a. Write a script that
+
  1. Write a script that:
     b. Prints an environment variable
+
     a. Prints an environment variable
     c. Saves the output of the date command to a variable
+
     b. Saves the output of the '''date''' command to a variable
     d. Sleeps briefly
+
     c. Sleeps for 2 seconds
     e. Prints the new and old date and time
+
     d. Prints the new and old date and time
 
   {| role="presentation" class="wikitable mw-collapsible mw-collapsed"
 
   {| role="presentation" class="wikitable mw-collapsible mw-collapsed"
 
     | <strong>Answer:</strong>
 
     | <strong>Answer:</strong>
Line 177: Line 185:
 
     |}
 
     |}
  
  6. create an shell variable MYIDENTITY and export it as below:  
+
  6. Create an shell variable MYIDENTITY and export it as below:  
 
     <code>$ export MYIDENTITY=whoami</code>
 
     <code>$ export MYIDENTITY=whoami</code>
 
     How will you list the shell variable MYIDENTITY?
 
     How will you list the shell variable MYIDENTITY?
Line 197: Line 205:
 
       The shell variable upon execution runs the command <code>whoami</code>, which is assigned to it. <code>whoami</code> command prints the user name of the effective user ID
 
       The shell variable upon execution runs the command <code>whoami</code>, which is assigned to it. <code>whoami</code> command prints the user name of the effective user ID
 
     |}
 
     |}
 +
 +
 +
{{Tutorial Navigation
 +
| previous = [[Introduction_to_Linux_in_HPC/The_vim_text_editor | The vim Text Editor ]]
 +
| main = [[Introduction_to_Linux_in_HPC | Overview ]]
 +
| next = [[Introduction_to_Linux_in_HPC/Environment_variables | Environment Variables ]]
 +
}}

Latest revision as of 17:24, 4 December 2020

Tutorial
Title: Introduction to Linux in HPC
Provider: HPC.NRW

Contact: tutorials@hpc.nrw
Type: Multi-part video
Topic Area: HPC Platforms
License: CC-BY-SA
Syllabus

1. Background and History
2. The Command Line
3. Linux Directory Structure
4. Files
5. Text display and search
6. Users and permissions
7. Processes
8. The vim text editor
9. Shell scripting
10. Environment variables
11. System configuration
12. SSH Connections
13. SSH: Graphics and File Transfer
14. Various tips

In this video the concept of shell scripting is introduced. Commands entered into the console can also be written into a text file to create an executable script. The tutorial will describe how to create such a script, explain its basic structure and how to execute it in the console.

Video

( Slides as pdf)

Quiz

Which command rund the executable script "test.sh" located in your home folder?

~$ test.sh
~$ ./test.sh
~$ ~/test.sh

Which bash command below assigns "value" to variable var?

var="value"
var = "value"
var=="value"

What do you have to add to the header of a script to make it an executable bash script?

#BASH
/bin/bash
#!/bin/bash

How can you de-reference the second command-line argument in a script when calling ~$ ./test.sh arg1 arg2 arg3?

$arg2
$1
$arg1
$2
$0

Exercises in Terminal

1. Write a script that:
   a. Prints an environment variable
   b. Saves the output of the date command to a variable
   c. Sleeps for 2 seconds
   d. Prints the new and old date and time
2. Find a way to execute a script without setting execute permissions.
3. Find out how to do other programming things in bash (e.g. functions, classes). How convenient do they look?
4. Look at different ways you can define if conditions.
5. Find out what different types of quotes (single ' vs. double ") do?
6. Create an shell variable MYIDENTITY and export it as below: 
   $ export MYIDENTITY=whoami
   How will you list the shell variable MYIDENTITY?
   Execute the shell variable MYIDENTITY, what is the output? 



<< The vim Text Editor

Overview

Environment Variables >>