Variabile: Atribuire - Greeting "Hello" Echo $greeting
Variabile: Atribuire - Greeting "Hello" Echo $greeting
Variabile: Atribuire - Greeting "Hello" Echo $greeting
Permission denied – change permissions : chmod u+x file (executable only for the user)
PATH = “$PATH:~/bin”
important!
Standard Streams
Pipes: ls | grep x
Redirect: cmd < inputfile > outputfile // >outputfile cmd < inputfile
Control flow
4. CASE
case WORD in
PATTERN1)
Code for pattern1;;
PATTERN2)
Code for pattern2);;
….
PATTERNn)
Code for pattern n);;
esac
// multiple patterns separated by |
Exemple:
Group commands
1. {} – a single statement {cm1; cmd2; cmd3}
2. || and &&
a) && - will execute next statement only if previous one succeeded
b) || - will execute next statement only if previous one failed
****
Variables
Integer variables: declare –(+)i num : EX: declare –i p; p=”4+5”; echo $p => 9
Arithmetic Expressions: let command: let n=100/2
((++x))
Read-only variables: declare –r constant=”some value”
Exporting variables: export var=”value” / declare –x var=”value”
Special Variables
1. Positional Parameters: hold the n-th command line argument: $1, $2, etc.; ${10}
2. $0: holds name of the script as it was called
3. $@ ~ $*: All (Equivalent to $1 $2 $3…$N)
4. $#: Holds the number of arguments passed to the script
5. Shift: removes the first argument: $2 -> $1; $3 -> $2
Shift 3 – removes the first three arguments
6. Getopts: getopts optstring name, where optstring=a list of expected options
It will place the next option into $name
Handling errors: - unknown option: “?” in option variable NAME; actual option in
OPTARG
- Missing option argument: “:” in option variable NAME; actual
option in OPTARG
Functions
Removing a pattern
Removing part of a string:
${var#pattern} – Removes shortest match from begin of string
${var##pattern} – Removes longest match from begin of string
${var%pattern} – Removes shortest match from end of string
${var%%pattern} – Removes longest match from end of string
2. Shopt
-can set many options with –s, unset with –u
o Shopt –s nocaseglob : ignore case with pathname expansion
o Shopt –s extglob: enable extended pattern matching
o Shopt –s dotglob: include hidden files with pathname expansion
RED HAT
~=$HOME
./ = Current Directory
INPUT : READ COMMAND
Press any key: read –n1 –p “Press any key when you’re ready to see the answer”
“Silent mode” (don’t see the key pressed on the screen): read –sn1 –p “Press any key when
you’re ready to see the answer”
**** the key pressed that isn’t show on the screen is stored in default variable : echo $REPLY
-e : File exists
-b : Block device files
-c : Char device files
-f : Regular files
-r : Read access
-w : Write access
-x : Execute permission
-s : Same as –x
WHILE/UNTIL
BREAK/CONTINUE
BREAK –Code after Break command is ignored. (next command – after done)
Continue - Code after Break command is ignored. (next command – back to the loop (while))
Scheduling a Script
Cron – [Misses jobs if the system is down] - Minute, Hour, Day of Month, Month, Day of Week
Anacron – [Doesn’t miss jobs if the system is down] – Can only run jobs once per day!!!
Exist Statuses
xtrace