This is an old revision of the document!
Table of Contents
Unix/Linux
How to automatically download and process email attachments from Gmail
Useful references
Eventually, after months of trying different things, came up with this combination of tools that worked very well.
Install tools
0@@
- Set Gmail account up for POP and disable IMAP
Configure fetchmail
This is the part that talks to Gmail and downloads the emails to the local machine.
1@@
If fetchmail complains about ssl certificates, try specifying wherein is with the sslcertfile line
Configure maildrop
2@@
Not really sure why this filter is not working properly. The cc works but the To: check doesn't. So I have a shell that runs separately to process the emails in the backup mail directory.
uudeview
This is a clever program that can extract attachments from emails and put them in a directory of you choosing.<br /> This shell runs from cron at regular intervals to run the whole process
3@@
Use sed to repeat characters
echo an '=' sign then use the loop feature of sed to run round a loop a further 79 times replacing what you've got with the same thing plus and extra one!
4@@
Convert between lower and upper case using ansi standard
This way should be portable and give predictable results
5@@
Use sed to delete the first line of a file or command output printing from the second line onwards
Actually deceptively simple. Example remove the header from df output.
6@@
As a useful extension, suppose TO_DATA_DIR contains multiple directories, this will total the free space for all relevant filesystems<br /> This one is specific to AIX, for other Unixes and Linux, use $4 instead of $3.
7@@
Use sed to munge a controlfile into submission!
- delete comment lines
- delete all lines after the one starting with a semicolon
- delete all blank lines
- change reuse to set
- change archivelog (with word boundaries) to noarchivelog
- change old sid to new sid
8@@
Find all files where a certain text does not exist
Mostly we want to search for files containing a particular string but how to find those files that do not contain some text.<br /> Using xargs instead of -exec forces the filename to be printed. -H should do this but doesn't seem to in this case.
9@@
Use tar and gzip to copy files more efficiently across network between hosts
From the destination server, this will connect to the source, tar up each file and pull it back to the current server
10@@
double hash and double percent in shell variables to trim off characters from variables
# - trims everything from the start of line till the FIRST occurrence of following character (abstemious) <br />
## - trims everything from the start of line till the LAST occurrence of following character (greedy)<br />
% - trims everything from the end of line backwards till the FIRST occurrence of following character (abstemious)<br />
- trims everything from the end of line backwards till the LAST occurrence of following character (greedy)
<code>11@@</code>
or<br />
<nowiki>#</nowiki> - remove prefix reluctantly<br />
<nowiki>##</nowiki> - remove prefix greedily<br />
% - remove suffix reluctantly<br />
- remove suffix greedily<br />
12@@
The best shell script ever. An example of how scripting should be done
It was written by someone at Oracle. Unfortunately (s)he did not put any author comment in it. Also unfortunately I cannot show it here as it is protected behind Oracles support website.<br /> If you have an Oracle Metalink id, you can get the complete script here<br /> Here is a snippet of a function that demonstrates proper commenting and a very good style.<br > The script is called physru.sh and upgrades an Oracle database in a rolling upgrade fashion by using a physical standby.<br /> There are 4500 lines in the full script but it is so easy to read and understand because of the way it's written, it's like a breath of fresh air. Well done whoever you are!
13@@
split variable (eg. filename) into separate variables using set
14@@
Centre align text on a line in bash shell
Doesn't work in Korn shell due to %*s
15@@
and as a one-liner
16@@
This works in Korn shell…
17@@
Right justify text (or repeat characters) on a line with leader dots!
18@@
cron jobs not submitted - daemon run out of child processes?
Sometimes have problems with NFS mounts and this causes cron jobs to hang. If they are scheduled to run regularly, eventually cron will no longer be able to start any more jobs.<br />
- Check the cron log /var/adm/cron/log to see if there are any errors or other messages around the time the jobs should run.
If cron has hit its process limit (default 100), it will try again after a number of seconds (default 60).<br /> Both the number of jobs and wait time are configured in the file /var/adm/cron/queuedefs. If it is unusual for cron to be running so many jobs, you can check the process table to view the jobs cron has created. These jobs will have parent process id (PPID) of the cron daemon.<br />
19@@
In this example, we only have 1 job running
Find long-running processes with a cron job
Processes running longer than 24 hours have a date instead of a start time…
20@@
Process command line arguments in shell
Borrowed from Mozilla Firefox installer
21@@
Carry XWindows settings across sessions
22@@
Cross-platform version of whoami
23@@
Set terminal title from command line
Put something like this in the .profile<br /> -n do not output the trailing newline<br /> -e enable interpretation of backslash escapes<br />
24@@
25@@
Remove blank lines and comments (also indented ones) from a file
26@@
or as part of a script that removes comments and blank lines from all Korn shell scripts in a directory
27@@
Return elements of an array in Korn shell
From here<br /> Could be used to separate the columns of an SQL select when returning to the shell<br /> This approach eliminates the need to put quotes around text with spaces in it.
28@@
A decent Unix Prompt
29@@
Simple arithmetic
pipe the calculation into the shell calculator
30@@
Calculate the remainder (modulo) of a division calculation
31@@
or do it in awk if scientific notation maybe involved
32@@
Script encryption and passphrase protection
Encrypt a shell script with the ability to execute the encrypted version
- from commandlinefu.com
33@@
Virtual host configuration in Apache http.conf
34@@
and in /etc/hosts, add…
35@@
Mount a website (or any other remote resource) locally using WebDav
Redhat/CentOS
36@@
Debian
37@@
then…
38@@
In man's terms, that last line would translate to:
39@@
Mac OSX
40@@
Add current hostname to list of hosts on an xcat server
41@@
What is todays 'Day Of the Year' number?
42@@
Convert Julian day numbers to dates
43@@
Send crontab job output to a date formatted log file
This will run a job every 5 minutes and send the output to a file ending with a time in hours and minutes.<br /> The thing to note is the escaped percent signs. This is because a % sign is interpreted by cron to mean a newline character. Everything after the first % is treated as input to the program.
44@@
Edit crontab file without crontab -e
It can happen that you need to add or modify a line in the crontab of many users or across many servers at once.<br />
In principle, there's nothing wrong with modifying the crontab file directly. You just lose the advantages of file locking (and syntax checking) that crontab -e offers.<br />
Here we take a backup of the current crontab, print it out, echo an extra command and ask cron to use these as input (thus overwriting the existing crontab file). Just don't run this close to midnight
45@@
or
46@@
or
47@@
Use shell to convert a number in scientific notation to normal
48@@
Check for jobs running longer that 24 hours
Run from the management server across all Unix servers. Checks the 5th column in a ps listing. If it doesn't find a : (time separator), the process is running longer than 24 hours.
49@@
Global find and replace with perl (restricted to list of files provided by Unix find command)
50@@
perl function to make filenames lower case
51@@
From the management server, search the TNS listener port for each database on a server and make an inline substitution in the ITM config files!
52@@
Run a job from cron every Nth day of the month
Example. Execute a job every third Saturday of the month.<br /> Paste this into a file called calenday and put it in /usr/local/bin so it's (probably) on the PATH
53@@
Now in crontab, you should be able to do something like this:<br />
54@@
This will also work on some Unices..<br /> We send backups to a special TSM node on the second Friday of each month. This report must run a day later - but that is not guaranteed to be the second Saturday or even the 3rd. So…
55@@
ps listing does not show start time after 24 hours
But you can see elapsed time using your own ps command
56@@
ps -ef cuts off args cmd column on Solaris
To see more than 80 characters of the last column on Solaris
This shows all the individual arguments to the command
57@@
This shows the ps listing in 'compatibility' mode (there are more compatibility commands in /usr/xpg4/bin)
58@@
To see more than 80 characters of the last column on AIX
This shows the full argument listing of a process (NOTE: no minus sign!)
59@@
Remove blank / empty lines from vi
Maybe you cut and pasted a file from Windows and it's full of blank lines and Control-M's now<br /> There are several methods but I think this is the easiest to remember
60@@
Right pad a variable
a function like rpad in SQL but for Shell<br />
61@@
Connect to a Windows server from Linux using rdesktop
My remmina stopped working so rolled my own. Very simple really. Put this is a shell.
62@@
Slight problems with rdesktop not working 100% of the time. Now using xfreerdp. Seems better…
63@@
Reset your password bypassing password rules
must be done as root
64@@
Sum the sizes of all files of an ls listing
It'll check to see if the sum of filesizes corresponds with the df -g (or h) listing (can get messed up due to open but deleted files)
65@@
Mount an iso image under Linux
66@@
How many processors on the machine?
- AIX
67@@
- Solaris
68@@
- Linux
69@@
Quick, simple, understandable example of how to use RRDTool
Use expect to respond automatically to interactive programs
70@@
Use expect to allow file copy with scp (if ssh keys are not an option)
71@@
Within a shell script set up simultaneous output to both terminal and a file using a FIFO (named pipes)
or “How to send shell output to screen/stdout as well as to a logfile using tee and redirection with exec”<br /> <br /> examples here on unix.com
- general info on redirections here at catonmat.net
72@@
More elaborate example
found here on stackoverflow
73@@
RedHat root filesystem has gone read-only
Kill all processes for a user
74@@
isRGHere
Checks if resource group is on this leg of an HACMP cluster. Returns 0 if true else 1.
75@@
AIX: add a user to a group
76@@
Find swear words in scripts by comparing it to a list found on the internet
77@@
grep -p (for paragraph) works on AIX but not on Linux or Solaris
Use awk instead
78@@
this prints the last word of the second line in the paragraph in dsm.sys that contains the search term AGRHDWQ1.<br /> Or slightly simpler…
79@@
or, case insensitively:
80@@
Using -v means you don't have to use a BEGIN section.
debug/redirect log of a shell
81@@
Different ways of Iteration in korn shell with a while loop
82@@
or
83@@
or
84@@
Filesystem 100% full, what's taking up all the space?
85@@
or
86@@
cd diag and repeat until culprits are found
Show paged memory hogs on AIX
87@@
Capitalise the first letter of a word in Korn Shell
Not as easy as it sounds if it needs to work in non-ksh93 shells
88@@
or maybe more neatly done in Perl…<br /> This will capitalise each word in a sentence passed into it
89@@
Remove / rename a directory / file containing weird control characters
Use ls with -i to see inode listing
90@@
Use find with -inum to get the filename and -exec to remove it
91@@
Run a local script on a remote host
Doing it this way means we do not need to scp the shell over to the host before executing it!
92@@
First of all, this command is a redirection: your shell will open the file your_script.sh and feed it as input to the ssh command. ssh, in turn, will tunnel its stdin to the remote command, namely, sh instance. And sh without arguments reads its script from stdin. So we got sh instance, which is launched on remote host, but reads commands from your local file.
Get a list of running instances - but only those started by the current user
This is the way I wanted to do it but there's an issue. Where does that spare } character come from? Anyone?
93@@
ps listing
94@@
Results of above command
95@@
Workaround 1. Send the ps listing to a file and work on it without pipes. Works but it's a bit long-winded especially as we have to clean up afterwards.
96@@
Workaround 2. Don't like the grep -v but until I find out where that } is coming from..
97@@
Return a list of Oracle databases running on a list of remote servers
98@@
Clever trick to check whether SQL output needs sending to someone
Using an exit status from SQL*Plus back to the shell so we can decide what to do. Nice one!
99@@
Check RMAN logfiles for errors from cron every day
100@@
101@@
An alternative to getopts
These methods have the advantage of allowing arguments of any length not just 1 character
102@@
another way - found in /etc/init.d/functions (daemon function) in Fedora
103@@
another way - found in adstrtal.sh (middleware start script for EBS)
104@@
and another in adautocfg.sh
105@@
Run a script on multiple servers
106@@
to be investigated… this one might be cleverer than mine…
107@@
improved version…<br /> just escape double quotes and dollar signs…
108@@
How can a shell script find out what directory it is in?
basename $0 does not always give the desired answer (might give “./”!
109@@
Array processing
110@@
Send an email by talking directly to an smtp server
111@@
Send an email by talking directly to an smtp server via file descriptor (no telnet! this time), adding authentication
112@@
rsync examples
Also see Distribute files to multiple servers using rsync and ssh
113@@
Handy aliases
Strip out comments and blank lines from a file
114@@
Does a ps and a grep
115@@
Getting a decent listing of filesystem space available. It is ordered such that the filesystems with no space left are at the end.
116@@
A handy way of listing subdirectories and their files
117@@
Watch progress of a copy
118@@
Reboots Linksys router
119@@
Nice one for bash. Colour codes the prompt depending on the outcome of the previous command
120@@
Mmmm, to be looked into. Executes remote commands on a unix box using curl.
121@@
How to configure SSH with public/private keys
Use SSH config file
122@@
then just connect using
123@@
A decent sed tutorial
From grymoire.com
A decent korn/bash shell tutorial
From dartmouth.edu Reproduced here just in case it disappears! Advanced shell scripting
trap
124@@
DNS not working
Ping to an IP address works ping 74.125.136.103 but this doesn't ping www.google.com
Check resolv.conf cat /etc/resolv.conf nameserver 95.130.132.17 nameserver 95.130.132.18 I had changed internet provider and forgot to update this. Just to set it to the router address and let that do the resolution nameserver 192.168.1.1
File descriptors
125@@
126@@
Mmm. See our output and also tee it to a log file!
127@@
Create new image with kvm
Ref: http://www.cyberciti.biz/faq/kvm-virtualization-in-redhat-centos-scientific-linux-6/<br /> Build an empty space for a CentOS virtual machine
128@@
Tried creating image with
129@@
gives error
130@@
rpm / yum commands
Install an RPM Package
131@@
Check dependencies of RPM Package before Installing
132@@
Install RPM Package with all dependencies
133@@
Install RPM Package with all dependencies (when RPM has been downloaded to local machine)
134@@
Force Install a RPM Package without dependencies
Package will not work if dependencies are required
135@@
Check an Installed RPM Package
136@@
List all files of an installed RPM package
137@@
List All Installed RPM Packages
138@@
Query information about an installed RPM package
139@@
Query information about a not yet installed RPM package
140@@
(Forcibly) Remove an RPM Package
Use package name (as seen in -qi above), not full name
141@@
Query a file that was installed as part of an RPM Package (which package contained this file)
142@@
Verify an RPM package
Compares information of installed files of the package against the rpm database
143@@
Rebuild corrupted RPM database
144@@
Install rpmforge repository
- Download rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
- Import the key
145@@
- Install the repository
146@@
- Check the installation
147@@
- Test it
148@@
Install rpmfusion repository
149@@
config file for yum
Checkout this file for global yum config
150@@
Setup Oracle Enterprise Linux (RedHat) with yum server
You need to download the yum .repo file from the server, as per the steps below. After this, you need to enable a flag in the .repo file as per your operating system version. Having done these two steps, when you run yum install <pkgname> command on your linux box, the Oracle's yum server will be scanned, the dependent & the relevant rpm's will be download and installed for you.<br /> cd /etc/yum.repos.d To download files here wget http://public-yum.oracle.com/public-yum-el5.repo
A file named public-yum-el5.repo will be created in your directory<br /> Edit this file and enter enabled=1 against the operating systems which is relevant to you vi public-yum-el5.repo
Next run the yum command yum install package-name
To change to static IP address (Raspberry Pi)
As root:
151@@
replace the line “iface eth0 inet dhcp” with
152@@
You should also take a look at the file /etc/resolv.conf and check it has a nameserver entry (probably pointing at your default gateway) or direct to your ISP name servers.
153@@
Troubleshoot wireless network problems
Short summary of all the things you need to do in just few lines
154@@
To change to static IP address (Redhat/CentOS)
As root:
155@@
Resrart the network interface
156@@
Check name server entry in resolv.conf
157@@
Enable processes / services to start at boot time
158@@
Run a command on lots of servers in parallel
This is a damn fine AIX utility - part of the CSM Distributed Shell. dsh -a “ls -al /etc/apache2/*conf” will list the Apache configuration file on all reachable servers (nodes)
Download a gzip file and pipe it into tar
159@@
Check in a script to make sure it is run only by root
160@@
Set terminal to use Backspace key to erase previous character instead of Control-H
Been looking for this for a long time.<br /> You can put:
161@@
in your .profile but this will be ruined if you do a copy/paste into another file.<br /> I wanted a way of doing this without entering the control character in the .profile. Finally stumbled upon it. And it's so simple. Just escape the caret!
162@@
Put this in the .profile. It's copy/pastable and it works!<br /> If you want CTRL-H to be your erase character, just do this:
163@@
Play with the terminal settings and reset them again either side of requesting a password
The -g option of stty gives a compact list of all the settings or the terminal and can be used as input to stty
164@@
Reset terminal to "sane" characteristics
If you've done a cat of a binary file or something else weird and your terminal is left in a mess, the following key sequence should bring it back to normal
165@@
Install OpenOffice on RedHat Enterprise when yum install doesn't!
Download Package
166@@
Change to root
167@@
Remove the old stuff
168@@
Extract Package
169@@
Install Package and exit root
170@@
Start it
171@@
What does this do?
172@@
or
173@@
List of Special Characters and what they mean
From Bruce Barnett
174@@
