Hey There,
Following in the tradition of our "terminal beautification" posts on using color in the shell and making fancy looking menus with tput, today we're going to take a look at setting the title of your terminal window from within the shell.
This is actually a slight variation from what we've looked at before, but it can be just as helpful, since you'll be using your shell to modify the "content" of your terminal's title in Windows (or whatever OS you use). This can be a great help in keeping organized. Most terminal clients provide functionality like this as a standard, but don't allow you to modify the defaults (like every window will have the title of the host you're connected to, etc) without jumping through hoops on the Windows (or Mac) end of things.
The simplest thing to do is to set static window titles in "xterm." In fact, if you're running X-Windows, you don't even have to do anything fancy, since most widely-distributed versions of it contain command line arguments specifically for doing this. For instance, the following command line (invoking xterm) would bring it up with the title "This XTerm Is Mine" (and, for goofs since it's an available option, an icon titled "This Icon Is Mine, Too!"):
host # xterm -T "This XTerm Is Mine" -n "his Icon Is Mine, Too!" &
Simple enough. It's also fairly simple to set static window titles for almost any terminal emulator (like PuTTY or SecureCRT) by typing at the command prompt or including a line in your .profile, .bashrc or .zshrc (Or whatever initialization file you use if you use an alternate shell).
Here again, we come back to some concepts that we first went over when we looked at using color in the terminal. Your window title is created by using very similar "echo -e" syntax (include the initial "escape") and can be done at the command line by typing the following:
host # echo -e "\033]0;This Is My Window Title And My Icon Title\007" <--- This pretty much works all the time
host # echo -e "\033]1;This Is Just My Icon Title\007" <--- This works sometimes depending on your terminal
host # echo -e "\033]2;This Is Just My Window Title\007" <--- This pretty much works all the time
In the above three lines, we set the window and icon title, icon title and window title, respectively. Some of these statements may not work on your terminal, so you'll have to test, but (in my experience so far) at least one of them should. Usually the first one (setting both the icon and title) is your best bet.
Now, if we want to have our window and icon title change dynamically (we're going to forget about the icon-only and window-only settings, since they can be derived by just substituting the "0" in the following lines with either "1" or "2," per the examples above) all we have to do is integrate the escape sequences into our command prompt. In some shells this functionality has been accounted for. In others, we can force it.
1. Bash - To set this in bash, we'll just set the value of the PROMPT_COMMAND variable, either at the command prompt or in our .bashrc (or .profile) for permanence:
host # PROMPT_COMMAND='echo -ne "\033]0;${HOSTNAME}:${PWD} # \007"';export PROMPT_COMMAND
and, you'll notice that your window and icon titles are now something like:host1:/users/user1 #
and that the window and icon titles will change whenever you change directories :) We'll do the same command line string for the other shells, so this post maintains some consistency.
2. Zsh - To set this in zsh, all we need to do is set the value of the precmd function at the command prompt or in our .zshrc, like so:
host # precmd () {print -Pn "\e]0;%n:%~\a"}
3. Ksh - To set this in ksh, all we need to do to get the same result is type the following at the command line, or include it in our .profile (Admittedly, this can be a little tricky since you have to insert the actual control characters into the prompt - For more info on how to do that, check out our previous post on how to really represent control characters within files). Note, also, that setting PS1 will change your main command prompt, so, after the ctl-G, I've added "${PWD}# '" which will be our actual command prompt, as opposed to our new window title. You should replace this with whatever your command prompt already is if you want to keep it the same:
host # PS1='^[]0;${USER}@${HOST}: ${PWD}^G${PWD}# '
And you can use that for most other shells, except...
4. Csh/Tcsh - To set this in csh/tcsh, you can use the "settitle" alias (Again, you'll need to manually put in the real control characters and the title is slightly different since my tcsh/csh knowledge is fair-to-middlin' in this respect ;)
host # alias settitle 'set t=$cwd:h;echo -n "^[]2;${HOST}: "!*"^G"'
And that should be all there is to getting yourself all set up to have dynamic and/or static window and icon titles for whenever you want or need. Here's hoping you can find a window and icon naming scheme you can live with and enjoy :)
Best wishes,
, Mike
linux unix internet technology
Thursday, April 3, 2008
Setting Static And Dynamic Window Titles From The Shell
Wednesday, April 2, 2008
Using Bash To Access The Network Via File Descriptors
Greetings,
It's been a while since we looked at down-and-dirty shell tricks, and this one comes as a tangential follow-up to our previous post on finding and reading files in the shell when your system is on the verge of a crash.
The first thing I'd like to clear up before we proceed is that, in re-reading that post, I notice that I was being a bit of a ksh-snob in the section regarding reading the contents of a regular file through a file descriptor to save on resource usage. My example line in that post, after exec'ing file descriptor 7, was:
host # while read -u7 line; do echo $line; done
And, as most bash fans probably noted, that syntax doesn't work in bash. That's what I get for only verifying the output in ksh ;) What that line should have read (since this will work in sh, ksh and bash) was:
host # while read line;do echo $line;done <&7
Apologies all around. In any event, that "mistake" serves as a fairly decent segue into what we're going to be looking at today. That same basic process of exec'ing file descriptors to read files directly is how we're going to use bash to read from the network directly using /dev/tcp.
And, just to be clear, this "does not" work in Solaris ksh or sh. It will work on Solaris and every Linux I've tested, but only, to my knowledge, in bash. Every version of ksh I've tested (even Solaris 10) can't create the virtual file system required by this operation. I've attached a small bash script, at the end of the post, to check a mail server and http server, so you have a working example of the process.
This method of accessing the network is very useful to know how to do and, going through it step by step, is actually very simple to accomplish. We'll use a mail server as our example since it involves both reading and writing to the network file descriptor.
The first thing you'll want to do is "exec" the file descriptor you'll be using to communicate on the network via tcp. You can do this like so (Just be sure not to exec file descriptors 0, 1 or 2, as these should already be assigned by your operating system as "Standard Input (STDIN)," "Standard Output (STDOUT)," and "Standard Error (STDERR)" :
host # exec 9<>/dev/tcp/mail_server.xyz.com/25 <-- Here we create file descriptor 9 via exec, open the /dev/tcp/mail_server.xyz.com/25 "virtual file" and assign file descriptor 9 to it.
Note that this line is where the script, or process, will either make or break. ksh (Definitely for Solaris 9 and 10) does not allow the creation of the virtual filesystem below /dev/tcp that this operation requires. If you attempt to do this in ksh you will most likely get the following error:
host # /dev/tcp/mail_server.xyz.com/25: cannot create
Assuming we're using bash, and that worked okay, we can now write to, and read from, our mailserver over port 25. This is analogous to doing something like Telnetting to port 25, but we're using a built in feature of the bash shell to access the network via a pseudo file system created under /dev/tcp (It acts much like a /proc filesystem, although you can't see what you've created by looking at the file that /dev/tcp links to, since it's a character device file and the directory path /dev/tcp/mail_server.xyz.com/25 doesn't "really" exist :)
Now we can send input, and receive output, using standard file descriptor redirection commands. A few "for instances" below:
host # echo "HELO xyz.com" >&9 <--- This sends the HELO string to our mailserver via file descriptor 9, which we exec'ed above.
host # read -r RESPONSE <&9 <--- This reads the data coming in on file descriptor 9 with as little interpretation as possible (for instance, it treats the backslash (\) as a backslash and doesn't imbue it with it's usual "special" shell powers).
host # echo -n "The mail server responded to our greeting with this load of sass: "
host # echo $RESPONSE
And, now, we can write to, and read from, file descriptor 9 until we have nothing left to say ;) When we're all finished, I find it's good practice to close the input and output channels for our file descriptor (although closing the input should be sufficient in most cases), and, if you want to or need to, you can also dump the remainder of the file descriptor's contents before you close it down.
host # cat <&9 2>&1 <--- Dump all of the remaining input/output left on file descriptor 9 to our screen.
host # 9>&- <--- Close the output file descriptor
host # 9<&- <--- Close the input file descriptor
Now, you're back to the way things were and you shouldn't be able to read or write from file descriptor 9 anymore (or, if you're a glass-half-full person, it's now available for use again :) I've tacked on a small script to demonstrate some basic functionality, but, hopefully, you'll have some fun playing around with this and figure out more, and better, ways to manipulate your server's interaction with the network through the file system.
host # ./check_net.sh <--- As simple as that to run it :)
Cheers,
This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License#!/bin/bash
#
# check_net.sh
#
# 2008 - Mike Golvach - eggi@comcast.net
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#
mailserver="mailserver.xyz.com"
httpserver="www.xyz.com"
domain="xyz.com"
echo "Testing mail server functionality"
exec 9<>/dev/tcp/$mailserver/25
read -r server_version <&9
echo "Server reports it is: $server_version"
echo "HELO $domain" >&9
read -r greeting <&9
echo "Server responded to our hello with: $greeting"
echo "VRFY username" >&9
read -r vrfy_ok <&9
echo "Server indicates that this is how it feels about the VRFY command: $vrfy_ok"
echo "quit" >&9
read -r salutation <&9
echo "Server signed off with: $salutation"
echo "Dumping any remaining data in the file descriptor"
cat <&9 2>&1
echo "Closing input and output channels for the file descriptor"
9>&-
9<&-
echo "--------------------------------------------------"
echo "Testing web server functionality - Here it comes..."
exec 9<>/dev/tcp/$httpserver/80
echo "GET / HTTP/1.0" >&9
echo "" >&9
while read line
do
echo "$line"
done <&9
echo "Dumping any remaining data in the file descriptor"
cat <&9 2>&1
echo "Closing input and output channels for the file descriptor"
9>&-
9<&-
echo "done"
, Mike
linux unix internet technology
Wednesday, March 26, 2008
Using Color In The Linux Or Unix Shell

Click the image above to see all the pretty colors bigger ;)
Hey There,
Today's we're going to go off on a tangent and take a look at color and the Linux and/or Unix console. I first noticed the use of color in the shell while using Linux a long long time ago, and it seemed really cool back then. Every now and again it bugs me, but I must admit that it does come in handy for a lot of things; editing code or simple file type identification are two great examples.
The script attached to this post was written mostly to illustrate the concept that you can see in the picture attached to this text. More to the point, I wrote it so that we could see the colors I'm writing about :) Admittedly, I used every possible combination of color and modification for completeness, which has the nasty side effect of making some of the output invisible (You can't see the black foreground on the black background unless reverse video or bolding are turned on), but that's the nature of the beast. You probably won't ever need or want to print black on black, unless you're writing a menuing system for someone you can't stand ;)
One thing to note is that the colors and modifications in this script are limited to the ECMA-compliant codes. Linux and the Bash shell, for example, have a lot more color options, but I wanted to keep this post as fairly balanced as possible. These codes should work on almost any Unix or Linux console and most SSH or Telnet shell clients today probably support them as well. Still, not everything will work everywhere. For instance, the blinking text code does not work on my PuTTY terminal, although it does work on my SecureCRT. Since I'm not paying for the color, I guess I can't complain ;)
Getting these colors to show on your terminal is actually fairly simple. First, you just need to know what all the codes stand for. Below is a quick listing. The 30's are foregrounds, the 40's are backgrounds and the remaining set are modifications (any and all of which can be combined, if you so choose):
# 30 black foreground
# 31 red foreground
# 32 green foreground
# 33 brown foreground
# 34 blue foreground
# 35 purple foreground
# 36 light blue foreground
# 37 gray foreground
#
# 40 black background
# 41 red background
# 42 green background
# 43 brown background
# 44 blue background
# 45 purple background
# 46 light blue background
# 47 white background
#
# 1 turn on bolding
# 22 turn off bolding
# 5 turn on blinking
# 25 turn off blinking
# 7 turn on reverse video
# 27 turn off reverse video
# 0 reset everything to default
Now, if you wanted to print a color to your screen, all you need to do is use the echo command (Note that -e tells echo to interpret the backslashed characters, so "\033[30m" doesn't show up literally). For instance, to print "HI" in red font, you could type:
host # echo -e "\033[31mHI"
The "m" character following the number code 31 (Indicating that you want to print in red) completes the instantiation of the color. Without it, your text would remain whatever color it already was. The incorrect echo statement may also have other bizarre side effects, like repositioning your cursor or resetting your terminal. The final "m" is very important :)
You can also combine different colors, as well as modifications, by using the semicolon. Below, the first line writes "HI" in light blue on a green background (Uggh) and the second does the same in BOLD :)
host # echo -e "\033[36;42mHI"
host # echo -e "\033[36;42;1mHI"
One thing to note is that all of these commands would leave your terminal stuck in whatever state you instructed on the command line. That is, if you used the color commands to type with a green background, your terminal's background would remain green after you typed the line. In order to return everything to normal, you can use the modification number "0" - This will reset your terminal to the default color settings. If you only want to print one line in red font, the line shown below would work better for you than the original (the first example, above)
host # echo -e "\033[31mHI\033[0m"
Note, also, that (for the color code commands) there can't be any spaces. "\033[31m" has to be one unit. The text you put in between this command and the closing "\033[0m" (should you elect to go back to the way things were ;) can have spaces anywhere, just like any other block of text.
Hope you have some fun with shell colors. Enjoy the script. If nothing else, it should serve as a good reminder of the syntax and a good indicator of the breadth of options ECMA compliant colors give you. Linux, as I mentioned, has even more variety, but using those specific codes can make your scripts less portable. Good or bad? You decide ;)
Cheers,
This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License#!/bin/bash
# colors.sh - Print out all the different ECMA color-mod combos
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#
declare -a color_array
declare -a alphabet_soup
color_array=( '30;40;1' '30;40;5' '30;40;7' '30;40;22' '30;40;25' '30;40;27' '30;40' '30;41;1' '30;41;5' '30;41;7' '30;41;22' '30;41;25' '30;41;27' '30;41' '30;42;1' '30;42;5' '30;42;7' '30;42;22' '30;42;25' '30;42;27' '30;42' '30;43;1' '30;43;5' '30;43;7' '30;43;22' '30;43;25' '30;43;27' '30;43' '30;44;1' '30;44;5' '30;44;7' '30;44;22' '30;44;25' '30;44;27' '30;44' '30;45;1' '30;45;5' '30;45;7' '30;45;22' '30;45;25' '30;45;27' '30;45' '30;46;1' '30;46;5' '30;46;7' '30;46;22' '30;46;25' '30;46;27' '30;46' '30;47;1' '30;47;5' '30;47;7' '30;47;22' '30;47;25' '30;47;27' '30;47' '30;1' '30;5' '30;7' '30;22' '30;25' '30;27' '30' '31;40;1' '31;40;5' '31;40;7' '31;40;22' '31;40;25' '31;40;27' '31;40' '31;41;1' '31;41;5' '31;41;7' '31;41;22' '31;41;25' '31;41;27' '31;41' '31;42;1' '31;42;5' '31;42;7' '31;42;22' '31;42;25' '31;42;27' '31;42' '31;43;1' '31;43;5' '31;43;7' '31;43;22' '31;43;25' '31;43;27' '31;43' '31;44;1' '31;44;5' '31;44;7' '31;44;22' '31;44;25' '31;44;27' '31;44' '31;45;1' '31;45;5' '31;45;7' '31;45;22' '31;45;25' '31;45;27' '31;45' '31;46;1' '31;46;5' '31;46;7' '31;46;22' '31;46;25' '31;46;27' '31;46' '31;47;1' '31;47;5' '31;47;7' '31;47;22' '31;47;25' '31;47;27' '31;47' '31;1' '31;5' '31;7' '31;22' '31;25' '31;27' '31' '32;40;1' '32;40;5' '32;40;7' '32;40;22' '32;40;25' '32;40;27' '32;40' '32;41;1' '32;41;5' '32;41;7' '32;41;22' '32;41;25' '32;41;27' '32;41' '32;42;1' '32;42;5' '32;42;7' '32;42;22' '32;42;25' '32;42;27' '32;42' '32;43;1' '32;43;5' '32;43;7' '32;43;22' '32;43;25' '32;43;27' '32;43' '32;44;1' '32;44;5' '32;44;7' '32;44;22' '32;44;25' '32;44;27' '32;44' '32;45;1' '32;45;5' '32;45;7' '32;45;22' '32;45;25' '32;45;27' '32;45' '32;46;1' '32;46;5' '32;46;7' '32;46;22' '32;46;25' '32;46;27' '32;46' '32;47;1' '32;47;5' '32;47;7' '32;47;22' '32;47;25' '32;47;27' '32;47' '32;1' '32;5' '32;7' '32;22' '32;25' '32;27' '32' '33;40;1' '33;40;5' '33;40;7' '33;40;22' '33;40;25' '33;40;27' '33;40' '33;41;1' '33;41;5' '33;41;7' '33;41;22' '33;41;25' '33;41;27' '33;41' '33;42;1' '33;42;5' '33;42;7' '33;42;22' '33;42;25' '33;42;27' '33;42' '33;43;1' '33;43;5' '33;43;7' '33;43;22' '33;43;25' '33;43;27' '33;43' '33;44;1' '33;44;5' '33;44;7' '33;44;22' '33;44;25' '33;44;27' '33;44' '33;45;1' '33;45;5' '33;45;7' '33;45;22' '33;45;25' '33;45;27' '33;45' '33;46;1' '33;46;5' '33;46;7' '33;46;22' '33;46;25' '33;46;27' '33;46' '33;47;1' '33;47;5' '33;47;7' '33;47;22' '33;47;25' '33;47;27' '33;47' '33;1' '33;5' '33;7' '33;22' '33;25' '33;27' '33' '34;40;1' '34;40;5' '34;40;7' '34;40;22' '34;40;25' '34;40;27' '34;40' '34;41;1' '34;41;5' '34;41;7' '34;41;22' '34;41;25' '34;41;27' '34;41' '34;42;1' '34;42;5' '34;42;7' '34;42;22' '34;42;25' '34;42;27' '34;42' '34;43;1' '34;43;5' '34;43;7' '34;43;22' '34;43;25' '34;43;27' '34;43' '34;44;1' '34;44;5' '34;44;7' '34;44;22' '34;44;25' '34;44;27' '34;44' '34;45;1' '34;45;5' '34;45;7' '34;45;22' '34;45;25' '34;45;27' '34;45' '34;46;1' '34;46;5' '34;46;7' '34;46;22' '34;46;25' '34;46;27' '34;46' '34;47;1' '34;47;5' '34;47;7' '34;47;22' '34;47;25' '34;47;27' '34;47' '34;1' '34;5' '34;7' '34;22' '34;25' '34;27' '34' '35;40;1' '35;40;5' '35;40;7' '35;40;22' '35;40;25' '35;40;27' '35;40' '35;41;1' '35;41;5' '35;41;7' '35;41;22' '35;41;25' '35;41;27' '35;41' '35;42;1' '35;42;5' '35;42;7' '35;42;22' '35;42;25' '35;42;27' '35;42' '35;43;1' '35;43;5' '35;43;7' '35;43;22' '35;43;25' '35;43;27' '35;43' '35;44;1' '35;44;5' '35;44;7' '35;44;22' '35;44;25' '35;44;27' '35;44' '35;45;1' '35;45;5' '35;45;7' '35;45;22' '35;45;25' '35;45;27' '35;45' '35;46;1' '35;46;5' '35;46;7' '35;46;22' '35;46;25' '35;46;27' '35;46' '35;47;1' '35;47;5' '35;47;7' '35;47;22' '35;47;25' '35;47;27' '35;47' '35;1' '35;5' '35;7' '35;22' '35;25' '35;27' '35' '36;40;1' '36;40;5' '36;40;7' '36;40;22' '36;40;25' '36;40;27' '36;40' '36;41;1' '36;41;5' '36;41;7' '36;41;22' '36;41;25' '36;41;27' '36;41' '36;42;1' '36;42;5' '36;42;7' '36;42;22' '36;42;25' '36;42;27' '36;42' '36;43;1' '36;43;5' '36;43;7' '36;43;22' '36;43;25' '36;43;27' '36;43' '36;44;1' '36;44;5' '36;44;7' '36;44;22' '36;44;25' '36;44;27' '36;44' '36;45;1' '36;45;5' '36;45;7' '36;45;22' '36;45;25' '36;45;27' '36;45' '36;46;1' '36;46;5' '36;46;7' '36;46;22' '36;46;25' '36;46;27' '36;46' '36;47;1' '36;47;5' '36;47;7' '36;47;22' '36;47;25' '36;47;27' '36;47' '36;1' '36;5' '36;7' '36;22' '36;25' '36;27' '36' '37;40;1' '37;40;5' '37;40;7' '37;40;22' '37;40;25' '37;40;27' '37;40' '37;41;1' '37;41;5' '37;41;7' '37;41;22' '37;41;25' '37;41;27' '37;41' '37;42;1' '37;42;5' '37;42;7' '37;42;22' '37;42;25' '37;42;27' '37;42' '37;43;1' '37;43;5' '37;43;7' '37;43;22' '37;43;25' '37;43;27' '37;43' '37;44;1' '37;44;5' '37;44;7' '37;44;22' '37;44;25' '37;44;27' '37;44' '37;45;1' '37;45;5' '37;45;7' '37;45;22' '37;45;25' '37;45;27' '37;45' '37;46;1' '37;46;5' '37;46;7' '37;46;22' '37;46;25' '37;46;27' '37;46' '37;47;1' '37;47;5' '37;47;7' '37;47;22' '37;47;25' '37;47;27' '37;47' '37;1' '37;5' '37;7' '37;22' '37;25' '37;27' '37' '40;1' '40;5' '40;7' '40;22' '40;25' '40;27' '40' '41;1' '41;5' '41;7' '41;22' '41;25' '41;27' '41' '42;1' '42;5' '42;7' '42;22' '42;25' '42;27' '42' '43;1' '43;5' '43;7' '43;22' '43;25' '43;27' '43' '44;1' '44;5' '44;7' '44;22' '44;25' '44;27' '44' '45;1' '45;5' '45;7' '45;22' '45;25' '45;27' '45' '46;1' '46;5' '46;7' '46;22' '46;25' '46;27' '46' '47;1' '47;5' '47;7' '47;22' '47;25' '47;27' '47' '1' '5' '7' '22' '25' '27' )
alphabet_soup=( ABC DEF GHI JKL MNO PQR STU VWX YYZ )
color_array_count=${#color_array[@]}
alphabet_soup_count=${#alphabet_soup[@]}
echo "Number of color code combinations in array = $color_array_count"
alpha_count=0
for ((x=0; x<$color_array_count; x++))
do
if [ $alpha_count -gt 9 ]
then
alpha_count=0
fi
echo -ne "\033[${color_array[${x}]}m${alphabet_soup[${alpha_count}]}\033[0m"
((alpha_count++))
done
echo
, Mike
linux unix internet technology
Thursday, February 28, 2008
Finding and Reading Files In The Shell When Your System Is Hung
Howdy,
Today we're going to look at a situation that occurs probably more often than it should ;) If you've done your fair share of system administration on Linux or Unix, you've probably run into a predicament where you got paged, called, etc, to look at a machine that was hanging on the brink, only to find that (asssuming you could log in at all) it was so completely trashed that it couldn't even muster up the strength to run the simple system commands you needed to diagnose the problem before just giving up and rebooting.
Typical errors you'd get at the command line, in this sort of situation, would be similar to:
host # ls /tmp
Insufficient Memory!
or
host # cat file
Cannot fork new process!
Basically, you're stuck in a situation where you can't do anything that relies on executing anything other than the login shell that you're lucky you got in the first place ;)
The good news is that you can, quite possibly, get as much information as you need before rebooting by simply using your shell and it's built-ins. All of these examples should work for sh, ksh, bash, etc (Possibly not in csh, but, hopefully that's not your system default root shell). If you have a good idea what's wrong before your machine goes down entirely, they can even help you decide what you want to do before you boot the system back up (Check out this post for some simple tricks to figure out what commands are available to you in Solaris' PROM).
Here are the things I try to do when I find myself in that sort of situation (in no order of importance ;)
1. Move around the filesystem.
Luckily, the "cd" and "pwd" commands are built into the shell, so you can always move around your filesystem (even if you are, figuratively, in the dark) and get to the hot spots you want to check. For instance:
host # cd /var/log
will work just fine. This is the most obvious thing you can do, but cd (on its own) depends on you to know where you're going. You can't cd to a directory that doesn't exist even when things are up and running perfectly ;)
If you happen to get lost, you can figure out where you are, using the built-in "pwd," like so:
host # pwd
/var/log
2. Take a look at the contents of your filesystem.
This is actually pretty obvious, as well, once you realize how to do it. You won't be able to use "ls" any more, since that is a command that the shell invokes outside of itself, but you can always use the built-in "echo" command, like so:
host # echo *
bin opt sbin usr tmp var
Note that this output won't usually be so pretty. If there are 50 files and/or directories in the directory you're in, you'll just get 50 filenames in a row on one line. But, it's better than nothing :)
3. Capture the contents of critical files that can help you troubleshoot and/or find your root cause so this won't ever happen again (hopefully).
This last one is slightly less obvious, but can be done in a variety of ways. The first two ways are messy, but they work. Simply read your file as though you were sourcing it, using either the "source" or dot "." built-ins. The reason I don't prefer these two methods is that your screen fills up with a lot of garbage and you may hang your system by sourcing the contents of a file that contains executable statements or commands. For our example here, we'll assume a file named BOB with one line in it that says "hi":
host # source ./BOB
-bash: hi: command not found
or
host # . ./BOB
-bash: hi: command not found
You've gotten your output, but you can see where the potential problem would lie. What if "hi" was a command and your "source" directive tried to run it on your already half-dead machine? It might put it all the way down right then and there.
In these instances, I think output redirection is your best bet. You can read the contents of a file by simply executing a new file descriptor and reading from that with "echo." You can use pretty much any number that works (although, try to stay away from 0, 1 and 2 as these are your system's STDIN, STDOUT and STDERR file descriptors), you won't have to read your file through the clutter of a bunch of error messages and you will be insulating yourself from accidentally executing any commands. For instance, the following would get you much better results:
host # exec 7<BOB <--- Execute new file descriptor 7 and redirect the output of your BOB file to it.
host # while read -u7 line; do echo $line; done <--- Then, just read from the file descriptor.
hi
And that's about it. If you use all 3 of these methods in whatever combinations are necessary, you should be able to collect most of the information you need to assess your situation and/or provide root cause.
For one last example that uses all 3, this is how I would go about getting the contents of my /var/log/syslog file (I'll shorten the output to only include the relevant stuff) - Note that I'm also doing the syslog reads in a command line "for loop" because I want to get all the information I can with as little typing as possible:
host # pwd
/home/mydirectory
host # cd /var/log
host # echo *
sysylog syslog.1 syslog.2
host # for x in syslog.2 syslog.1 syslog; do exec 7<$x;while read -u7 line;do echo $line;done;done
blah.... <--- All of the sylog files' output. Notice that I did them in reverse, so that the output would be from oldest to newest. It's also a good idea (if possible) to either log your terminal session or set your terminal client's buffer to a very large number so that you can cut and paste this output into your desktop editor.
Hope this helps you out :)
Best wishes,
, Mike
linux unix internet technology
Saturday, December 15, 2007
A Simple Trick To Keep Your SSH Session From Timing Out
Hey there,
I thought I'd write a simple something this Saturday; just a little trick that's served me well over the years.
This has to do with any login session in a terminal window (Telnet, SSH, etc). Although this may not always be the case, most places I've ever worked at have had, at least, a few machines were they enforced time-outs on your login. This can be frustrating if you're in the middle of doing something, get called away to do something else, and come back to a "Disconnected!" dialogue box.
Usually, you should be able to get away with just putting a line like this in your .profile or .bash_profile:
TMOUT=0;export TMOUT
But, I've found that a lot of setups don't honor this shell setting. Instead, they take a measure of your activity and log you out if you don't produce enough in your session. So, when you type:
sqlplus @database_query
Even if that takes all day to run, you'll still get disconnected in 5 minutes.
This simple shell loop has almost always worked for me:
while :;do print -n "* ";sleep 15;done
Substitute either
echo -n "* "
or
echo "*\c" <--- \c is the escape character representation of a space.
for the print statement in that little one-line loop, depending on your shell (sh doesn't support the "print" statement) and its implementation of echo (you might be using the built-in or the system binary; in any event - one of these three varieties should do the trick for you.
I leave the carriage-return out on purpose so I can come back later and look at:
*******_
instead of pages of single *'s along the side of my screen, forcing me to scroll back forever to remember what I was doing ;)
This trick basically works because, even though you're not directly interacting with your terminal, you're sending packets back and forth every time that print (or echo) statement executes.
Enjoy a little less stress. Your terminal windows should now be waiting for you instead of threatening to leave :)
, Mike
linux unix internet technology





