Lecture 3
chmod mode file
chmod mode file
Changes file permissions
Mode consists of 3 parts:
user type
u = user
g = group
o = other
operator
'+' = add this permission
'-' = remove this permission
'=' = set permission exactly
Permission
'r' = read bit
'w'= write bit
'x' = execute bit
chmod otr file
chmod otr file
Gives others permisison to read
chmod oug=rw file
chmod oug=rw file
Make everyone's permission read and write (not execute)
chmod u=r file.txt
chmod u=r file.txt
Shell Scripts
File that contains a series of commands that we will execute as a program
eg. print date, current user, current directory
Variables
name=value (notice there are no spaces)
assigns value to variable
eg.
x=1
andx =1
are different in bash
${varname}
retrieves data stores in variable
eg.
$x
do not use
$
when assigning variablesGood practice:
${x}
eg.
echo "The cost is ${x}USD"
Command line args are stored
$1, $2, $3
In bash, all variables are strings.
Double quotes
allow variable expansion
Single quotes
supresses variable expansion
if
Statement Format
if
Statement FormatExample: Check if argument is in dictionary
Print ${1}
if it's a word in the dictionary. Otherwise, does nothing.
Example: A 'good' password should not be in the dictionary. Answer whether a word is a good password.
First way to do it:
Second way to do it:
Every program returns a status code
egrep returns 0 if a match is found, 1 if not found
In Unix: 0 is success, non-zero is failure
? stores status of most recently executed command
Syntax for doing math
Bash Loops
Example: Print numbers from 1 to ${1}
Bash For loops
Last updated