Scripting
From Systerserver
console
- the screen and keyboard which allows access and control of the server
- virtual console, a user interface for multiple computer consoles on one device
- the graphical X Window System starts in the seventh virtual console, Alt+F7
terminal emulator
- a program that substitutes for a computer console or computer terminal
- emulates a "dumb" video/grapahics terminal within some other display architecture
- though typically synonymous with a command line shell or text terminal, the term 'terminal' covers all remote terminals, including graphical interfaces
- a terminal emulator inside a graphical user interface is often called a terminal window (gnome-terminal, xterm, terminator)
launch a command
- in windows you click start -> run and enter command name
- in linux you press alt+F2 and enter command name
- in macos x you press option+space bar for spotify or use quicksilver
shell
- a program that provides an interface between a user and an operating system
- also called a shell account
- traditionally a shell account was a user account on a remote server which gave you to access a Unix shell via a command-line interface protocol such as telnet or ssh
- examples include Korn shell (ksh), C shell (csh) and Bash (Bourne-again shell)
interface
- gui vs cli
prompt
- at the blinking cursor start typing
- the focus is the cursor, also in a gui enironment
- a prompt is a cue for action
- computer is ready to receive a command
command line interface
A CLI can generally be considered as consisting of syntax and semantics.
Bash (Bourne-again shell)
- an interpreter
- a Unix shell written for the GNU Project
- /bin/sh is linked to bash and /bin/csh is linked to tcsh,
- so specifying these interpreters actually runs these compatible or improved versions
bash environment
- to make lasting changes, edit ~/.bashrc:
- VARNAME=value_of_variable;export VARNAME
- save and exit editor
- source $HOME/.bashrc
- EXAMPLES
- HISTTIMEFORMAT='%F %T '
- HISTTIMEFORMAT="%h/%d %H:%M "
- HISTCONTROL=ignoreboth (ignorespace:ignoredups)
- to make temporary changes:
- export VAR; unset VAR
- after changing the shell enivronment you need to log out and in again
- or you can "source" the file
- eg "source ~/.bashrc" or ". ~/.bashrc" (are the same)
- add the following line to the /etc/profile or %HOME/.profile
- it varies by shell, but basically the set command is used to define a variable for the current shell.
- the export command is used to define a variable as one which subshells should inherit.
- synchronizing the history across multiple sessions:
- PROMPT_COMMAND="history -a;history -c; history -r;$PROMPT_COMMAND
- When you login, the BASH shell only reads the .bash_profile file. Check that the line
- source $HOME/.bashrc
- is in your .bash_profile file. This ensures that the .bashrc file is read each time you login or start a subshell.
delete bash history
- for your current session as well as old sessions, you should do two things:
- delete the .bash_history file:
- # rm -rf ~/.bash_history
- clear the current history stored in RAM:
- # history -c
delete only one entry in the bash history
- for example because you entered a password on the command line by mistake
bash tips
- export HISTCONTROL=erasedups
- export HISTSIZE=1000
- shopt -s histappend
profile
- /etc/profile is the systemwide initialization profile file for all users.
- /etc/bash.bashrc is the systemwide per-interactive-shell startup file. This file is called from /etc/profile
- $HOME/.bash_profile is the personal initialization file, executed for login shells. Add PATH settings and other user specific variables to this file.
- $HOME/.bashrc is the individual per-interactive-shell startup file. Add user specific aliases and functions to this file.
scripting
"Scripts" are distinct from the core code of the application, which is usually written in a different language, and are often created or at least modified by the end-user. Scripts are often interpreted from source code or bytecode, whereas the applications they control are traditionally compiled to native machine code. Scripting languages are nearly always embedded in the applications they control.
The specifics of what separates scripting languages from high-level programming languages is a frequent source of debate. But generally speaking a scripting language is one which requires an interpreter.
I/O
- input output
- communication and feedback
API
- application programming interface
- linux API programming get's one even closer to the operating system than using a shell
low level programming
- machine code is in ascii, bits, zeros and one, electrical pulses (on-off)
sed
sed (stream editor) is a Unix utility which (a) parses text files and (b) implements a programming language which can apply textual transformations to such files. It reads input files line by line (sequentially), applying the operation which has been specified via the command line (or a sed script), and then outputs the line.
awk
- it's prime use is to extract data from table-like input
- ie good for using my mysql
- founders Alfred Aho, Peter Weinberger and Brian Kernighan
- gawk (GNU awk)
Pattern Matching
- wildcards
- regular expressions
Regular Expressions
- extended regular expression and basic regular expression
- are meta-characters and wildcards the same?
- wildcards gebruik je als je meerdere bestandsnamen wil aangeven
- regex gebruik je als je tekenreeksen wil aangeven
- ^ $ . [][^] - \< of>\ \
- ^ begin van de regel
- $ einde van de regel
- . wildcard naar elk willekeurig teken m.u.v. newline
- [] een van de characters
- [^] geen van deze characters
- - reeks karakters
- \< of\> begin of einde van woord, word boundry
- \ interpreteer special character niet
- herhaling in regex .... * ? {n}
- + is an extended regular expression "meta-character", but only a regular character in basic regular expressions
- grep and sed often use regex
- grep supports extended regex (ERE)
- sed only supports basic regex (BRE)
Wildcards
- wildcards in regex genereren geen bestandsnamen
- * ? [ ] ~
- double quotes - supress expansion, alleen invloed op wildcards
- single quotes - no change
- backticks are outdates, they were used for command substitution
- use $() for command substitution
- zoek naar een backslash - very difficult! and in diff environments diff
shebang
- the character sequence #!
- in a Unix-like operating system, the program loader takes the presence of these two characters as an indication that the file is a script, and tries to execute that script using the interpreter specified by the rest of the first line in the file
- for instance, shell scripts for the Bourne shell start with the first line: #!/bin/sh
- number sign plus exclamation mark, or exclamation point
- sharp or hash (and sometimes, even, mesh) to refer to the number sign (#)
- and bang to refer to the exclamation mark (!)
- hashbang, hashpling, pound bang, or crunchbang
- american slang “the whole shebang”, the lot, everything
shortcuts
- Ctrl + a Go to the beginning of the line you are currently typing on
- Ctrl + e Go to the end of the line you are currently typing on
- Ctrl + l Clears the Screen, print current line on top
- Ctrl + u Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
- Ctrl + h Same as backspace
- Ctrl + r Let’s you search through previously used commands
- Ctrl + c Kill whatever you are running
- Ctrl + d Exit the current shell (when on a new line)
- Ctrl + d Delete the letter under the cursor
- Ctrl + z Puts whatever you are running into a suspended background process. fg restores it.
- Ctrl + w Delete the word before the cursor
- Ctrl + k Cut the line after the cursor
- Alt + d Delete rest of word
- Ctrl + y Paste (d,k,u) the line after the cursor
- Alt + y Paste the the line _before_ the previous cut/copy command
- Ctrl + t Swap the last two characters before the cursor
- Alt + t Swap the last two words before the cursor
- Ctrl + _ Undo Cut, Delete, Paste, typing up to empty line (same as Ctrl + x or Ctrl + u)
- Alt + r Undo all changes to this line
- Ctrl + f Move cursor one letter forward
- Alt + f Move cursor forward one word on the current line
- Ctrl + b Move cursor one letter backward
- Alt + b Move cursor backward one word on the current line
- Tab+Tab Auto-complete files and folder names
- Esc . Insert last used argument ( same as !$ )
- !123 Insert history command 123
- EscEscEsc show current dir while typing command