250 lines
4.6 KiB
HTML
250 lines
4.6 KiB
HTML
<HTML
|
|
><HEAD
|
|
><TITLE
|
|
>External Commands in the Prompt</TITLE
|
|
><META
|
|
NAME="GENERATOR"
|
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
|
|
REL="HOME"
|
|
TITLE="Bash Prompt HOWTO"
|
|
HREF="index.html"><LINK
|
|
REL="UP"
|
|
TITLE="External Commands"
|
|
HREF="c262.html"><LINK
|
|
REL="PREVIOUS"
|
|
TITLE="PROMPT_COMMAND"
|
|
HREF="x264.html"><LINK
|
|
REL="NEXT"
|
|
TITLE="What to Put in Your Prompt"
|
|
HREF="x295.html"></HEAD
|
|
><BODY
|
|
CLASS="SECT1"
|
|
BGCOLOR="#FFFFFF"
|
|
TEXT="#000000"
|
|
LINK="#0000FF"
|
|
VLINK="#840084"
|
|
ALINK="#0000FF"
|
|
><DIV
|
|
CLASS="NAVHEADER"
|
|
><TABLE
|
|
SUMMARY="Header navigation table"
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TH
|
|
COLSPAN="3"
|
|
ALIGN="center"
|
|
>Bash Prompt HOWTO: </TH
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="left"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="x264.html"
|
|
ACCESSKEY="P"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="80%"
|
|
ALIGN="center"
|
|
VALIGN="bottom"
|
|
>Chapter 4. External Commands</TD
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="right"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="x295.html"
|
|
ACCESSKEY="N"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"></DIV
|
|
><DIV
|
|
CLASS="SECT1"
|
|
><H1
|
|
CLASS="SECT1"
|
|
><A
|
|
NAME="AEN279"
|
|
></A
|
|
>4.2. External Commands in the Prompt</H1
|
|
><P
|
|
>You can use the output of regular Linux commands directly in the prompt as
|
|
well. Obviously, you don't want to insert a lot of material, or it will
|
|
create a large prompt. You also want to use a <EM
|
|
>fast</EM
|
|
>
|
|
command, because it's going to be executed every time your prompt appears
|
|
on the screen, and delays in the appearance of your prompt while you're
|
|
working can be very annoying. (Unlike the previous example that this
|
|
closely resembles, this does work with Bash 1.14.7.)</P
|
|
><TABLE
|
|
BORDER="1"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>[21:58:33][giles@nikola:~]$ PS1="[\$(date +%H%M)][\u@\h:\w]\$ "
|
|
[2159][giles@nikola:~]$ ls
|
|
bin mail
|
|
[2200][giles@nikola:~]$</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><P
|
|
>It's important to notice the backslash before the dollar sign of the
|
|
command substitution. Without it, the external command is executed exactly
|
|
once: when the PS1 string is read into the environment. For this prompt,
|
|
that would mean that it would display the same time no matter how long the
|
|
prompt was used. The backslash protects the contents of $() from immediate
|
|
shell interpretation, so <B
|
|
CLASS="COMMAND"
|
|
>date</B
|
|
> is called every time
|
|
a prompt is generated. </P
|
|
><P
|
|
>Linux comes with a lot of small utility programs like
|
|
<B
|
|
CLASS="COMMAND"
|
|
>date</B
|
|
>, <B
|
|
CLASS="COMMAND"
|
|
>grep</B
|
|
>, or <B
|
|
CLASS="COMMAND"
|
|
>wc</B
|
|
>
|
|
that allow you to manipulate data. If you find yourself trying to create
|
|
complex combinations of these programs within a prompt, it may be easier to
|
|
make an alias, function, or shell script of your own, and call it from the
|
|
prompt. Escape sequences are often required in bash shell scripts to
|
|
ensure that shell variables are expanded at the correct time (as seen above
|
|
with the date command): this is raised to another level within the prompt
|
|
PS1 line, and avoiding it by creating functions is a good idea. </P
|
|
><P
|
|
>An example of a small shell script used within a prompt is given below:</P
|
|
><TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>#!/bin/bash
|
|
# lsbytesum - sum the number of bytes in a directory listing
|
|
TotalBytes=0
|
|
for Bytes in $(ls -l | grep "^-" | awk '{ print $5 }')
|
|
do
|
|
let TotalBytes=$TotalBytes+$Bytes
|
|
done
|
|
TotalMeg=$(echo -e "scale=3 \n$TotalBytes/1048576 \nquit" | bc)
|
|
echo -n "$TotalMeg"</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><P
|
|
>I used to keep this as a function, it now lives as a shell script in my
|
|
<TT
|
|
CLASS="FILENAME"
|
|
>~/bin</TT
|
|
> directory, which is on my path.
|
|
Used in a prompt: </P
|
|
><TABLE
|
|
BORDER="1"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>[2158][giles@nikola:~]$ PS1="[\u@\h:\w (\$(lsbytesum) Mb)]\$ "
|
|
[giles@nikola:~ (0 Mb)]$ cd /bin
|
|
[giles@nikola:/bin (4.498 Mb)]$</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
><DIV
|
|
CLASS="NAVFOOTER"
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"><TABLE
|
|
SUMMARY="Footer navigation table"
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="x264.html"
|
|
ACCESSKEY="P"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="index.html"
|
|
ACCESSKEY="H"
|
|
>Home</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="x295.html"
|
|
ACCESSKEY="N"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
>PROMPT_COMMAND</TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="c262.html"
|
|
ACCESSKEY="U"
|
|
>Up</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
>What to Put in Your Prompt</TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
></BODY
|
|
></HTML
|
|
> |