Scripting

From Systerserver

Jump to: navigation, search

there is a fine line between a bash command, unix program, scripting and programming!

Contents

console

  • the screen and keyboard which allows access and control of the server
  • terminal emulator, a program that substitutes for a computer console or computer terminal
  • 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 terminal emulator is a program that emulates a "dumb" video/grpahics 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.

shell

  • a piece of software 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) and C shell (csh)

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 exlamation mark, or exclamation point
  • sharp or hash (and sometimes, even, mesh) to refer to the number sign
  • and bang to refer to the !
  • hashbang, hashpling, pound bang, or crunchbang
  • american slang “the whole shebang”, the lot, everything

command line interface

A CLI can generally be considered as consisting of syntax and semantics.

Bash

  • an interpreter, a command line interpreter
  • a Unix shell written for the GNU Project
  • its name is an acronym which stands for Bourne-again shell
  • /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.

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