Week 7 - Shell Scripts Part 1 Flashcards

1
Q

Basic commands

A

ls - lists contents of directory
cp - copies files
mv - moves files rename
touch - creates file
rm - deletes file
echo - prints to standard output

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

more commands

A

grep - search regular expression
less - display big files, one screen at a time (space)
pwd - print working directory
diff - see difference between 2 files
find - find file in a directory tree

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

navigation commands

A

cd - change directory
mkdir - make directory
rmdir - remove directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

echo command

A

writes arguments to standard output
- standard output = display screen
- can be changed = e.g. could be changed to a file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

find command

A

utility to search through one or more directory trees of a file system
locates files based on some user-specified criteria

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Shell Scripts

A

Series of BASH commands
Anything you can type in the command line, you can include in a script
Mainly used to automate tasls

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Shell Scripts

A

Programming Language
– Set of rules for instructing a computer how to
perform specific tasks
– C, C++, C#, Java are programming languages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

BASH Script

A

A BASH script is a list of instructions that is interpreted one instruction at a time
– we have to specify the interpreter as the first line of the script as in #!/bin/bash
- BASH is also a programming language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Compiled Languages

A

e.g. Java, C#

Program written in language and translated all at once from
source code into executable machine code
– Compiler: program which translates source code into executable program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Interpreted Languages

A

e.g. BASH, PHP
– Programmer enters instructions one at a time
– Interpreter: accepts and parses instruction, then converts to
appropriate executable instruction(s), and executes it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Script

A

Sequence of instructions read by interpreter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

BASH Shell Script

A

– Contains a sequence of BASH commands to execute line by line
– Used in troubleshooting
– Server Maintenance
– Data Backup
– Some scripts run when the Linux system starts
* Need to know how to manage these scripts if problems occur during the boot process.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Creating Shell Scripts

A

1.
Create a shell script:
– Create a file
– Write commands in sequence
(e.g. using vim)
– Assign execute permission for the file
2. Run a shell script:
– Enter the absolute or relative path to where it’s stored

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Variables

A

Scripts will need to:
1. Store information
2. Access stored information

Storage locations that have names
Name - value pair

$ notation used to retrieve value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Environment Variable

A

– Placeholder for data that can change
– Gets its value automatically from the OS startup or
the shell being used
– Each user has his or her own environment variables

common environment variables
HOME, USER, PATH, HOST

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Environment Variable
env command

A

Displays a list of all environment variables and their stored values

17
Q

Environment variable
echo command

A

echo command with $ symbol before the variable name
- display value of particular environment variable

18
Q

Shell variable

A

Similar to an environment variable
– Value is assigned in a shell script
– Related to a particular script
* Not necessarily the global environment

Three methods to store information in a variable
1. Direct Assignment
2. Prompt Method
3. Positional Parameters

19
Q

Direct assignment Variable

A

Direct assignment method
– Specify the variable’s value in the command
* Output value of variable using
echo $variable_name

20
Q

The Prompt method

A

Prompt method
– User is asked to enter a value for the variable

21
Q

Positional Parameters

A

Positional parameter method
– Parameter passed as argument to command
* command [-options] [arguments]

– Uses the order of arguments in a command to assign values to variables on the command line
– Variables from
$0 to $9 are available
* Values are defined by what the user enters

$0 represents name of script,
$1 represents first argument …. $9 represents ninth argument

22
Q

Exit Status Code $?

A

Exit status code is sent to the shell
– When you quit a program or a command
– Is updated after each command is executed
* Successful commands:
Exit Status Code = 0
* Command that fails:
Exit Status Code > 0
* Code isn’t actually displayed onscreen
– Reference it with $?

23
Q

Conditions

A

Tell interpreter to skip commands based on a
condition
* if statement
– Carry out certain commands based on testing a condition

24
Q

Common condition statements used in scripts:

A

– if statement starts the condition being tested
– then statement starts the portion of code specifying what to do if the condition evaluates to true
– else statement starts the portion of code specifying what to do if the condition evaluates to false
– fi statement indicates the end of the condition being tested

25
Q

Menu Scripts

A

Menu scripts
– Allows users to choose from a list of options
– Can create a menu script with
if and then statements
* elif statement
– Combines the else and if statements
– Create multiple conditions without closing each condition

26
Q

The case statement

A

– Uses one variable to specify multiple values and matches a portion of the script to each value
Double semicolon (;;)
– Marks the end of each code portion matching a specific value
* *) character
– Runs if the value the user enters doesn’t match any of the choices specified in the case statement