OPS145 Lab 9 Newversion: Difference between revisions

From Littlesvr Wiki
Jump to navigation Jump to search
(Replaced content with "The latest version is here: OPS145_Lab_9 Category:OPS145")
Tag: Replaced
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Bash scripting =
The latest version is here: [[OPS145_Lab_9]]
Bash is the shell you've been using in this course to run Linux commands. Bash is also a programming language. It's a special purpose programming language, not something you would write a graphical application in. What you do in a bash script is essentially the exact equivalent of what you would run at a terminal prompt - except you can run all your commands at once, instead of one at a time.
 
A bash script is a plain text file. The bash programming language is interpreted (as opposed to compiled) - meaning the code you write doesn't need to be compiled before you can execute it.
 
= Setup =
The setup for writing a bash script is minimal. You'll need to:
 
# Create the script in a plain text editor: either graphical or on the command line. Usually you save it with a '''.sh''' extension, though technically you don't have to.
# Make sure you (and anyone else you want to allow to execute the script) have '''read''' and '''execute''' permissions for the file.
# Add a '''shebang''' line at the top.
 
== Permissions ==
Since bash scripts are interpreted (rather than executed outright): you can't actually execute a bash file. In order to execute the commands in a bash script: they need to be read, and interpreted, and executed, by the bash program.
 
That's why just giving a script execute permissions may not be enough to run it. You need to give yourself read permission, so that the bash program can read your script and execute it.
 
In fact you don't even need execute permissions to run a bash script. You can run '''bash''', and give it the name of the script as the first argument.
 
== Shebang line ==
Because bash scripts are interpreted, and extensions are mostly ignored in Linux: the shell you're using to execute your script needs to know what kind of script it is. There are many interpreted programming languages. If you don't make it clear what language your script is written in: there's a chance it will be misinterpreted.
 
A shebang line for a bash script looks like this:<syntaxhighlight lang="bash">
#!/bin/bash
 
</syntaxhighlight>It has the be the first line in your script.
 
Anything following this line is regular bash.
 
= hello.sh =
Decide for yourself whether you can handle the bash learning in this lab while using vi. If you feel that's too hard: you can use a graphical text editor.
 
* Create the '''lab9''' directory inside your home directory.
* Open a text editor and save an empty file into '''~/lab9/hello.sh'''
* Add the '''shebang''' line at the top
* Look at the permissions for your file in a terminal. You'll find that by default you have read and write permissions, but not execute permissions. Give yourself execute permissions using one of these two commands<syntaxhighlight lang="bash">
chmod a+x lab9.sh # Add execute permissions for everyone, or:
chmod 755 lab9.sh # Set permissions to exactly this
 
</syntaxhighlight>
*Run the script by specifying an absolute path, a relative-to-home path, or if your PWD is right: you can use the '''.''' special character as a shortcut:<syntaxhighlight lang="bash">
/home/yourusername/lab9/first.sh # Run first.sh using an absolute path
~/lab9/first.sh # Run first.sh relative-to-home path
./first.sh # Run first.sh from the PWD
</syntaxhighlight>
Your script doesn't do anything yet: but if you get any errors: the script might be in the wrong place, or it might have the wrong permisions, or you're not executing it correctly.
 
== echo ==
echo is an interesting command. Initially it may appear to do nothing of use, but with better understanding of how programming works it will make a lot more sense.
 
The echo command '''prints some output''' (via STDOUT) - whatever output you tell it to print.
 
For example: if you run this in a terminal:<syntaxhighlight lang="bash">
echo Hello
</syntaxhighlight>it will print the word Hello.
 
If you give echo multiple arguments: it will print them all, with one space in between each of them:<syntaxhighlight lang="bash">
echo Hello    there # Note the multiple spaces
</syntaxhighlight>Remember that in bash the arguments for a command are whitespace-separated. So it doesn't matter how many spaces you put between arguments to echo: they are still interpreted as separate arguments.
 
If you want to include multiple words and all the spacing between them in echo's output: combine them all into a single argument but enclosing the entire string of text into quotes:<syntaxhighlight lang="bash">
echo "Hello    there" # Note the multiple spaces
</syntaxhighlight>The echo command can be used for more complicated things, but this is all we need for this lab.
 
* Add a line to your shell script so that when you run the script: it will print: "Hello. I will now do a bunch of stuff". It should look like this when you run it:
 
[[File:Hello.sh-1.png|center]]One of the main reasons shell scripts are exceptionally useful is that once you get your script to work: you don't need to worry about typos, command syntax, or even remembering how exactly the commands work.
 
The other big reason is: you don't have to retype your commands every time you want to run them.
 
This simple echo program is a great example of that. You can already see that typing the command to run the script is much shorter than the one echo line inside the script. Obviously the longer the script: the greater the chances of mistakes, and the more you'd need to type when you wanted those commands executed.
 
== date ==
 
= Also =
 
* echo
* date
* temp files
* script2:
** delete directory
** create directory tree
* script3 extended from script2:
** fill up tree with files
** create tarball
* <br />
 
=Submit evidence of your work=
 
After you finish the lab: run the following commands to submit your work:<syntaxhighlight lang="bash">
cd ~
wget http://ops345.ca/check/ops145-lab9-check.sh # Download the check script
chmod 700 ops145-lab9-check.sh # Make the downloaded file executable
./ops145-lab9-check.sh # Run the check script
</syntaxhighlight>If it says "Your lab9 has been submitted": make a screenshot, and you're done. If it gives you any warnings or errors: you have to fix them and try the ./ops145-lab7-check.sh command again.


[[Category:OPS145]]
[[Category:OPS145]]

Latest revision as of 12:58, 27 March 2024

The latest version is here: OPS145_Lab_9