Filed under CLI | by Samuel Huckins | Date Posted: October 31, 2007 - 2:08 PM
I have been experimenting with adding more color to my bash prompts of late. I find it easier to read when the fields are in distinct colors. The problem I always have is remembering what those wonderfully obscure ANSI escape sequences represent. I always have to look at a table to remind myself that “light red” maps to “1;31″.
In an interesting guide to configuring your bash prompt that I have been going through, there was a script listed that makes this much easier:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| #!/bin/bash
#
# This file echoes a bunch of color codes to the
# terminal to demonstrate what is available. Each
# line is the color code of one forground color,
# out of 17 (default + 16 escapes), followed by a
# test use of that color on all nine background
# colors (default + 8 escapes).
#
# Taken from the Bash Prompt HOWTO:
# http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
T='gYw' # The test text
echo -e "\n 40m 41m 42m 43m\
44m 45m 46m 47m";
for FGs in ' m' ' 1m' ' 30m' '1;30m' ' 31m' '1;31m' ' 32m' \
'1;32m' ' 33m' '1;33m' ' 34m' '1;34m' ' 35m' '1;35m' \
' 36m' '1;36m' ' 37m' '1;37m';
do FG=${FGs// /}
echo -en " $FGs \033[$FG $T "
for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
do echo -en "$EINS \033[$FG\033[$BG $T \033[0m";
done
echo;
done |
This is from the Bash Prompt HOWTO, from a script by Daniel Crisman. It produces the following when run:

Just add an alias like
1
| alias colors="~/dir/of/handy/scripts/print_shell_colors.sh" |
, and you can be reminded whenever you like.
I am still experimenting with my prompt colors, but differentiating the fields I find useful has made things a lot easier:

Tags:
Bash,
CLI,
command
October 31, 2007 - 2:08 PM
Filed under CLI | by Samuel Huckins | Date Posted: October 30, 2007 - 3:20 PM
Any linux distribution around these days will have compression and rotation in place for files in
(or wherever else they happen to go). So if you look in there, you will see one or two log files (current and the last one) for each process logged, as well as 3-7 compressed files for the same process. These then get rotated out.
In any case, say you want to look inside one of those compressed log files. You could write out an untar command and then view the file. Then have to delete the temporary file. Messy. There is a better way.
The first I found was interesting (from PuppyLinux): Simply run
. You view the contents of the file with the man file viewer. The annoying thing is that is does funny things with line breaks.
A much better way:
1
| <a href="http://www.mkssoftware.com/docs/man1/zcat.1.asp">zcat</a> COMPRESSEDFILE | less |
. You view the file in less, and when done, there is no temp file to delete. To make it faster, just add a simple alias:
1
| alias viewlog="zcat $1 | less" |
. This makes viewing those compressed logfiles as painless as viewing the current ones.
Tags:
Bash,
CLI,
command,
Linux
October 30, 2007 - 3:20 PM
Filed under Programming | by Samuel Huckins | Date Posted: October 23, 2007 - 10:55 PM
It seems nearly every week, I find something amazingly handy in bash that I had not used before. This time: Loops. I would bet nearly everyone who has written more than a handful of shell scripts over a few lines in length has used one or more of the loops bash has to offer. But, what I realized (not sure why it did not bash me sooner) is that loops are also very effective timesavers as one-off commands in everyday shell usage.
I suppose the association came from first using the shell in a very simple way, of just giving commands singly, and only using constructs like loops in scripts I wrote out in files. But recently, I had several identical operations to perform on a series of files with consistent names, and a loop came to mind. The files were all mp4 videos, and I needed to convert them with ffmpeg, and send them through flvtool2. Instead of 2 commands typed out for each file, I ran:
1 2 3 4 5 6 7
| for FILE in $(find . -maxdepth 1 -type f -iname \*large.mp4); do
ffmpeg -sameq -i $FILE -s 480x270 -ar 44100 -r 10 $FILE.flv;
done
for FILE in $(find . -maxdepth 1 -type f -iname \*.flv); do
cat $FILE | flvtool2 -U stdin $FILE;
done |
To simply iterate over a list of strings, using each in a command:
1 2 3
| for TABLE in cool_table1 cool_table2 cool_table3; do
mysqldump -u root cool_database $TABLE;
done |
You can use loops like this in any shell script, just write the whole loop on one line, separating the conditions, parts in the suite, and the termination with semicolons (these are mostly optional when using line breaks in scripts in files). Assorted resources:
Tags:
Bash,
CLI
October 23, 2007 - 10:55 PM
Filed under CLI | by Samuel Huckins | Date Posted: October 23, 2007 - 10:48 AM
Fuser is a very handy command when you are trying to investigate what is listening on a box.
Consider the following case. You, being a diligent systems administrator, have been performing regular nmap scans against your boxes from remote hosts. You discover that something is listening on port 587 on a server. What you immediately need to know is: what program is actually listening on that port? The quickest way to find out is to simply run
on the box in question. This queries the kernel for what PID is listening on the specified port and reveals almost what you need:
The first column is the port you specified, the second is the PID using that port currently. This can be combined with ps to give you the desired output, such as via
1
| echo `sudo /sbin/fuser 587/tcp` | cut -d' ' -f 2 | xargs ps |
:
1
| PID TTY STAT TIME COMMAND 8102 ? S 14:13 /usr/libexec/postfix/master |
I used echo in this case because I was unable to decipher the delimiter used between the two columns in the default output. The whole thing should be aliased such that you run the alias and pass a port, and the ps output is produced.
NOTE: fuser is generally found in /sbin or /usr/sbin, which you may have to add to your path.
Tags:
CLI,
command,
Linux
October 23, 2007 - 10:48 AM
Filed under CLI | by Samuel Huckins | Date Posted: October 20, 2007 - 11:21 PM
My last post was on how I used Samba to share files from my central media server to other machines on my local network. Seemed great at first, but having used it for a little while, I was rather disappointed. I had Rhythmbox looking at the mounted drive as my library. It started scanning the files once the directory was mounted. I could play them, but after a few minutes, Rhythmbox would freeze scanning the files. In addition, I could not even list the directory that contained the mounted directory. Something was getting frozen. This kept happening more and more, and was quite frustrating.
I then tried of SSHFS for the same purpose, and it has been working much better. If you can SSH to the machine with the files you want to share, then you don’t have to make any changes to that computer. Just a few steps on the machine that will be accessing the remote store, and you are ready to go. From the top:
- Create the dir which to which the remote dir will be mounted (e.g. /home/myuser/Music)
- Make sure you have ssh access on remote machine from the local machine
- On the local machine:
-
1
| sudo apt-get install sshfs |
-
1
| sudo usermod -G fuse -a youruser |
-
1
| sudo chgrp fuse /dev/fuse |
- Logout of myuser on the local machine, then back in
-
1
| sshfs 192.168.1.6:/dir/on/remote/server LOCALDIR |
will make sure you have that kernel module loaded. You have to be in the fuse group to perform the mount, sudo or not. I am not exactly sure why you have to change the group of the fuse device, but you definitely have to to get it to work.
At this point, you should be able to view all the files in /dir/on/remote/server from LOCALDIR. Performance wise, I had great success. Pointing Rhythmbox to the dir I mounted, it scanned all the files in a timely manner, and I was able to play them as if they were on the local machine without issue.
If you wish to unmount the share, run
1
| sudo fusermount -u LOCALDIR |
. One problem that has been suggested for which I do not have a fix yet is how to handle if the remote machine has a problem, reboots, or the network connection is otherwise dropped. I am not sure how this would be handled, since the mount would no longer work. fuse being a kernel module, this may cause freezing. If I find this addressed somewhere, I will post it.
Additional information and links:
Tags:
CLI,
filesystem,
ssh
October 20, 2007 - 11:21 PM