nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

.bashrc, doubts about bash scripting.

This is a discussion on .bashrc, doubts about bash scripting. within the Shell scripting forums, part of the Development/Scripting category; Hello! I am really new in shell scripting and knowing other languages as Java or C# I have been googling ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 13-03-2009, 06:35 PM
KaLPo's Avatar
Junior Member
User
 
Join Date: Mar 2009
Location: I am a ramdom nowhereman
OS: Ubuntu Intrepid
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
KaLPo is on a distinguished road
Question .bashrc, doubts about bash scripting.

Hello!

I am really new in shell scripting and knowing other languages as Java or C# I have been googling for an answer of the question I about to ask but I found no one. This is because I ask it here, .

My OS is Ubuntu 8.10 (Intrepid). Using Gnome.
GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)

I am customizing the file bash.bashrc in order to feel the Gnome terminal a little bit more homely. My aim now, after have changed colors and added needless stuff is to make more dynamic the actual path:

As normal, when I am in my home folder I see:
[ ~ ] $ instead of [ /home/username ] $

That is fine, but I want to go further and abbreviate other folders, such as my workspace:
[ + ] $ instead of [ /home/username/drive/external/workspace ] $

I have found a pretty useful code for doing that:

Code:
PROMPT_COMMAND='DIR=`pwd|sed -e "s!$HOME/drive/external/workspace!+!" | sed -e "s!$HOME!~!"`;CurDir=$DIR;'

PS1="${BLUE}${RED}${debian_chroot:+$debian_chroot}\[\033[0;38;5;32m\][\[\033[0;38;5;202m\] \$CurDir \[\033[0;38;5;32m\]]${RED}\$ ${NC} "
I can not say that I understand everything, probably that is the main problem , but I know the comand "sed -e" and obviously "s!$HOME!~!" change the home folder for ~ in some way. But, it is case sensitive.

So, if I type "cd ~/drive/external/WorKsPACe", it does not work.

I have found some interesting functions which transform strings, for example:
Code:
$VAR_NAME | tr '[:upper:]' '[:lower:]'
But I guess the structure "s!$HOME/drive/external/workspace!+!" do not let me use it because it do not use variables for the substitution.

Do you know what can I do?

Thank you.

Last edited by KaLPo; 13-03-2009 at 07:10 PM. Reason: Including my system details
Reply With Quote
  #2 (permalink)  
Old 13-03-2009, 10:25 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,706
Thanks: 11
Thanked 243 Times in 183 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

I'm sorry I do not understood your question. Do you want to change prompt settings?
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 13-03-2009, 10:29 PM
KaLPo's Avatar
Junior Member
User
 
Join Date: Mar 2009
Location: I am a ramdom nowhereman
OS: Ubuntu Intrepid
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
KaLPo is on a distinguished road
Default

Yes this is, I want to change my prompt for shorten some folders root, as the system does with the local user folder ( ~ ).
Reply With Quote
  #4 (permalink)  
Old 14-03-2009, 12:51 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,706
Thanks: 11
Thanked 243 Times in 183 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Try
Code:
export PS1=[\u@\h \W]\$
Add it to your ~/.bashrc or ~/.bash_profile. No need to modify system scripts. Add all stuff to your personal to ~/.bashrc. Refer following for further details:

  1. How to: Change / Setup bash custom prompt (PS1)
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #5 (permalink)  
Old 14-03-2009, 01:05 AM
KaLPo's Avatar
Junior Member
User
 
Join Date: Mar 2009
Location: I am a ramdom nowhereman
OS: Ubuntu Intrepid
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
KaLPo is on a distinguished road
Default

I am sorry for my awful explanations.

I already have the symbol "~" representing "/home/username", I want to add other symbols or abbreviations, for example, "+" for "~/drive/external/workspace" or "@" for "/var/www".

Thank you very much for answeringnixcraft.
Reply With Quote
  #6 (permalink)  
Old 14-03-2009, 01:39 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,706
Thanks: 11
Thanked 243 Times in 183 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

I do not think so it is possible. Traditional programming languages such as C++ or QT toolkits have something called hooks or signal/slot. They can send signals containing event information and function can take action. But shell do not have such function to modify PS1 (there is now pwd signal) as you change directory to /var/www and add @ as prefix.

You may have to write a code in C or other real programming language to do so...
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
'bashrc 'User Profile caron_k2000 Linux software 2 23-07-2008 02:52 PM
Doubts vaibhav.kanchan Networking, Firewalls and Security 1 06-12-2007 11:32 AM
Help needed regarding bash scripting of a webpage chris411 Shell scripting 2 07-09-2006 10:40 AM
Help needed regarding bash scripting ---full description chris411 Shell scripting 1 07-09-2006 04:21 AM
scripting ganes Shell scripting 2 06-09-2005 12:04 PM


All times are GMT +5.5. The time now is 09:51 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

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 28 29 30 31 32 33 34 35 36 37 38