File permissions:
- ls -al lists long form of listing alongside of hidden files, of all files in current directory
- ls -l shows:
permissions owner group size (bytes) datelastmodified filename
For permissions: it is a 10 digit string
The first character in permissions is a “type” can be - (file) and a d means (directory)
Permissions are three groups of 3 bits and when all set, are:
- user bit applies to files owner
- group bit applies to files group
- other bit applies to everyone else
For DIRECTORIES:
- r: means we can ls the directory, use globbing, and tab completion
- w: we can add or rm files from the directory
- x: means the directory can be navigated (can cd into it). If a directory’s executable bit is not set, regardless of the other read or write bits, access to the that directory, nor any file within it, are set
-
- for FILES:**
- r: can be read
- w: contents can be modified
- x: file’s contents can be executed as a program
How can we change permissions? We have a cmd for that:
The mode consists of 3 parts:
- User types:
- u = user
- g = group
- o = other
- a = all
- operator:
-
- add permissions
-
- subtract permissions
- = set permissions exactly (the equal signs not in quotes are simply relay the definitions of the symbols, will lose other permissions that prev. existed
-
- permissions
- r = read
- w = write
- x -executable
This will give group and other read and write permissions. The resulting permissions looks like:
Shell Scripts
Files containing a sequence of shell commands executed as a program
This line tells OS #!/bin/bash
is the location of the program you should use to interpret this file.
Give the file executable permissions:
cd..
means go back one directorycd../
means go back to the parent directory of the cwd
Bash can have variables
So $
with no brackets is fetch value, with bracket is subshell
NOTES:
- Use $ when fetching the value of variable
- No $ when setting a variable
- Good practice:
${x}
also means fetch the value of x- This is good practice because if I want to print “1st”
echo $xst
wont work, need echo${x}st
Ofc putting the above substitution inside of ” will not change it
LAST NOTE:
All bash variables are strings, e.g. x contains the string “1”
Occurs in the double quotes, but not in single quotes
- *Special was: **
Example: check whether a word is in the dictionary or not
In our file, we would write:
So, we egrep
any line in the dictionary (every line is just the words found in a dictionary) that starts and ends with the first command line argument.
e.g.
This prints nothing if word not found, otherwise, prints the word
Example: A good password should not be in the dictionary
-
- WE NEED TO END IF STATEMENTS WITH FI**
As seen above, a return value of 0 is success and anything other than a 0, is a fail. So in the above code snippet, as the passed password command line argument is not in the dictionary, the return value is not 0.
- *Example: ** Verify correct # of args, print how to use the program otherwise
(1) usage: write a function. good idea to make usage of a fn, in case you need # to print it multiple place
$0
is current running program
$#
is a special variable in bash, that expands to the number of arguments (positional parameters) e.g $1, $2 ...
passed to the script in question or the shell in case of argument directly passed to the shell
For all available comparisons and other conditions, see the reference sheet given on Learn.
LOOPS:
Print numnbers from 1 to $!
The loop below will rename all .cpp files to .cc
To run a file in bash, we need to give ourselves executable permissions to the file through chmod
. Then, we need ./ in front of the executable file since we need the path to the file to run it.
END OF LECTURE 3