Week 7 - Shell Scripts Part 1 Flashcards
Basic commands
ls - lists contents of directory
cp - copies files
mv - moves files rename
touch - creates file
rm - deletes file
echo - prints to standard output
more commands
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
navigation commands
cd - change directory
mkdir - make directory
rmdir - remove directory
echo command
writes arguments to standard output
- standard output = display screen
- can be changed = e.g. could be changed to a file
find command
utility to search through one or more directory trees of a file system
locates files based on some user-specified criteria
Shell Scripts
Series of BASH commands
Anything you can type in the command line, you can include in a script
Mainly used to automate tasls
Shell Scripts
Programming Language
– Set of rules for instructing a computer how to
perform specific tasks
– C, C++, C#, Java are programming languages
BASH Script
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
Compiled Languages
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.
Interpreted Languages
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.
Script
Sequence of instructions read by interpreter
BASH Shell Script
– 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.
Creating Shell Scripts
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
Variables
Scripts will need to:
1. Store information
2. Access stored information
Storage locations that have names
Name - value pair
$ notation used to retrieve value
Environment Variable
– 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