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
SU(1)							    BSD General Commands Manual 						     SU(1)

NAME
su -- substitute user identity SYNOPSIS
su [-] [-flm] [login [args]] DESCRIPTION
The su utility requests appropriate user credentials via PAM and switches to that user ID (the default user is the superuser). A shell is then executed. PAM is used to set the policy su(1) will use. In particular, by default only users in the ``admin'' or ``wheel'' groups can switch to UID 0 (``root''). This group requirement may be changed by modifying the ``pam_group'' section of /etc/pam.d/su. See pam_group(8) for details on how to modify this setting. By default, the environment is unmodified with the exception of USER, HOME, and SHELL. HOME and SHELL are set to the target login's default values. USER is set to the target login, unless the target login has a user ID of 0, in which case it is unmodified. The invoked shell is the one belonging to the target login. This is the traditional behavior of su. The options are as follows: -f If the invoked shell is csh(1), this option prevents it from reading the ``.cshrc'' file. -l Simulate a full login. The environment is discarded except for HOME, SHELL, PATH, TERM, and USER. HOME and SHELL are modified as above. USER is set to the target login. PATH is set to ``/bin:/usr/bin''. TERM is imported from your current environment. The invoked shell is the target login's, and su will change directory to the target login's home directory. - (no letter) The same as -l. -m Leave the environment unmodified. The invoked shell is your login shell, and no directory changes are made. As a security precau- tion, if the target user's shell is a non-standard shell (as defined by getusershell(3)) and the caller's real uid is non-zero, su will fail. The -l (or -) and -m options are mutually exclusive; the last one specified overrides any previous ones. If the optional args are provided on the command line, they are passed to the login shell of the target login. Note that all command line arguments before the target login name are processed by su itself, everything after the target login name gets passed to the login shell. By default (unless the prompt is reset by a startup file) the super-user prompt is set to ``#'' to remind one of its awesome power. ENVIRONMENT
Environment variables used by su: HOME Default home directory of real user ID unless modified as specified above. PATH Default search path of real user ID unless modified as specified above. TERM Provides terminal type which may be retained for the substituted user ID. USER The user ID is always the effective ID (the target user ID) after an su unless the user ID is 0 (root). FILES
/etc/pam.d/su PAM configuration for su. EXAMPLES
su man -c catman Runs the command catman as user man. You will be asked for man's password unless your real UID is 0. su man -c 'catman /usr/share/man /usr/local/man' Same as above, but the target command consists of more than a single word and hence is quoted for use with the -c option being passed to the shell. (Most shells expect the argument to -c to be a single word). su -l foo Simulate a login for user foo. su - foo Same as above. su - Simulate a login for root. SEE ALSO
csh(1), sh(1), group(5), passwd(5), environ(7), pam_group(8) HISTORY
A su command appeared in Version 1 AT&T UNIX. BSD
September 13, 2006 BSD
All times are GMT -4. The time now is 05:38 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy