More reference - Scripting (Tag)
Shell Environment
1 - Shell Config
Global & Local vars
- Global - Available from current shell & any child processes spawned from the shell
-
printenv
- Show all global env vars 📌 -
export
- make var available to all programs launched from the shell
-
- Local - Only available in the shell that creates it
-
set
- Show all env vars for a specific process -
env
- set var for just one program - Recommend using lowercase letters (distinguish from system env vars)
-
1 | # var is available to all |
Common env vars
Note: For multiple X sessions,
DISPLAY
always starts with0:0
for the first session, then1:0
Variable | Explanation |
---|---|
SHELL |
Path to current shell |
HOSTNAME |
Current hostname |
PS1 |
Default bash prompt |
TERM |
Current terminal type (e.g. xterm ) |
DISPLAY |
Identify X display. Usually 0:0 |
3 Ways to start Bash shell
- At login time - As default login shell
- Interactive shell (launched via Terminal GUI)
- Non-interactive shell - To run a script
Shell config files
- Global
- Login -
/etc/profile
(main default startup file) - Interactive -
/etc/bashrc
- Login -
- User
- Login -
~/.bash_profile
- Interactive -
~/.bashrc
- Login -
~/.inputrc
- Customize keyboard config- X uses its own keyboard config. So this doesn’t affect programs running in X
Shell Scripting
1 - General
Notes
Piping - Redirect output to another command
Run script directly from command prompt - New subshell
Make local shell env vars availbale in the script 📌
1
exec ./xx.sh
Shebang / hashbang
-
#!
- Tells Linux it’s a script -
/bin/bash
- Path to interpreter to run the shellscript (e.g. Bash)
Source a script
- Run in the current shell. Script has access to env vars defined in the calling shell. 📌
-
source ~/.bashrc
-
. ~/.bashrc
Shell Variables
$0
- Script name$$
- PID of current shell$?
- Exit status of last command (e.g.0
)1
2# change exit status (from 0 to 120)
exit 120
Command-line Arguments
1 | # example - test.sh |
Command Substitution
- Assign the command’s output to a user var in the script
- Can also capture function output using
$()
📌
1 | # use `` |
File Descriptor
- Non-negative number
-
0
-STDIN
(keyboard) -
1
-STDOUT
-
2
-STDERR
Read input
Silent reading - Text is displayed, but sets text color same as terminal background color 📌
1 | # read & use input |
Running multiple commands
1 | # use ; |
List all active aliases
1 | alias -p |
2 - Synatx
Math
- Floating point arthmetic is controlled by a built-in var -
scale
(set to desired number of decimals) 📌 -
zsh
- Supports advanced math functions & features
1 | # $[] - int only |
Condition Tests - Numeric
Test - If [ … ] | Description |
---|---|
n1 -eq n2 |
n1 == n2 |
n1 -ne n2 |
n1 != n2 |
n1 -ge n2 |
n1 >= n2 |
n1 -gt n2 |
n1 > n2 |
n1 -le n2 |
n1 <= n2 |
n1 -lt n2 |
n1 < n2 |
Condition Tests - File
Test - If [ … ] | Description |
---|---|
-e [file] |
If file exists |
-f [file] |
If file exists & is a file |
-d [file] |
If file exists & is a directory |
-s [file] |
If file exists & not empty |
-r [file] |
If file exists & readable |
-w [file] |
If file exists & writeable |
-x [file] |
If file exists & executable |
Logics - Combine tests with boolean symbols
-
&&
- And -
||
- Or
If
1 | if [ ... ] |
Case
1 | # input is 1 char, Y or N |
For
seq
- Similar torange()
, but is[a, b]
(闭区间)
1 | # execute 4 times, default interval = 1 |
While
1 | while [ ... ] |
Function
Use
return
to specify single int value to define exit status
1 | # Format 1 |
Example - Putting it altogether
Accepts source & target filename. Aborts when target file exits
1 |
|
3 - SQL Queries
All Syntax is used in MySQL
Update
1 | UPDATE objects SET size=5 |
Exact Matches
1 | SELECT * FROM objects |
Multiple Tests
Return an ordered list
1 | SELECT * FROM objects |
Deletion
1 | # delete all data from a table (keeps the table) |
Other Syntax
-
JOIN
- combine data from multiple tables -
GROUP BY
- Used with math operators, e.g.SUM()
1 | SELECT objects.name, objects.value, SUM(value) |