Are you a spammer

Please note, that the first 3 posts you make, will need to be approved by a forum Administrator or Moderator before they are publicly viewable.
Each application to join this forum is checked at the Stop Forum Spam website. If the email or IP address appears there when checked, you will not be allowed to join this forum.
If you get past this check and post spam on this forum, your posts will be immediately deleted and your account inactivated.You will then be banned and your IP will be submitted to your ISP, notifying them of your spamming. So your spam links will only be seen for an hour or two at most. In other words, don't waste your time and ours.

This forum is for the use and enjoyment of the members and visitors looking to learn about and share information regarding the topics listed. It is not a free-for-all advertising venue. Your time would be better spent pursuing legitimate avenues of promoting your websites.

new bash_aliases

Source code I have written openly published for your viewing pleasure.


new bash_aliases

Postby archeryguru2000 » Wed Jul 22, 2009 10:04 am

Hello all. I've decided to make a contribution to the scripting community here in Ultimate Edition land. One of the things that I usually cannot stand when using a command line interface (terminal), is when I enter a directory with an unusually long name/title or if I traverse many folders deep to do something, my "header" line becomes soo long that my commands begin wraping into the next line down (see attachment 'too_long.png').

What I've done is create an alias for bash, aptly titled '.bash_aliases' in my home directory. Basically what this script does is take the current working directory and display that information in the upper right corner of the window (or upper right corner of the monitor when working in a virtual terminal) and only display the username at the prompt. I found a similar script to this in a book I had purchased some time back that gave me this idea. Whenever you open a terminal (bash), your config file (~/.bashrc) is read. There is usually a loop contained within that file that seems to always be commented out by default;
Code: Select all
#if [ -f ~/.bash_aliases ]; then
#    . ~/.bash_aliases
#fi

If you create this file (~/.bash_aliases), uncomment these lines. This will allow you to modify how your terminal appears, acts, executes, etc.

Here is my .bash_aliases file with comments.
Code: Select all
#!/bin/bash
# written by: archeryguru2000
# Free Software Foundation, 2009.
#
# The bash script alters the appearance of the user's command line interface by
# changing the titlebar, prompt, and location of current directory listing.  This
# script was adapted from a script written in "Linux Desktop Hacks, Tips & Tools
# for Customizing and Optimizing Your OS" by Nicholas Petreley & Jono Bacon (p 42).
#

function prompt_command
{
   tput sc                  # save the current position
   let backwash=$(tput cols)-$(echo $(pwd) | wc -m)-2
   # backwash is the calculation of the position to place the cursor and write the whole current working directory, back-up 2 more places to include the brackets
   tput cup 0 ${backwash}      # position the cursor at y=0, x=calculated length
   tput setaf 1 ; tput smul   # set the foreground color, bold
   echo -n "<"               # wrap the full path in brackets
   echo -n "$(pwd)"         # show the path
   echo -n ">"               # show the closing bracket
   tput setaf 2 ; tput rmul   # return colors back to preferred
   tput rc                  # return cursor to the saved position
}

PROMPT_COMMAND=prompt_command

GREEN="\[$(tput setf 2)\]"
RED="\[$(tput setaf 7)\]"
NO_COLOR="\[$(tput sgr0)\]"

case $TERM in
   xterm*|rxvt*)
      TITLEBAR='\[\033]0;\u@\h \007\]'
      ;;
   *)
      TITLEBAR=""
      ;;
esac

PS1="${TITLEBAR}$GREEN\u$RED\$$NO_COLOR: "
PS2='> '
OS4='+ '

After creating this file and modifying ~/.bashrc, your CLI will appear similar to the attached image 'just_right.png'.

I hope this is easy to follow and hopefully somebody else can make use of this.

~~archery~~
Attachments
too_long.png
too_long.png This shows how inconvenient looonng title can be in a terminal.
just_right.png
just_right.png This is one solution to expanding your working line in a terminal.
~~archery~~
User avatar
archeryguru2000
U.E. Newbie
U.E. Newbie
 
Posts: 24
Joined: Thu Jul 16, 2009 3:19 pm
Location: Central IL, USA
Age: 45
Operating System: Ultimate Edition 3.4 64 BIT

Return to Programming

Who is online

Users browsing this forum: No registered users and 6 guests