Shells

Shells and scripts - General

Make variables read-only

Define these in the system-wide environment (Bash: /etc/environment or /etc/bash.bashrc, AIX ksh: /etc/profile)

The 'readonly' and 'typeset' shell builtins may be used in both bash and ksh:

readonly VARIABLE
readonly VARIABLE=value

typeset -r VARIABLE

Listing read-only variables

typeset -r
readonly
readonly -p

Bash can use the above and 'declare' builtin which is equivalent to 'typeset'.

declare -r VARIABLE
declare -ir # applies for integers
declare -rx # export; makes variable(s) persist in child processes
declare -r

One-liners

Use OpenSSH known_hosts for SSH commands. Note: It will not work with hashed known_hosts.

awk -F, '{print $1}' .ssh/known_hosts | while read h; do ssh -n -o PubkeyAuthentication=yes -o PasswordAuthentication=no -o ConnectTimeout=3 $h hostname; done

Delete nth field's contents with awk (useful where the total number of fields is not constant)

echo "jo jo jo jo rossz" | awk '{$5=""}1'

Reuse quotes as field separator with awk (empty 10th field by keeping quotes)

echo "'jo','jo','jo','jo','rossz'" | awk -F\' '{OFS="'\''"} {$10=""}1'

FTPS upload and download with cURL - ' -k' disables SSL certificate check

curl --ftp-ssl -k -v -u user:pass domain.tld:port -T transferfile
curl --ftp-ssl -k --list-only -u user:pass domain.tld:port/path/to/file
curl --ftp-ssl -k -u user:pass domain.tld:port/path/to/file -o outfile

Avoid grep finding itself in the output of ps ('pidof' is lame, pgrep is not available on AIX) Feel free to format with -o

ps -ef | grep "[t]omcat"

Alternatively, grep just for the commands without their arguments

ps -eo pid,comm | grep tomcat

Copy your environment to other servers

tar -cDpf - .hushlogin .kshrc .profile .ssh/ .ssh/config .ssh/authorized_keys | ssh $host 'tar -xpf -'

Here, the trick is tar -D which is used to skip 'mkdir .ssh' but avoiding to copy the entire .ssh/ directory. Of course, copying evertything to a separate dir and then running scp -r from there is just as good.

Linux and GNU ls: rename Panasonic .MTS video files to .mp4 with an added timestamp (example: 00027.MTS => 20141018-1720-00027.MTS.mp4).

Optionally, you can remove "MTS" from the filename.

cd /dir
ls -l --time-style="+%Y%m%d-%H%M- " *.MTS | while read m i u g s mtime file; do [ "$mtime" -a "$file" ] && echo "mv $file $mtime$file.mp4"; done
ls -l --time-style="+%Y%m%d-%H%M- " *.MTS | while read m i u g s mtime file; do [ "$mtime" -a "$file" ] && mv $file $mtime$file.mp4; done

Korn shell (mostly AIX ksh88)

...

GNU Bash (Bourne-Again Shell)

There are complete references, these are just a few I'm actually using (and keep forgetting ;-).

Built-in variables

The text form is listed to make search easier. See also: man bash - Special Parameters

Setting and listing options

List functions by name

declare -F

List functions (expanded)

declare -f

List builtin commands and their status

enable -a

List shell options and their status

shopt -s

---

Enable case insensitive globbing (completion)

shopt -o nocaseglob

Shortcuts, key bindings

Search in history

Ctrl r

Search next match

Ctrl r (repeatedly)

Delete whole word backwards

Esc Backspace

Repeat last argument from the previous line

Esc .

Coin flipping

[ $(( $RANDOM % 2 )) -eq 0 ] && echo Heads || echo Tails

Invocation, profile files

INVOCATION

A login shell is one whose first character of argument zero is a -, or one started with the --login option.

An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to terminals (as determined

by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.

Login shell

/etc/profile

~/.bash_profile

~/.bash_login

~/.profile

When a login shell exits, bash reads and executes commands from the file

~/.bash_logout

if it exists.

Interactive shell (which is not a login shell) and stdin connected to network

/etc/bash.bashrc

~/.bashrc

Non-interactive shell (script etc)

BASH_ENV

Bash behaves as if the following command were executed:

if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi

Bourne shell (sh)

/etc/profile

~/.profile

When invoked as an interactive shell with the name sh, bash looks for the variable

ENV

expands its value if it is defined, and uses the expanded value as the name of a file to read and execute.

A non-interactive shell invoked with the name sh does not attempt to read any other startup files.

---

To have a nice output with set -x:

export PS4=' +${SECONDS}:${0}[${LINENO}]: '

Other

Emacs-style keybindings

Ctrl-A Go to left edge of window.

Ctrl-B Cursor left, wrapping to previous line if appropriate.

Ctrl-D Delete character under cursor.

Ctrl-E Go to right edge (stripspaces off) or end of line (stripspaces on).

Ctrl-F Cursor right, wrapping to next line when appropriate.

Ctrl-G Terminate, returning the window contents.

Ctrl-H Delete character backward.

Ctrl-J Terminate if the window is 1 line, otherwise insert newline.

Ctrl-K If line is blank, delete it, otherwise clear to end of line.

Ctrl-L Refresh screen.

Ctrl-N Cursor down; move down one line.

Ctrl-O Insert a blank line at cursor location.

Ctrl-P Cursor up; move up one line.

Source: http://codespeak.net/svn/pypy/dist/lib-python/2.5.2/curses/textpad.py