Bash tricks
Bash'isms
I find this really useful, just a call out to sed so I don't have to remember the syntax.
ssh_delete_key() {
sed -i -e ${1}d ~/.ssh/known_hosts
}
alias sshdel=ssh_delete_key
Usage:
[thomas@laptop: ~] $ ssh 192.168.0.1
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone…
It came up twice that I had to do this, so I decided to see if I could make something simple that solved my problem. I'm sure there's a project to do this already, but here goes. I have a list of machines, I want to see if they respond to a ping so I can determine if they are up or not.
I wanted to do this two ways, either by feeding in a list from stdin or by giving hostnames as arguments. This took a few revisions to get just right, but here's what I came up with:
#!/bin/bash
# ping many hosts and return up or down for each…
It sometimes comes up that the usual trick of reading in a stream with a pipe and a while loop doesn't work, since the pipe causes a subshell and any variables set in the subshell are unavailable to the parent.
For example the normal trick (output the first few lines of the /etc/services file, just the first two columns and not empty or comment lines):
[uphill@zagreb]: grep -v ^# /etc/services | grep -v ^$ | head | awk '{print $1" "$2;}'
tcpmux 1/tcp
tcpmux 1/udp
rje 5/tcp
rje 5/udp
echo 7/tcp
echo 7/udp
discard 9/tcp
…
Sometimes it's easier to write a quick script in bash that uses arrays rather than figure out how to do it with something more appropriate like awk or perl.
It's really very quick and simple. To start, declare your variable as an array
[uphill@host]: declare -a myvar
If the array is going to be global then add x to export it.
[uphill@host]: declare -ax myvar
Then you can put things into your array like this:
[uphill@host]: myvar[…
Note: this is super old, use puppet instead these days...
Script: update_machine update_file
We use this simple script (no error checking as usual) to update our machine configuration.
We keep the configuration files in cvs and download them to a new machine.
Usage: update_machine directory root
- directory
- subdirectory to apply
- root
- where to apply the files (filesystem root, useful when doing this at…
Script: iptohex
No error checking, just a simple time saver. Converts an ipaddress into hexadecimal suitable for use by tftp tools and pxe booting. The builtin command gethostip does this also, I include the script incase someone finds it useful.
Example:
uphill@surrey[1]:
iptohex 10.168.192.134
0AA8C086
#!/bin/bash
IPADDR=$1
if [ x${IPADDR}x = xx ]; then
echo "usage: $0
exit 1
fi
for octet in `echo $IPADDR |tr…
This happens to me when I'm importing photographs, I end up with files called .JPG or .jpeg and I prefer .jpg.
for file in *.JPG
do
mv $file `basename $file .JPG`.jpg
done
The command rename does this same thing nicely, but knowing the for loop way can be more useful sometimes...and if you didn't know about rename, then I guess that would be useful too.
rename .JPG .jpg *.JPG
Say you have some files named a100 to a200 that you want to rename to b200 to b100 (it really does happen).
y=200 for x in `seq 100 200` do mv a$x b$((y--)) done
seq is a fairly useful tool, it can also take a reversed list.
for x in `seq 200 -1 100` do printf "$x " done