Browse > Home / Archive by category 'Bash'

| Subcribe via RSS

SSH Keys Howto Quicky

October 11th, 2007 | 3 Comments | Posted in Bash, HOWTO

Toss this little shell script in your bin dir and you can quickly create and setup ssh keys between your client and server. I called it sshkeys.sh but you can name it what you want.

#!/bin/bash

KEY_PRIVATE="$HOME/.ssh/id_dsa"
KEY_PUBLIC="${KEY_PRIVATE}.pub"

if [ "$1" == "" ] ; then
   echo "Usage: $0 <[user@]server>"
   exit
fi

if [ ! -f "${KEY_PRIVATE}" ] ; then
   echo Creating the private and public keys.
   ssh-keygen -t dsa -f "${KEY_PRIVATE}" -N ''
fi

if [ -f "${KEY_PUBLIC}" ] ; then
   cat "${KEY_PUBLIC}" |
   ssh "${1}" "mkdir -p ~/.ssh ; cat >> .ssh/authorized_keys2 ; chmod -R go-rwx ~/.ssh"
else
   echo Unable to find "${KEY_PUBLIC}"
fi

Run this, enter your pass once, and then you're free to ssh without entering a password.

Watch for line wraps in your browser, especially the ssh line.

Sed - Stream Editor - Cheat Sheet - good coders code, great reuse

August 2nd, 2007 | No Comments | Posted in Bash

post-icon-sed.jpgSed - Stream Editor - Cheat Sheet - good coders code, great reuse

I present to you my cheat sheet of sed, the Superman of stream editing! It has come handy 101 times for me because sed is not what I use daily and after some time of not using the sed commands tend to fade away from my mind and I have to review them quickly. This cheat sheet is ideal for that!

Very cool cheat sheet for all the sed aficionados in the house.

Ubby Dubby Translator

August 2nd, 2006 | 2 Comments | Posted in Bash, General

A simple English to Ubby Dubby translator.

echo Can you understand this? |sed -e 's/\([aeiouAEIOU]\{1,\}\)/ub\1/g'

BASH: Split a string without 'cut' or 'awk'

April 10th, 2006 | 27 Comments | Posted in Bash

For a little test script I'm writing I needed to split a line on a ';' but preservere the "s and 's, something that echo doesn't like to do. Digging deeper into the bash docs I see that there are some handy string handling functions.

#!/bin/bash
line='this "is" a command;this "is" a pattern'
COMMAND=${line%;*}
PATTERN=${line#*;}
echo $COMMAND
echo $PATTERN

And the output would be:

this "is" a command
this "is" a pattern

BASH: Convert Unix Timestamp to a Date

April 6th, 2006 | 21 Comments | Posted in Bash

I've been asked this a number of times and always have to look it up, so here are 3 ways to convert a unix timestamp (seconds since Jan 1, 1970 GMT) to a real looking date.
Perl method 1: use the ctime module:

perl -e "require 'ctime.pl'; print &ctime($EPOCH);"

Perl method 2: use the scalar and localtime functions:

perl -e "print scalar(localtime($EPOCH))"

Awk has a wrapper for the standard C strftime function:

echo $EPOCH|awk '{print strftime("%c",$1)}'

Here's a sample script that uses all methods.

!#/bin/bash
EPOCH=1000000000
DATE=$(perl -e "require 'ctime.pl'; print &ctime($EPOCH);")
echo $DATE
DATE=$(perl -e "print scalar(localtime($EPOCH))")
echo $DATE
DATE=$(echo $EPOCH|awk '{print strftime("%c",$1)}')
echo $DATE

[update: Thanks to S. Maynard for reminding me of the proper use of quotes and how to avoid using the pipe...]
DATE=$(awk "BEGIN { print strftime(\"%c\",$EPOCH) }")

[UPDATE]

A reader found another way listed below. This doesn't seem to be as portable (The mac ignores the –date and -d is an illegal option).
# date –date='1970-01-01 1000000000 sec GMT'
Sat Sep 8 20:46:40 CDT 2001

[UPDATE]
# date -d @1000000042
Sun Sep  9 01:47:22 GMT 2001

But this only works on newer versions of date.  It fails on my FC2 server and my Debian Sarge machine, but works fine on Ubuntu Feisty and Debian Etch.

Open Tech Support - [Unix/Linux] Linux Shell One-Liners

February 21st, 2006 | No Comments | Posted in Bash

Open Tech Support - [Unix/Linux] Linux Shell One-Liners

A handy little list of some one liners.  I use stuff like this every day and should really get in the habit of posting them.

A short introduction to getting a little more from your shell with piping & redirection

Stupid sed tricks

October 17th, 2005 | No Comments | Posted in Bash, Simple Tips

I just learned that you can use any character, not just the slash (/), for a delimitter when substituting with sed. Most unique characters should work.

This is especially useful when editing streams of file paths.

Here's an example that modifies the path in a playlist:

cat list.m3u | sed 's+/home/aolsen/+~/+'

The +'s worked well as a delimitter here and eliminate the need to quote the /'s

This also works in vi.
–Anton

coming soon

April 10th, 2005 | 1 Comment | Posted in Bash

I'm working on a script randomly grab mp3s from a playlist and fill a CD with shuffled songs.

I'll be using bash's built-in random number generator which I believe is available in all versions.

Here's the shuffle function for those interested:


# Usage: shuffle <filename>
function shuffle () {

   cat "${@}"|grep -v "^#" | \
   while read mp3 ; do
      echo "$RANDOM $mp3"
   done | sort | cut -d" " -f2-
}

Requirements will be mpg321 and cdrecord.

using a bash 'for loop' to wget

March 24th, 2005 | 8 Comments | Posted in Bash, Browser

One of the ways that I frequently fetch files from the internet is with wget. It is a very useful command line utility that is capable of fetching anything from one file to mirroring whole sites.

When looking for new and interesting music I often find myself on a page with a few or more mp3 urls. Using firefox to download them all, even with a good download manager is tedious. The following bash script will read from stdin and download each url it sees.

# while read url ; do wget "${url}" ; done

More »

read a file with bash

March 23rd, 2005 | 60 Comments | Posted in Bash, Simple Tips

I'm going to try to post a few howto articles for some of the simpler tasks. Many of these will be things that I use daily.

It is often necessary to read a file with bash, and act upon the entire line. There are many different ways to do this, but I'll outline two of the simpler methods, both suitable for stacking on a single command line.

For this exercise I'll assume the file is a list of files that we need to execute a command on.

# cat file.lst |while read line; do echo "${line}"; done /tmp/file1.txt /tmp/file with space.txt #

More »