You are not logged in.

#1 2006-08-27 16:47:29

Kris
Member
From: Denmark
Registered: 2006-07-11
Posts: 86
Website

fortune when I start a terminal!?

Hey!

How do I do so I run a fortune quote when my urxvt term opens? I have seen this on for example Smoons desktops smile And it looks very nice smile


// Kris

"Penquins, Penquins, über alles!"

Offline

#2 2006-08-27 17:15:28

smoon
Member
Registered: 2005-08-22
Posts: 468
Website

Re: fortune when I start a terminal!?

I've got the following at the end of my .zshrc:

if which fortune &> /dev/null ; then
    echo "e[1;31m`fortune`e[0m"
    echo ""
fi

It checks if fortune (the program to show the quotes; package to install is fortune-mod) is in $PATH and just echos it's output if found.

echo "`fortune`"

will work just fine if fortune is installed. This should work in a .bashrc as well.

Offline

#3 2006-08-27 17:25:34

chrismortimore
Member
From: Edinburgh, UK
Registered: 2006-07-15
Posts: 655

Re: fortune when I start a terminal!?

Can't you just do

`which fortune`

in .bashrc?  If it finds fortune, it runs it.  If it doesn't, nothing gets outputted.


Desktop: AMD Athlon64 3800+ Venice Core, 2GB PC3200, 2x160GB Maxtor DiamondMax 10, 2x320GB WD Caviar RE, Nvidia 6600GT 256MB
Laptop: Intel Pentium M, 512MB PC2700, 60GB IBM TravelStar, Nvidia 5200Go 64MB

Offline

#4 2006-08-27 18:02:59

detto
Member
Registered: 2006-01-23
Posts: 510

Re: fortune when I start a terminal!?

chrismortimore wrote:

Can't you just do

`which fortune`

in .bashrc?  If it finds fortune, it runs it.  If it doesn't, nothing gets outputted.

thats just exactly what the little if-script does, just leave out the zsh parts so it looks lie this

if which fortune &> /dev/null ; then
  echo "`fortune`"
  echo ""
fi

^-- checks if fortune is present, and then 'starts' it wink

cheers,
detto

Offline

#5 2006-08-27 18:09:09

chrismortimore
Member
From: Edinburgh, UK
Registered: 2006-07-15
Posts: 655

Re: fortune when I start a terminal!?

detto wrote:

thats just exactly what the little if-script does

Which mine does in two words, no if statement required tongue


Desktop: AMD Athlon64 3800+ Venice Core, 2GB PC3200, 2x160GB Maxtor DiamondMax 10, 2x320GB WD Caviar RE, Nvidia 6600GT 256MB
Laptop: Intel Pentium M, 512MB PC2700, 60GB IBM TravelStar, Nvidia 5200Go 64MB

Offline

#6 2006-08-27 18:26:16

smoon
Member
Registered: 2005-08-22
Posts: 468
Website

Re: fortune when I start a terminal!?

chrismortimore wrote:
detto wrote:

thats just exactly what the little if-script does

Which mine does in two words, no if statement required tongue

Plus you get a nice error message if fortune isn't found by which tongue

Offline

#7 2006-08-27 20:05:16

elasticdog
Member
From: Washington, USA
Registered: 2005-05-02
Posts: 995
Website

Re: fortune when I start a terminal!?

chrismortimore wrote:

Can't you just do

`which fortune`

in .bashrc?  If it finds fortune, it runs it.  If it doesn't, nothing gets outputted.

If it finds <code>fortune</code>, it would not run it, but return its location.  If it is not found, it would return an error message.

Offline

#8 2006-08-27 20:09:40

Kris
Member
From: Denmark
Registered: 2006-07-11
Posts: 86
Website

Re: fortune when I start a terminal!?

detto @
It works great - but I must use .bash_profile, .bashrc don't have effect...

smoon @
I have seen that you - in the Show Your Desktop-thread big_smile - havde changed the default name/HOSTNAME-thing smile How do you make that?


// Kris

"Penquins, Penquins, über alles!"

Offline

#9 2006-08-27 20:43:25

smoon
Member
Registered: 2005-08-22
Posts: 468
Website

Re: fortune when I start a terminal!?

elasticdog wrote:
chrismortimore wrote:

Can't you just do

`which fortune`

in .bashrc?  If it finds fortune, it runs it.  If it doesn't, nothing gets outputted.

If it finds <code>fortune</code>, it would not run it, but return its location.  If it is not found, it would return an error message.

If you prefix it with an echo this is true. If you just run `which fortune` it will run the result of `which fortune' which is either the full path to fortune if found, or an error message if fortune isn't in $PATH - and honestly, running an error message is not the best idea wink

Kris wrote:

[...]
smoon @
I have seen that you - in the Show Your Desktop-thread big_smile - havde changed the default name/HOSTNAME-thing smile How do you make that?

It's the environment variable PS1 in http://hg.nooms.de/dotfiles?f=0aebb8fe2 … yle=gitweb that is responsible for that.

Offline

#10 2006-08-27 20:51:26

chrismortimore
Member
From: Edinburgh, UK
Registered: 2006-07-15
Posts: 655

Re: fortune when I start a terminal!?

elasticdog wrote:

If it finds <code>fortune</code>, it would not run it, but return its location.  If it is not found, it would return an error message.

Note the ` and the ` around the command, once it finds the command, it executes the string, as in the location of the executable.


Desktop: AMD Athlon64 3800+ Venice Core, 2GB PC3200, 2x160GB Maxtor DiamondMax 10, 2x320GB WD Caviar RE, Nvidia 6600GT 256MB
Laptop: Intel Pentium M, 512MB PC2700, 60GB IBM TravelStar, Nvidia 5200Go 64MB

Offline

#11 2006-08-27 21:17:36

Kris
Member
From: Denmark
Registered: 2006-07-11
Posts: 86
Website

Re: fortune when I start a terminal!?

smoon @

 
35 # exports
36 ###
37 if [ "$HOST" != "twoflower" ] ; then
38 _h="@$HOST"
39 fi
40 if [ $UID -eq 0 ] ; then
41 export PS1=$' %{e[1;31m%}%n@%m:%{e[1;32m%}%~ %{e[1;31m%}%#%{e[0m%} '
42 else
43 export PS1=$' %{e[1;34m%}%n'$_h$':%{e[1;32m%}%~ %{e[1;34m%}%#%{e[0m%} '
44 fi

This?[/code]


// Kris

"Penquins, Penquins, über alles!"

Offline

#12 2006-08-27 23:05:27

zen_guerrilla
Member
From: Greece
Registered: 2002-12-22
Posts: 259

Re: fortune when I start a terminal!?

Consider adding this check-up to avoid problems with some terms:

if [ $TERM != "dumb" ]; then `which fortune`; fi

Offline

#13 2006-08-27 23:20:53

smoon
Member
Registered: 2005-08-22
Posts: 468
Website

Re: fortune when I start a terminal!?

Kris wrote:

smoon @

 
35 # exports
36 ###
37 if [ "$HOST" != "twoflower" ] ; then
38 _h="@$HOST"
39 fi
40 if [ $UID -eq 0 ] ; then
41 export PS1=$' %{e[1;31m%}%n@%m:%{e[1;32m%}%~ %{e[1;31m%}%#%{e[0m%} '
42 else
43 export PS1=$' %{e[1;34m%}%n'$_h$':%{e[1;32m%}%~ %{e[1;34m%}%#%{e[0m%} '
44 fi

This?[/code]

Yeah, the PS1 stuff. The first one is red and looks like this
root@host:/pwd %
The secocnd one is blue and looks like
user:/pwd %

Offline

#14 2006-08-27 23:22:48

smoon
Member
Registered: 2005-08-22
Posts: 468
Website

Re: fortune when I start a terminal!?

zen_guerrilla wrote:

Consider adding this check-up to avoid problems with some terms:

if [ $TERM != "dumb" ]; then `which fortune`; fi

What terminals are "dumb" and why are they unable to print out text?

Offline

#15 2006-08-28 01:03:28

zen_guerrilla
Member
From: Greece
Registered: 2002-12-22
Posts: 259

Re: fortune when I start a terminal!?

smoon wrote:

What terminals are "dumb" and why are they unable to print out text?

http://en.wikipedia.org/wiki/Dumb_terminal

Offline

#16 2006-08-28 01:04:13

elasticdog
Member
From: Washington, USA
Registered: 2005-05-02
Posts: 995
Website

Re: fortune when I start a terminal!?

chrismortimore wrote:
elasticdog wrote:

If it finds <code>fortune</code>, it would not run it, but return its location.  If it is not found, it would return an error message.

Note the ` and the ` around the command, once it finds the command, it executes the string, as in the location of the executable.

Doh...didn't read carefully.  :oops:

Offline

#17 2006-08-28 09:09:43

chrismortimore
Member
From: Edinburgh, UK
Registered: 2006-07-15
Posts: 655

Re: fortune when I start a terminal!?

elasticdog wrote:

Doh...didn't read carefully.  :oops:

I'll let you off this time wink


Desktop: AMD Athlon64 3800+ Venice Core, 2GB PC3200, 2x160GB Maxtor DiamondMax 10, 2x320GB WD Caviar RE, Nvidia 6600GT 256MB
Laptop: Intel Pentium M, 512MB PC2700, 60GB IBM TravelStar, Nvidia 5200Go 64MB

Offline

#18 2006-08-28 11:36:11

Kris
Member
From: Denmark
Registered: 2006-07-11
Posts: 86
Website

Re: fortune when I start a terminal!?

smoon wrote:
Kris wrote:

smoon @

 
35 # exports
36 ###
37 if [ "$HOST" != "twoflower" ] ; then
38 _h="@$HOST"
39 fi
40 if [ $UID -eq 0 ] ; then
41 export PS1=$' %{e[1;31m%}%n@%m:%{e[1;32m%}%~ %{e[1;31m%}%#%{e[0m%} '
42 else
43 export PS1=$' %{e[1;34m%}%n'$_h$':%{e[1;32m%}%~ %{e[1;34m%}%#%{e[0m%} '
44 fi

This?[/code]

Yeah, the PS1 stuff. The first one is red and looks like this
root@host:/pwd %
The secocnd one is blue and looks like
user:/pwd %

Hmm... I think that zsh gives a different output than BASH big_smile

[code]
%{%}%n@:%{%}%~ %{%}%#%{%}
[/code]


// Kris

"Penquins, Penquins, über alles!"

Offline

#19 2006-08-28 13:15:16

smoon
Member
Registered: 2005-08-22
Posts: 468
Website

Re: fortune when I start a terminal!?

Kris wrote:
smoon wrote:
Kris wrote:

smoon @

 
35 # exports
36 ###
37 if [ "$HOST" != "twoflower" ] ; then
38 _h="@$HOST"
39 fi
40 if [ $UID -eq 0 ] ; then
41 export PS1=$' %{e[1;31m%}%n@%m:%{e[1;32m%}%~ %{e[1;31m%}%#%{e[0m%} '
42 else
43 export PS1=$' %{e[1;34m%}%n'$_h$':%{e[1;32m%}%~ %{e[1;34m%}%#%{e[0m%} '
44 fi

This?[/code]

Yeah, the PS1 stuff. The first one is red and looks like this
root@host:/pwd %
The secocnd one is blue and looks like
user:/pwd %

Hmm... I think that zsh gives a different output than BASH big_smile

[code]
%{%}%n@:%{%}%~ %{%}%#%{%}
[/code]

Hmm, yeah. There are some differences in Bash (I forgot about that).

%{e[1;31m%} for zsh is [033[1;31m] in bash, whereas 033[ is the indicator for a following escape sequence (e[ should work as well), 1;31 is the colorcode (1 for bold and 31 for red; there's more like underline etc. which you can combine) and m to mark the end of the escape sequence. The [ and ] around that are to tell bash that the characters inside of these braces are not printed out, if you don't put the escape sequences in these bash will get confused about the length of your prompt. If you want to know more just search the intarweb, you'll find plenty of information about this topic there.

Edit: The %n, %m, etc. are different in Bash as well. For example %n is the current user in zsh, in Bash it is u. Have a look at the bash manpage to find out about the available backslash-escaped special characters, the topic is PROMPTING.

Offline

#20 2006-08-28 15:43:47

Kris
Member
From: Denmark
Registered: 2006-07-11
Posts: 86
Website

Re: fortune when I start a terminal!?

smoon wrote:
Kris wrote:
smoon wrote:
Kris wrote:

smoon @

 
35 # exports
36 ###
37 if [ "$HOST" != "twoflower" ] ; then
38 _h="@$HOST"
39 fi
40 if [ $UID -eq 0 ] ; then
41 export PS1=$' %{e[1;31m%}%n@%m:%{e[1;32m%}%~ %{e[1;31m%}%#%{e[0m%} '
42 else
43 export PS1=$' %{e[1;34m%}%n'$_h$':%{e[1;32m%}%~ %{e[1;34m%}%#%{e[0m%} '
44 fi

This?[/code]

Yeah, the PS1 stuff. The first one is red and looks like this
root@host:/pwd %
The secocnd one is blue and looks like
user:/pwd %

Hmm... I think that zsh gives a different output than BASH big_smile

[code]
%{%}%n@:%{%}%~ %{%}%#%{%}
[/code]

Hmm, yeah. There are some differences in Bash (I forgot about that).

%{e[1;31m%} for zsh is [033[1;31m] in bash, whereas 033[ is the indicator for a following escape sequence (e[ should work as well), 1;31 is the colorcode (1 for bold and 31 for red; there's more like underline etc. which you can combine) and m to mark the end of the escape sequence. The [ and ] around that are to tell bash that the characters inside of these braces are not printed out, if you don't put the escape sequences in these bash will get confused about the length of your prompt. If you want to know more just search the intarweb, you'll find plenty of information about this topic there.

Edit: The %n, %m, etc. are different in Bash as well. For example %n is the current user in zsh, in Bash it is u. Have a look at the bash manpage to find out about the available backslash-escaped special characters, the topic is PROMPTING.

[code]
# Exports
if [ "$HOST" != "twoflower" ] ; then
_h="@$HOST"
fi
if [ $UID -eq 0 ] ; then
export PS1=$' [033[1;31m]%n@%m:[1;32m]%~ [1;31m]%#[0m] '
else
export PS1=$' [1;34m]%n'$_h$':[1;32m]%~ [1;34m]%#[0m] '
fi
[/code]

Like this? It give also a weird output smile


// Kris

"Penquins, Penquins, über alles!"

Offline

Board footer

Powered by FluxBB