420-N23_Operating_Systems_-_Topic_14_-_Scripting_I (1)
420-N23_Operating_Systems_-_Topic_14_-_Scripting_I (1)
./script.sh #Runs it
Comments on previous script
# is a comment.
./ runs the script.
Why? Because when we issue a command to run, the bash shell looks at your PATH, NOT your
immediate surrounding!
Try it: echo $PATH
The ./ command means:
◦ Dot : Current directory
◦ Slash : “In the Current Directory”
Alternative: Use absolute path to your script. ~/scripts/myscript.sh
Variables
When referring to or reading a variable we place a $ sign before the
variable name.
When setting a variable we leave out the $ sign.
It is suggested to use “camelCase” to name your variables. Otherwise
consider using lowercase words separated by underscores.
this_is_a_variable
A variable may be placed anywhere in a script (or on the command
line for that matter) and, when run, Bash will replace it with the value
of the variable. This is made possible as the substitution is done
before the command is run.
Variables …
Remember to use quotes when assigning complex strings with spaces.
Also remember not to use spaces AT ALL.
my_variable=“This is a string”
my_variable2=This is another string
We were able to echo “The meaning of life is …” without quotes but that is ECHO not variable
assigning!
Single and double quotes
Just like in PHP, we treat single and double quotes differently.
“Things in here are PROCESSED”
‘Things in here are NOT PROCESSED”.
var1=“ABC”
echo “The value is $var1” # The value is ABC
echo ‘The value is $var1’ # The value is $var1
Concatenating strings
Method 1
foo="Hello"
foo="$foo World"
echo $foo
Method 2
a='hello'
b='world'
c=$a$b
echo $c
Running commands WITHIN ECHO
METHOD 1
Anything between the "tick" characters is executed like a real command and it's output is
returned.
You can run a command in echo like this:
myCommand=$( date )
Echo “The date is : $myCommand”
Exercise 1
Write a your first program.
Set two variables; $name and $favouriteNumber
Name = Your Name
Favourite Number = 42
Display the line “Hello, my name is Brendan and my favourite number is 42.”
Remember to set the bash type, and the execution bits.
Special Variables
$0 - The name of the Bash script.
$? - The exit status of the most recently run process.
$$ - The process ID of the current script.
$USER - The username of the user running the script.
$HOSTNAME - The hostname of the machine the script is running on.
$SECONDS - The number of seconds since the script was started.
$RANDOM - Returns a different random number each time is it referred to.
$LINENO - Returns the current line number in the Bash script.
num_three=$((1+2))
num_whatever=$((num1 + num2))
#!/bin/bash
# A simple copy script
cat $1 $2 >> result_$1
# Let's verify the copy worked
echo file $1 concatenated to $2 successfully.
Exercise 2a
Rewrite exercise 1 using parameters.
I can run it like this:
./myscript.sh Brendan 42
Question: How can I pass my full name to the script? Brendan Wood?
Pretend it’s a perfect world and people will enter EXACTLY 2 numbers.
Exercise 3 (Practice question)
Tell me how many files are in the current directory (in a script) by
using commands in a variable?
Hint:
wc will count lines.
ls –1 will output n lines.
Class Question
What will this output?
1) I log into Linux
2) I write this:
◦ myName=“Brendan”
Export
◦ Makes variables available to any children of this shell
Readonly
◦ Any program other than this shell can only read the value and not modify it
More special variables
$HOME Your home directory
$PATH The path we look for executable programs or scripts
$PS1 Your command prompt string
$PWD Current working directory
$SHELL Full path to your shell
Console IO
Read
◦ Read a value from the keyboard
This is beyond the scope of the course, but you can use printf to print using automatic formatting and
variable placement.
Result: 2 files
file1.txt
018-04-02_22:49:11_file1.txt
*
**
***
****
*****
IF statement
if [ <test> ] if [ $1 -gt 100 ]
Then then
<commands> echo A large number.
else else
<more commands> echo Not a large number.
fi fi
IF TESTS
! EXPRESSION The EXPRESSION is false.
-n STRING The length of STRING is greater than zero.
-z STRING The lengh of STRING is zero (ie it is empty).
STRING1 = STRING2 STRING1 is equal to STRING2
STRING1 != STRING2 STRING1 is not equal to STRING2
INTEGER1 -eq INTEGER2 INTEGER1 is numerically equal to INTEGER2
INTEGER1 -gt INTEGER2 INTEGER1 is numerically greater than INTEGER2
INTEGER1 -lt INTEGER2 INTEGER1 is numerically less than INTEGER2
-d FILE FILE exists and is a directory.
-e FILE FILE exists.
-r FILE FILE exists and the read permission is granted.
-s FILE FILE exists and it's size is greater than zero (ie. it is not empty).
-w FILE FILE exists and the write permission is granted.
-x FILE FILE exists and the execute permission is granted.
If..elif..else..fi
If [ conditional expression1 ]
then
statement1
statement2
elif [ conditional expression2 ]
then
statement3
statement4
else
statement5
fi
Final LAB CHALLENGE (3)
Using while loop(s) do this:
Loop from 0 to 100