Sponsored Content
Full Discussion: RFC - Korn shell prompt
Top Forums Shell Programming and Scripting RFC - Korn shell prompt Post 302905795 by gezley on Saturday 14th of June 2014 02:10:33 AM
Old 06-14-2014
RFC - Korn shell prompt

Hi,

I am learning shell scripting for the first time. I use AT&T Korn Shell, Version AJM 93u+ 2012-08-01, compiled from source on NetBSD.

So far I have managed to set up what I think is a useful and pleasing shell prompt, which can be seen in the image attached to this post.

The prompt is a multi-line prompt, as follows:

Line 1 (reverse video) shows the TERM variable, together with TTY, SHELL, and the date and time as it was when the prompt was updated.
(The print -f statement formats this line so that the text is right-aligned and the reverse-video bar takes up the whole width of the terminal, even if it is resized. It uses the COLUMNS variable to achieve this.)

Line 2 shows the host I am connected to (obscured for security).

Line 3 shows the job number and current working directory.

Line 4 shows the user name and the prompt itself.

I have just one problem: unless I symlink /bin/ksh93 to /bin/sh (the default shell on NetBSD) I get errors about "bad substitution". I am certain my prompt is causing this problem, but I still don't know enough to say what is wrong. Would somebody be so kind as to look at my PS1 and tell me where I am going wrong, and where I can improve it? I have learned quite a bit about if...then and case statements while learning how to do this prompt, but I am still unclear about brackets, single quotes and double quotes.

The following is the relevant part of my .kshrc file. I have split the PS1 lines for readability but they are all on one line in the file.

Code:
                                                                         
ttyhere=$(tty | sed -e "s:/dev/::")                                               
timenow=$(date +"%H:%M %Y%m%d")                                                   
currshell=$(print "$SHELL" | sed -e "s:/bin/::")                                  
                                                                                  
PS1='$(print -f "\n\E[1;7m%${COLUMNS}s\E[0m\n" "[$TERM] \
[$ttyhere] [$currshell] [Time at prompt: $timenow] "; \
print "[$(hostname)]"; \
print -n "[\E[1;36m!\E[0m]"; \
if [[ "${PWD#$HOME}" != "$PWD" ]] then; \
print "[\E[1;35m~${PWD#$HOME}\E[0m]"; \
else; print "[\E[1;35m$PWD\E[0m]"; fi; \
if [[ $(id -u) -ne 0 ]] then; \
print "[\E[1;36m$(id -un)\E[0m]$ "; \
else; print "[\E[1;35m$(id -un)\E[0m]# "; \
fi;)' ;;

RFC - Korn shell prompt-promptpng

Last edited by gezley; 06-14-2014 at 03:22 AM.. Reason: Formatting
 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

KORN Shell - Spawn new shell with commands

I want to be able to run a script on one server, that will spawn another shell which runs some commands on another server.. I have seen some code that may help - but I cant get it working as below: spawn /usr/bin/ksh send "telnet x <port_no>\r" expect "Enter command: " send "LOGIN:x:x;... (2 Replies)
Discussion started by: frustrated1
2 Replies

2. Shell Programming and Scripting

how to convert from korn shell to normal shell with this code?

well i have this code here..and it works fine in kornshell.. #!/bin/ksh home=c:/..../ input=$1 sed '1,3d' $input > $1.out line="" cat $1.out | while read a do line="$line $a" done echo $line > $1 rm $1.out however...now i want it just in normal sh mode..how to convert this?... (21 Replies)
Discussion started by: forevercalz
21 Replies

3. AIX

korn prompt autocompletion possible ?

My client`s system is an AIX 4.2 and using the Kron shell. I was just wondering if it is possible to have the prompt autocompletion enabled on it without changing shell version. By autocompletion, I mean to automatically complete the filenames or directories we type in using the Tab key. (2 Replies)
Discussion started by: Browser_ice
2 Replies

4. Shell Programming and Scripting

How to activate Korn Shell functionnalities in Bourne Shell

Hi All I have writing a Korn Shell script to execute it on many of our servers. But some servers don't have Korn Shell installed, they use Borne Shell. Some operations like calculation don't work : cat ${file1} | tail -$((${num1}-${num2})) > ${file2} Is it possible to activate Korn Shell... (3 Replies)
Discussion started by: madmat
3 Replies

5. Shell Programming and Scripting

Database connection from korn shell prompt

Hello, I want to connect to Database through shell command line. Here is my command from putty, $ sqlplus -S ora/ora@ORA But I am not able to connact to database. If this command succed, what is the expected output on shell prompt? Could you please let me know how to connact to... (3 Replies)
Discussion started by: Poonamol
3 Replies

6. Shell Programming and Scripting

MKS KORN SHELL WONT EXECUTE from windows command prompt

Can anybody help me with this small script , the script works fine and launches the IE from c:\documents and settings \test\my documents>ksh prompt $RunURL1.sh this scitpt works and launches the ie from ksh , but when i schedule it to run the script then i get the error box saying command:1... (2 Replies)
Discussion started by: venu
2 Replies

7. Shell Programming and Scripting

Bourne shell & Korn shell

Could some one tell me the difference btw Bourne shell and the Kshell? Which is more flexible and reliable in terms of portability and efficiency. When i type the following command .. $ echo $SHELL yields me /bin/sh Does this tells me that I am in Bourne shell. If yes, how can i get... (6 Replies)
Discussion started by: bobby1015
6 Replies

8. Shell Programming and Scripting

Two-line prompt using Korn

I'm attempting to set up a two-line prompt using Korn. This is what I've set up in .kshrc PS1='$(print -n "`logname`@`hostname`:";if ] then; print -n "~${PWD#$HOME}"; else; print -n "$PWD";fi;print "\n$ ")' And in .profile, ENV="$HOME/.kshrc"; export ENV The hosts that in use are... (10 Replies)
Discussion started by: capnpepper
10 Replies
pg(1)							      General Commands Manual							     pg(1)

Name
       pg - file perusal filter for soft-copy terminals

Syntax
       pg [-number] [-p string] [-cefs] [+linenumber] [+/pattern/] [files...]

Description
       The command is a filter that allows the examination of files one screenful at a time on a soft-copy terminal.  When the file name is desig-
       nated by a minus (-) and/or NULL argument, the command reads from the standard input.  Each screenful is followed by a prompt.  If the user
       types a carriage return, another page is displayed.

       This command is different from previous paginators because it allows you to back up and review material that has already passed.

       In  order  to determine terminal attributes, scans the data base for the terminal type specified by the environment variable TERM.  If TERM
       is not defined, the terminal is assumed to be a dumb terminal.  The command takes responses that can  be  divided  into	three  categories:
       those causing further perusal, those that search, and those that modify the perusal environment.

       Commands  causing  further  perusal normally take a preceding address, which is an optionally signed number indicating the point from which
       further text should be displayed.  This address is interpreted in either pages or lines depending on the command.  A signed address  speci-
       fies  a	point  relative  to  the current page or line, and an unsigned address specifies an address relative to the beginning of the file.
       Each command has a default address that is used if none is provided.

       The perusal commands and their defaults are as follows:

       (+1)<newline> or <blank>
			   Causes one page to be displayed.  The address is specified in pages.

       (+1) l		   Causes to simulate scrolling the screen, forward or backward, the number of lines specified when used with  a  relative
			   address.  With an absolute address this command prints a screenful beginning at the specified line.

       (+1) d or ^D	   Simulates scrolling half a screen forward or backward.

       The following perusal commands take no address:

       . or ^L		   Causes the current page of text to be redisplayed.

       $		   Displays the last window full in the file.  Use with caution when the input is a pipe.

       The  following  commands  are  available  for searching for text patterns in the text.  The regular expressions described in are available.
       They must always be terminated by a <newline>, even if the -n option is specified.

       i/pattern/	   Searches forward for the ith (default i=1) occurrence of pattern.  Searching begins immediately after the current  page
			   and continues to the end of the current file, without wrap-around.

       i^pattern^
       i?pattern?
	      Searches	backwards  for the ith (default i=1) occurrence of pattern.  Searching begins immediately before the current page and con-
	      tinues to the beginning of the current file, without wrap-around.  The circumflex (^) notation is  useful  for  Adds  100  terminals
	      which do not handle the question mark (?) properly.

       After searching, normally displays the line found at the top of the screen.  This can be modified by appending m or b to the search command
       to leave the line found in the middle or at the bottom of the window from now on.  The suffix t can be used to restore the original  situa-
       tion.

       The user of can modify the environment of perusal with the following commands:

       in		   Begins perusing the ith next file in the command line.  The i is an unsigned number.  Default value is 1.

       i		   Begins perusing the ith previous file in the command line.  i is an unsigned number.  Default is 1.

       iw		   Displays another window of text.  If i is present, sets the window size to i.

       s filename	   Saves  the  input  in the named file.  Only the current file being perused is saved.  The white space between the s and
			   filename is optional.  This command must always be terminated by a <newline>, even if the -n option is specified.

       h		   Helps by displaying an abbreviated summary of available commands.

       q or Q		   Quits

       !command 	   The command is passed to the shell, whose name is taken from the SHELL environment variable.  If this is not available,
			   the default shell is used.  This command must always be terminated by a <newline>, even if the -n option is specified.

       At  any	time  when  output is being sent to the terminal, the user can hit the quit key (normally control-) or the interrupt (break) key.
       This causes to stop sending output and to display the prompt.  The user may then enter one of the above	commands  in  the  normal  manner.
       Unfortunately,  some  output  is lost when this is done, because any characters waiting in the terminal's output queue are flushed when the
       quit signal occurs.

       If the standard output is not a terminal, then acts just like except that a header is printed before each file (if there is more than one).

Options
       The command line options are:

       -number	      Specifies the size (in lines) of the window that is to use instead of the default.  (On a terminal containing 24 lines,  the
		      default window size is 23).

       -p string      Causes  to  use  as the prompt.  If the prompt string contains a %d, the first occurrence of %d in the prompt is replaced by
		      the current page number when the prompt is issued.  The default prompt string is designated by a colon (:).

       -c	      Homes the cursor and clears the screen before displaying each page.  This option is ignored if clear_screen is  not  defined
		      for this terminal type in the data base.

       -e	      Causes not to pause at the end of each file.

       -f	      Inhibits from splitting lines.  Normally, splits lines longer than the screen width, but some sequences of characters in the
		      text being displayed (for example, escape sequences for underlining) generate undesirable results.  The -f  option  prevents
		      the splitting of these sequences.

       -s	      Causes to print all messages and prompts in standout mode (usually inverse video).

       +linenumber    Starts up at linenumber.

       +/pattern/     Starts up at the first line containing the regular expression pattern.

Examples
       The following example shows how the command is used reading system news:
       news | pg -p "(Page %d):"

Notes
       While  waiting for terminal input, responds to BREAK, DEL and the circumflex (^) by terminating execution.  Between prompts, however, these
       signals interrupt command's current task and place the user in prompt mode.  These should be used with caution when  input  is  being  read
       from a pipe, since an interrupt is likely to terminate the other commands in the pipeline.

Restrictions
       Terminal tabs must be set every eight positions.

       Using as a filter with another command changes the terminal I/O options.  For example, terminal settings may not be restored correctly.

Files
       Terminal information data base

       Temporary file when input is from a pipe

See Also
       crypt(1), ed(1), grep(1), terminfo(5)

																	     pg(1)
All times are GMT -4. The time now is 07:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy