- both stdout and stderr file dirlist: ls > dirlist 2>&1
- redirecting input from myfile to descriptor n: n<myfile
- appending: n>>file
- redirecting stdout and stderr: &>word or >&word
- appending stdout and stderr: &>>word
- close stdin: 1<&- close stdout: 2<&-
- duplicating input file descriptor n is a copy of word: n<&word
- duplicating output file descriptor n is a copy of word: n>&word
- movin input file descriptor: n<&digit- (from digit to n)
- movin output file descriptor: n>&digit- (from digit to n)
- opening myfile descriptor for read and write: n<>myfile
exec 1<&- (close stdout)
exec 2<&- (close stderr)
exec 1<>$logfile #moving stdin to logfile; read write from
exec 2>&1 #stderr redirected to stdout
exec 3>fileA
ls >&3 #result goes to fileA
exec 3>&- #close filedesc 3
#!/usr/bin/bash
# will execure cleanup in any case of exiting
#
function cleanup {
# cleanup before exiting this script
}
trap cleanup EXIT
.... lot of useful script lines.
#
#FIN
#!/bin/sh
LOOP=12
while test $LOOP -lt 20
do
LOOP=`expr $LOOP + 1`
echo $LOOP
done
- TOOLS
- 'htop' instead of 'top'
- Use 'apt-file' to see which package provides that file you're missing
- 'dict' is a commandline dictionary
- 'echo start_backup.sh | at midnight' starts a command at the specified time
- Pipe any command over 'column -t' to nicely align the columns
- Google 'magic sysrq' and learn how to bring you machine back from the dead
- 'diff --side-by-side fileA.txt fileB.txt | pager' to see a nice diff
- if you liked the 'psgrep' alias, check 'pgrep' as it is far more powerful
- never run 'chmod o+x * -R', capitalize the X to avoid executable files. If you want _only_ executable folders: 'find . -type d -exec chmod g+x {} \;'
- 'xargs' gets its input from a pipe and runs some command for each argument
- run jobs in parallel easily: 'ls *.png | parallel -j4 convert {} {.}.jpg'
- NETWORKING
- 'socat TCP4-LISTEN:1234,fork TCP4:192.168.1.1:22' forwards your port 1234 to another machine's port 22. Very useful for quick NAT redirection.
- Some tools to monitor network connections and bandwith:
- 'lsof -i' monitors network connections in real time
- 'iftop' shows bandwith usage per *connection*
- 'nethogs' shows the bandwith usage per *process*
- ssmtp can use a Gmail account as SMTP and send emails from the command line. 'echo "Hello, User!" | mail user@domain.com' ## Thanks to Adam Ziaja.
Configure your /etc/ssmtp/ssmtp.conf:
root=***E-MAIL***
mailhub=smtp.gmail.com:587
rewriteDomain=
hostname=smtp.gmail.com:587
UseSTARTTLS?=YES
UseTLS?=YES
AuthUser?=***E-MAIL***
AuthPass?=***PASSWORD***
AuthMethod?=LOGIN
FromLineOverride?=YES
Retrieved from http://mmb.pcb.ub.es/~carlesfe/unix/tricks.txt