mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-06-08 10:47:13 +00:00
show line numbers in conde snippets
This commit is contained in:
parent
cd1df0e376
commit
255a68d673
82 changed files with 1249 additions and 1251 deletions
|
@ -8,7 +8,7 @@
|
|||
|
||||
[sudo vs su](https://unix.stackexchange.com/questions/35338/su-vs-sudo-s-vs-sudo-i-vs-sudo-bash/35342)
|
||||
|
||||
```bash
|
||||
```bash linenums="1"
|
||||
sudo su # login as root (user must be sudoers, root password not required) DANGEROUS
|
||||
sudo -s # act as root and inherit current user environment (env as is now, along current dir and env vars) SAFE (can modify user environment)
|
||||
sudo -i # act as root and and use a clean environment (goes to user's home, runs .bashrc) SAFEST
|
||||
|
@ -22,7 +22,7 @@ su USER # change user but don't load it's home folder
|
|||
|
||||
### Getting Info
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
man COMMAND # show command manual
|
||||
help COMMAND # show command info
|
||||
whatis COMMAND # one-line command explanation
|
||||
|
@ -35,7 +35,7 @@ id # Print user and group information for the specified USER, or (when USER omi
|
|||
|
||||
### Moving & Showing Directory Contents
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
pwd # print working (current) directory
|
||||
ls [option]... [FILE]... # list directory contents ("list storage")
|
||||
cd rel_path # change directory to path (rel_path must be inside current directory)
|
||||
|
@ -49,7 +49,7 @@ popd # return to previous directory (before pushd)
|
|||
|
||||
### Creating, Reading, Copying, Moving, Modifying Files And Directories
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
touch FILE # change FILE timestamp fi exists, create file otherwise
|
||||
cat [FILE] # concatenate files and print on standard output (FD 1)
|
||||
cat >> FILE # append following content ot file (Ctrl+D to stop)
|
||||
|
@ -84,7 +84,7 @@ cp SOURCE DESTINATION # copy SOURCE to DESTINATION
|
|||
|
||||

|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
chmod MODE FILE # change file (or directory) permissions
|
||||
chmod OCTAL-MODE FILE # change file (or directory) permissions
|
||||
|
||||
|
@ -121,7 +121,7 @@ chgrp [OPTION]... GROUP FILE... # change group ownership
|
|||
|
||||
### Finding Files And Directories
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
find [path] [expression] # search file in directory hierarchy
|
||||
find [start-position] -type f -name FILENAME # search for a file named "filename"
|
||||
find [start-position] -type d -name DIRNAME # search for a directory named "dirname"
|
||||
|
@ -134,7 +134,7 @@ find [path] -exec <command> {} \; # execute command on found items (identified
|
|||
|
||||
### Other
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
tee # copy standard input and write to standard output AND files simultaneously
|
||||
tee [FILE]
|
||||
command | sudo tee FILE # operate on file w/o using shell as su
|
||||
|
@ -171,7 +171,7 @@ watch -n SECONDS COMMAND # execute command every SECONDS seconds (no less than
|
|||
|
||||
**Data wrangling** is the process of transforming and mapping data from one "raw" data form into another format with the intent of making it more appropriate and valuable for a variety of downstream purposes such as analytics.
|
||||
|
||||
```bash
|
||||
```bash linenums="1"
|
||||
sed # stream editor for filtering and transforming text
|
||||
sed -E "s/REGEX/replacement/" # substitute text ONCE (-E uses modern REGEX)
|
||||
sed -E "s/REGEX/replacement/g" # substitute text multiple times (every match)
|
||||
|
|
|
@ -44,20 +44,20 @@ shebang indicating which interpreter to use
|
|||
|
||||
### Simple Command
|
||||
|
||||
```bash
|
||||
```bash linenums="1"
|
||||
[ var=value ... ] command [ arg ... ] [ redirection ... ] # [.] is optional component
|
||||
```
|
||||
|
||||
### Pipelines (commands concatenation)
|
||||
|
||||
```bash
|
||||
```bash linenums="1"
|
||||
command | file.ext # link the first process' standard output to the second process' standard input
|
||||
command |& file.ext # link the first process' standard output & standard error to the second process' standard input
|
||||
```
|
||||
|
||||
### Lists (sequence of commands)
|
||||
|
||||
```bash
|
||||
```bash linenums="1"
|
||||
command_1; command_2; ... # execute command in sequence, one after the other
|
||||
command_1 || command_2 || ... # execute successive commands only if preceding ones fail
|
||||
command_1 && command_2 && .. # execute successive commands only if preceding ones succeeds
|
||||
|
@ -65,7 +65,7 @@ command_1 && command_2 && .. # execute successive commands only if preceding on
|
|||
|
||||
### COMPOUND COMMANDs (multiple commands as one)
|
||||
|
||||
```bash
|
||||
```bash linenums="1"
|
||||
# block of commands executed as one
|
||||
<keyword>
|
||||
command_1; command_2; ...
|
||||
|
@ -108,7 +108,7 @@ We can copy file descriptors to make them share a stream. There are also many ot
|
|||
|
||||
### Redirections
|
||||
|
||||
```bash
|
||||
```bash linenums="1"
|
||||
[x]>file # make FD x write to file
|
||||
[x]<file # make FD x read from file
|
||||
|
||||
|
@ -142,7 +142,7 @@ x>&-, x<&- # close FD x (stream disconnected from FD x)
|
|||
|
||||
With Command Substitution, we effectively write a command within a command, and we ask bash to expand the inner command into its output and use that output as argument data for the main command.
|
||||
|
||||
```bash
|
||||
```bash linenums="1"
|
||||
$(inner_command) # $ --> value-expansion prefix
|
||||
command !* # !* expands to everything except the first argument in the previous line
|
||||
command !$ # refers to the last argument of the previous command
|
||||
|
@ -151,7 +151,7 @@ sudo !! # !! expands to the entire previous command
|
|||
|
||||
## Shell Variables
|
||||
|
||||
```bash
|
||||
```bash linenums="1"
|
||||
varname=value # variable assignment
|
||||
varname="$(command)" # command substitution, MUST be double-quoted
|
||||
"$varname", "${varname}" # variable expansion, MUST be double-quoted (name substituted w/ variable content)
|
||||
|
@ -188,7 +188,7 @@ Omit the pattern to match any character.
|
|||
|
||||
Only the final exit code after executing the entire list is relevant for the branch's evaluation.
|
||||
|
||||
```bash
|
||||
```bash linenums="1"
|
||||
if command_list; then
|
||||
command_list;
|
||||
elif command_list; then
|
||||
|
@ -207,7 +207,7 @@ fi
|
|||
|
||||
### Comparison Operators
|
||||
|
||||
```bash
|
||||
```bash linenums="1"
|
||||
[[ "$a" -eq "$b" ]] # is equal to
|
||||
[[ "$a" -ne "$b" ]] # in not equal to
|
||||
[[ "$a" -gt "$b" ]] # greater than
|
||||
|
@ -218,7 +218,7 @@ fi
|
|||
|
||||
### Arithmetic Comparison Operators
|
||||
|
||||
```bash
|
||||
```bash linenums="1"
|
||||
(("$a" > "$b")) # greater than
|
||||
(("$a" >= "$b")) # greater than or equal to
|
||||
(("$a" < "$b")) # less than
|
||||
|
@ -227,7 +227,7 @@ fi
|
|||
|
||||
### String Comparison Operators
|
||||
|
||||
```bash
|
||||
```bash linenums="1"
|
||||
[ "$a" = "$b" ] # is equal to (whitespace around operator)
|
||||
|
||||
[[ $a == z* ]] # True if $a starts with an "z" (pattern matching)
|
||||
|
@ -246,14 +246,14 @@ fi
|
|||
|
||||
## Commands short circuit evaluation
|
||||
|
||||
```bash
|
||||
```bash linenums="1"
|
||||
command_1 || command_2 # if command_1 fails executes command_2
|
||||
command_1 && command_2 # executes command_2 only if command_1 succeeds
|
||||
```
|
||||
|
||||
## Loops
|
||||
|
||||
```bash
|
||||
```bash linenums="1"
|
||||
for var in iterable ; do
|
||||
# command here
|
||||
done
|
||||
|
@ -261,7 +261,7 @@ done
|
|||
|
||||
## Script Hardening
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
set -o errexit # exit on error
|
||||
set -o nounset # fail on unset variable (bypass with ${VAR:-})
|
||||
set -o pipefail # file entire pipeline if one step fails
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue