[SOLVED] Quick bash question (~/.bashrc stuff)

Hi!
First I have to say that I have installed Arch few days ago and it's pretty sweet stuff -- but I guess you already have heard that oh-so-many-times!
So there is it, I would like to change my prompt's color everytime <enter> is pressed in the terminal (well, everytime a new prompt is shown).
But so far I was only able to change my prompt's color everytime a new terminal is opened, with this code in .bashrc :
# color changing for the prompt
colors=("\[\e[0;30m\]" "\[\e[0;34m\]" "\[\e[0;32m\]" "\[\e[0;36m\]" "\[\e[0;31m\]" "\[\e[0;35m\]" "\[\e[0;33m\]" "\[\e[0;37m\]" "\[\e[1;30m\]" "\[\e[1;34m\]" "\[\e[1;32m\]" "\[\e[1;36m\]" "\[\e[1;31m\]" "\[\e[1;35m\]" "\[\e[1;32m\]" "\[\e[1;37m\]")
let R=$RANDOM%16+0
color=${colors[$R]}
prompt="${color}\u@\h:\w\$\[\e[0m\] "
# the prompt
PS1=$prompt
Thanks.
Last edited by orph (2009-03-01 16:10:57)

You could use the PROMPT_COMMAND:
colors=("\[\e[0;30m\]" "\[\e[0;34m\]" "\[\e[0;32m\]" "\[\e[0;36m\]" "\[\e[0;31m\]" "\[\e[0;35m\]" "\[\e[0;33m\]" "\[\e[0;37m\]" "\[\e[1;30m\]" "\[\e[1;34m\]" "\[\e[1;32m\]" "\[\e[1;36m\]" "\[\e[1;31m\]" "\[\e[1;35m\]" "\[\e[1;32m\]" "\[\e[1;37m\]")
PROMPT_COMMAND='let R=$RANDOM%16+0;color=${colors[$R]};PS1="${color}\u@\h:\w\$\[\e[0m\] "'

Similar Messages

  • [Solved] Quick Bash Question

    Ok, I have a loop and I want to count the number of times the loop occurs in my script. Every time the loop occurs there is an output. I want during the loop to display 1 - infinite number until the loop stops.
    Do you know what I am getting at?
    Last edited by haxit (2009-02-13 18:04:37)

    Ok, so I have this script:
    #!/bin/bash
    if [ -z "$1" ]; then
    echo "Please run like so: 'sh "$0" <highest number on dice>'"
    else
    WOW=`echo "$1*2" | bc`
    for X in 1 2 3; do
    ONE=`seq 1 $1 | sort -R | head -1`
    TWO=`seq 1 $1 | sort -R | head -1`
    HAH=`echo "$ONE+$TWO" | bc`
    until [ $HAH -eq $WOW ]; do
    ONE=`seq 1 $1 | sort -R | head -1`
    TWO=`seq 1 $1 | sort -R | head -1`
    HAH=`echo "$ONE+$TWO" | bc`
    NUM=`echo "1"`
    echo "$HAH" >> probability.log
    done
    LOL=`cat probability.log | wc -l`
    echo "$LOL"
    rm probability.log
    done
    fi
    It gives me three outputs, I want to add those outputs and divide them by 3.

  • Quick Bash Question

    I'm trying to write a simple bash script which downloads the compat-wireless drivers and installs them. The script works fine but i'm trying to learn bash and would like to make it a bit nicer to look at and add some more functionality. What I have done is used grep to pull out the percentage of the download, however every number prints on a new line like so:
    1%
    2%
    3%
    4%
    5%
    6%
    etc.... What I'm trying to do is replace the last percentage with the next one, so it's all on one line. I've had a look around and found that a way to do what I'm trying to do is with:
    tput cbt
    or with the bash escape codes directly. Now the thing is I don't know where I should put this in my code, could anyone help?
    #!/bin/bash
    #Retrieve latest compat-wireless soruces
    echo -e "Downloading sources\n"
    wget http://wireless.kernel.org/download/compat-wireless-2.6/compat-wireless-2.6.tar.bz2 |& grep -o "[0-9]\+%"
    if test -fs compat-wireless-2.6.tar.bz2 then
    echo "File Downloaded OK"
    else
    echo "Download Failed, Exiting."
    exit
    fi
    #Untar folder
    echo "Extracting sources"
    tar jxvf compat-wireless-2.6.tar.bz2 > /dev/null
    #Enter build folder
    cd compat-wireless-*
    #Select ath9k as driver to build
    ./scripts/driver-select ath9k
    #Compile sources
    make
    #Install sources
    sudo make install
    #Unload wireless modules
    sudo make unload wireless
    #Reload ath9k module
    sudo modprobe ath9k
    #Return to original directory
    cd ..
    #Remove traces of install files
    sudo rm -r compat-wireless-*

    Ok, so I've tried
    #Retrieve latest compat-wireless soruces
    echo -e "Downloading sources\n"
    wget http://wireless.kernel.org/download/compat-wireless-2.6/compat-wireless-2.6.tar.bz2 |& printf "\r `grep -o "\r[0-9]\+%"`"
    To no avail, am I doing this correctly, or at least on the right lines? I can't seem to find any examples where a command is used within a printf statement.
    Thansk for the help so far!
    edit: Just a thought, could I possibly encompass the grep command into a variable and then pipe it into a printf statement?
    Last edited by jack.mitchell (2011-01-18 15:50:19)

  • [Solved] Quick ln Questions...

    How can I symlink my /tmp with /dev/shm? Would it just be:
    ln -s /dev/shm /tmp
    Last edited by haxit (2009-03-22 02:31:56)

    Please edit your first post to indicate the thread is [SOLVED] (if it is)
    Last edited by Ranguvar (2009-03-22 02:06:30)

  • [SOLVED] Stupid bash question (cp command)

    I accidentally copied a file using * as the destination, like this:
    cp /path/to/file *
    There was no error so it must have done something but I can't figure out what.
    Can anyone tell me what that command does, thanks.
    Last edited by r6 (2010-08-31 07:15:29)

    Hehe getting used to the terminal in the beginning can be fun, you are bound to make some mistakes - I still do at times
    In the mean time adding these to your .bashrc could be helpful safer:
    alias mv="mv -i"
    alias cp="cp -i"
    alias rm="rm -i"

  • Hi all .hope all is well ..A quick trim question

    Hi all
    Hope all is well ......
    I have a quick trim question I want to remove part of a string and I am finding it difficult to achieve what I need
    I set the this.setTitle(); with this
    String TitleName = "Epod Order For:    " + dlg.ShortFileName() +"    " + "Read Only";
        dlg.ShortFileName();
        this.setTitle(TitleName);
        setFieldsEditable(false);
    [/code]
    Now I what to use a jbutton to remove the read only part of the string. This is what I have so far
    [code]
      void EditjButton_actionPerformed(ActionEvent e) {
        String trim = this.getTitle();
          int stn;
          if ((stn = trim.lastIndexOf(' ')) != -2)
            trim = trim.substring(stn);
        this.setTitle(trim);
    [/code]
    Please can some one show me or tell me what I need to do. I am at a lose                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

    there's several solutions:
    // 1 :
    //you do it twice because there's a space between "read" and "only"
    int stn;
    if ((stn = trim.lastIndexOf(' ')) != -1){
        trim = trim.substring(0,stn);
    if ((stn = trim.lastIndexOf(' ')) != -1){
          trim = trim.substring(0,stn);
    //2 :
    //if the string to remove is always "Read Only":
    if ((stn = trim.toUpperCase().lastIndexOf("READ ONLY")) != -1){
       trim = trim.substring(0,stn);
    //3: use StringTokenizer:
    StringTokenizer st=new StringTokenizer(trim," ");
        String result="";
        int count=st.countTokens();
        for(int i=0;i<count-2;i++){
          result+=st.nextToken()+" ";
        trim=result.trim();//remove the last spaceyou may find other solutions too...
    perhaps solution 2 is better, because you can put it in a separate method and remove the string you want to...
    somthing like:
    public String removeEnd(String str, String toRemove){
      int n;
      String result=str;
      if ((n = str.toUpperCase().lastIndexOf(toRemove.toUpperCase())) != -1){
       result= str.substring(0,stn);
      return result;
    }i haven't tried this method , but it may work...

  • Quick script question

    Hi all,
    Could anyone advise me if i can add anything within - header('Location: http://www.mysite.co.uk/thankyou.html'); in the script below so as the page
    re- directs back to the main index page after a few seconds ?
    Thankyou for any help.
    <?php
    $to = '[email protected]';
    $subject = 'Feedback form results';
    // prepare the message body
    $message = '' . $_POST['Name'] . "\n";
    $message .= '' . $_POST['E-mail'] . "\n";
    $message .= '' . $_POST['Phone'] . "\n";
    $message .= '' . $_POST['Message'];
    // send the email
    mail($to, $subject, $message, null, '');
    header('Location: http://www.mysite.co.uk/thankyou.html');
    ?>

    andy7719 wrote:
    Mr powers gave me that script so im rather confused at this point in time
    I don't think I "gave" you that script. I might have corrected a problem with it, but I certainly didn't write the original script.
    What you're using is far from perfect, but to suggest it would lay you open to MySQL injection attacks is ludicrous. For you to be prone to MySQL injection, you would need to be entering the data into a MySQL database, not sending it by email.
    There is a malicious attack known as email header injection, which is a serious problem with many PHP email processing scripts. However, to be prone to email header injection, you would need to use the fourth argument of mail() to insert form data into the email headers. Since your fourth argument is null, that danger doesn't exist.
    One thing that might be worth doing is checking that the email address doesn't contain a lot of illegal characters, because that's a common feature of header injection attacks. This is how I would tidy up your script:
    <?php
    $suspect = '/Content-Type:|Bcc:|Cc:/i';
    // send the message only if the E-mail field looks clean
    if (preg_match($suspect, $_POST['E-mail'])) {
      header('Location: http://www.example.com/sorry.html');
      exit;
    } else {
      $to = '[email protected]';
      $subject = 'Feedback form results';
      // prepare the message body
      $message = 'Name: ' . $_POST['Name'] . "\n";
      $message .= 'E-mail: ' . $_POST['E-mail'] . "\n";
      $message .= 'Phone: ' . $_POST['Phone'] . "\n";
      $message .= 'Message: ' . $_POST['Message'];
      // send the email
      mail($to, $subject, $message);
      header('Location: http://www.example.com/thankyou.html');
      exit;
    ?>
    Create a new page called sorry.html, with a message along the lines of "Sorry, there was an error sending your message". Don't put anything about illegal attacks. Just be neutral.
    By the way, when posting questions here, don't use meaningless subject lines, such as "Quick script question". If you're posting here, it's almost certain to be a question about a script. It doesn't matter whether it's quick. Use the subject line to tell people what it's about.

  • Challenging question! (Does anyone how to solve this Javabat question?)

    Does anyone how to solve this Javabat question?
    Here is the link:
    http://www.javabat.com/prob?id=Array3.linearIn

    I'm a little surprised by the implied rating of this problem... it's a plain to-way merge. You can use the same template as for the merging step in merge sort:
    public boolean linearIn(int[] outer, int[] inner) {
      int i = 0;
      int j = 0;
      while (i < outer.length || j < inner.length) {
          if (j < inner.length && (i == outer.length || inner[j] < outer)) {
    // inner[j] smallest
    return false;
    else if (i < outer.length && (j == inner.length || outer[i] < inner[j])) {
    // outer[i] smallest
    i++;
    else {
    // inner[j] == outer[i]
    i++; j++;
    return true;

  • [SOLVED] alias or bash question

    Hello all,
    I just need a liittle help on alias or bash
    Right now, I want to run vim on xterm. So I always invoke
    xterm vim
    Everything works fine but when I manually add a directory or a directory of a file, something like
    xterm vim /directory/of/file_here
    It won't work.
    This is like manually opening a file in gvim or any app.
    I just want vim to run on xterm.  This is like clicking a file and open with gvim ('xterm vim' in this case).
    I'm currently using KDEmod4.2
    Last edited by ragingmon (2009-02-22 11:08:21)

    yay!! thanks a lot droog!!!
    that was really fast.
    marked as solved..

  • [SOLVED] Quick question about mirrorlist.pacnew

    Is it safe to delete? Does it have to be merged?
    I may have stupidly made a rash decision after an update to just delete after a quick glance through a wiki about it.
    Is there a way to view with pacman -Q a chronological order or timeline of recently installed packages so I can try to re-install and generate another mirrorlist.pacnew ? (I understand that there is a way to query similar information about individual packages, but I simply don't remember which packages I upgraded last night).
    Any help would be appreciated. I want to avoid any hiccups or future issues as my current setup has been super solid.
    Last edited by halo (2014-01-08 17:26:46)

    halo wrote:10:21:09: This is Geany 1.23.1.
    10:21:09: The file "/var/log/pacman.log" does not look like a text file or the file encoding is not supported.
    10:21:09: New file "untitled" opened.
    WorMzy wrote:
    If your editor can't handle plain text files, I'd report it to the devs, that's a pretty big bug.
    Regarding the pacnew, I usually delete it. If I have issues with my mirrors, I use reflector to generate a new mirrorlist.
    Exactly!
    Last edited by GE (2014-01-08 17:23:59)

  • [SOLVED]A simple question about bash scripting

    Just for fun, I thought I'd try making Arch as "parent friendly" as possible (i.e., easy to use once set up).  I've already made a nifty little "system maintenance" script that presents you with a "select" bash menu, with options being "Install, Remove, Update [Without AUR, with AUR, abs, exit], Search, Install and Search (using Yaourt), and exit"
    That works wonderfully, and it only requires typing "sm" into the terminal.
    But just to make it yet *more* "parent friendly", I thought I'd make a .desktop file.
    This is the problem.
    I want the shortcut to essentially open up the script in a terminal.  So it would have to execute, say, "xterm" and then open the "sm" script within the called terminal.
    I believe this would mean I need to either add something to my "sm" script or make a new script that opens terminal and calls sm.
    I'm sure this has an easy solution, but I'm a total amateur just having some fun.
    Any suggestions?
    Last edited by hwkiller (2010-07-13 00:32:48)

    I am not a 100% on .desktop files since I never use them.
    but if your command in the desktop file is something like
    xterm -e /path/to/your/sm/file
    that should work.
    EDIT: Bah too late !
    Last edited by Inxsible (2010-07-12 19:27:11)

  • [Solved] Quick question about yaourt

    I was just wondering if it is save to update my regular packages with yaourt using 'yaourt -Syu --devel --aur' or 'yaourt Syua'. I've heard about problems with having to have root access to update regular programs and it's a very bad thing to build AUR packages while root. I want to be able to update my AUR packages like I can with 'pacman -Syu' and if I could do them all at the same time, well that would be even better.
    Last edited by slughappy1 (2011-10-27 16:46:44)

    Parmazae wrote:
    slughappy1 wrote:Ok, thanks for the replies. So as I understand it Yaourt will build the AUR packages as me (non root when called) and then will only ask for root access when installing/updating packages using pacman?
    Correct .
    Sweet, thanks.

  • [solved] Quick question about Qt4

    I am trying to create a PKGBUILD for QSvn, which requires Qt4. I installed Qt4:
    pacman -S qt4
    then
    qt-config qt4
    then,
    source /etc/profile
    So i know it is installed
    to install QSvn you have to run:
    cmake -D CMAKE_BUILD_TYPE="Release" ../src
    I get this error (sorry for the foto, only way, could not output to text):
    Any ideas...???
    Julian D
    Last edited by SleepingGiant08 (2007-03-07 22:30:21)

    fftb wrote:
    Yeahh, please, pm it to me. This would be gorgeous. BTW did you already test QSvn? Does it fit your needs? I'm looking for a SVN client which integrates in GNOME/Nautilus, but wasn't successful yet.
    Benjamin.
    ehhh..... got rid of it, didn't really like it that much, idk why, I just use plain old subversion now .... There are a good amount of SVN clients out there, but i don't know if any integrate into Nautilus tho

  • [Solved] Quick question about mkinitcpio upgrade

    I just ran pacman -Syu and got the following message:
    (4/8) upgrading mkinitcpio                         [######################] 100%
    ==> If your /usr is on a separate partition, you must add the "usr" hook
        to /etc/mkinitcpio.conf and regenerate your images before rebooting
    I'm just wondering, what counts as a separate partition? My partitions contain:
    /boot
    /home
    swap
    My /usr is presumably on the / partition, which is "separate" from the boot partition where the kernel images are. Do I have to do anything?
    Last edited by eggplantbren (2012-05-24 03:00:37)

    eggplantbren wrote:
    I just ran pacman -Syu and got the following message:
    (4/8) upgrading mkinitcpio                         [######################] 100%
    ==> If your /usr is on a separate partition, you must add the "usr" hook
        to /etc/mkinitcpio.conf and regenerate your images before rebooting
    I'm just wondering, what counts as a separate partition? My partitions contain:
    /boot
    /home
    swap
    My /usr is presumably on the / partition, which is "separate" from the boot partition where the kernel images are. Do I have to do anything?
    THANK YOU MAAN...
    I'm so GLAD that you post this here, it's 2 days ago I updated my Archlinux, and after update and reset I didn't use to login to it.
    I know that problem should be near MKINITCPIO but I didn't know where.
    But today I read your post and after it I boot to archlinux by liveISO and update mkinitcpio again with MISSED parameter "usr".
    Well done boy...
    Thank you so much
    Have a nice day.

  • ::SOLVED:: change bash prompt while in ssh

    I'm learning how to use bash bit by bit, but came up with a question I'm not certain how to solve.  I learned some time ago about setting the PS1 variable to customize the shell prompt, and have a nice prompt I like using.  However, it would be nice to have the prompt be flagged if I'm logged in through ssh as opposed to directly on the computer itself.  How could I do this?
    One thought I had was to check to see if certain environment variables exist (eg. $SSH_CONNECTION).
    Doing if [ $SSH_CONNECTION ]; then ... fi gives an error about too many arguments.  None of the other SSH related variables seem to be just a number value.  Is there a way to test for the existence of a variable in bash?
    Last edited by beretta (2009-05-07 20:44:06)

    Procyon wrote:What you want is [ -n "$SSH_CONNECTION" ] (variable is set/nonzero length) or the opposite [ -z "$SSH_CONNECTION" ] (variable is not set/zero length)
    Thanks-- that got it.  I was missing the quotes, so it was expanding at the spaces rather than interpreting the entire thing as a string.

Maybe you are looking for