Heiner's SHELLdorado
Shell Tips & Tricks (Beginner)
SHELLdorado - your UNIX shell scripting resource
HomeGood Shell Coding PracticesExample Shell ScriptsShell Scripting LinksShell Scripting Tips+TricksShell Scripting Articles

Tips&Tricks Home
  Beginner
  Intermediate
  Script Programmer
  Advanced

Submit a new Tip

  Top Next Tip
Determine where a command is
Level: Beginner Submitted by: ??? URL: none
The command "type" may be used to find out,
which command the shell executes.

Example:

    $ type ls
    /usr/bin/ls
    $ type l
    l is an alias for 'ls -l'
      

Previous Tip Top Next Tip
Suspending and resuming a command
Level: Beginner Submitted by: ??? URL: none
If you did i.e. a "telnet" to another host, but want to do
a "ls -l" on the current host, you do not have to close the
"telnet" session.

Just type "^Z" (control-z) to suspend the current telnet session.
The command is then waiting for you to resume. In this time
you get a shell prompt and can issue any command.

If you are ready to continue, you can resume the "telnet" session
with the command "fg %1" ("foreground: the first command
waiting").

If you want to run the command in the background, you could
issue a "bg %1" ("background: first command waiting"),
too.

The command "jobs" lists all commands waiting.


      

Previous Tip Top Next Tip
Using arrow keys in kornshell
Level: Beginner Submitted by: perrella@yahoo.com URL: none

Use this tip to enable your arrow keys.
Caveat: You will have to use the emacs bindings

Enjoy!

#!/bin/ksh
# file: enable-arrow
# source in current environment with
# command:
#       . enable-arrows
#

set -o emacs

# Note: these are the actual control
# characters. In vi, type i ctrl-v
# then ctrl-P (if u want a ctrl-p)
alias _A=^P
alias _B=^N
alias _D=^B
alias _C=^F

alias __A=^P
alias __B=^N
alias __D=^B
alias __C=^F
      

Previous Tip Top Next Tip
Removing ^M's in imported DOS and MAC files
Level: Beginner Submitted by: chuck@benatong.com URL: http://www.spamtrap.com
If you have ever imported a DOS or MAC text file, you most likely have run
into the dreaded ^M's at the end of each line.
UNIX uses a newline (\n 10 or 0x0A)
DOS uses BOTH a carriage return and a newline (\r 13 or 0x0D) and \n
and MAC, just to be different, uses just a carrage return (\r)

In the UNIX world and newline is usually displayed as two characters ^M
the ^ means that this is not a printable ASCII character and is usually
spoken as "control"
since it is the character that would be generated if the control key is
held down while typing "M"

To get rid of these unwanted characters you need the UNIX tr  (translate)
command.
I usually add an alias to my shell startup script to create easy to remember
variations
of the tr command for each purpose.

Using bash as an example, edit .bashrc and add these lines.

alias cvtMAC="tr '\r' '\n' "
alias cvtDOS="tr -d '\r' "

You now have two new commands that you can type from the command line.
To try the commands out right away, without opening a new terminal, you
need to tell bash to re-read it's startup file by typing

source .bashrc

Now you are ready to try out the new commands.

To convert a MAC file you would type
cvtMAC < MACFILE > UNIXFILE
This will read a file named MACFILE and create a file named UNIXFILE
that has all of the \r's converted to \n's

For DOS files, you just want to remove the darn \r's
so in cvtDOS the -d tells tr to delete them.

Of course you can create tr commands that reverse this
process.

Have Fun.

chuck
      

Previous Tip Top Next Tip
Print enviroment variables in an easy to read format
Level: Beginner Submitted by: glong@openwave.com URL: none
Replace a normal environment
variable output as in $PATH:

/usr/bin:/usr/ccs/bin:/usr/local/bin:

with:

/usr/bin
/usr/ccs/bin
/usr/local/bin

Use this SIMPLE script:

#! /usr/bin/ksh
IFS=':'
for ii in $PATH
do
        echo $ii
done
# end of file
      

Previous Tip Top Next Tip
Include the current directory within your prompt
Level: Beginner Submitted by: ??? URL: none
To include the current directory within the prompt, include
the following line into your $HOME/.kshrc (KSH) or $HOME/.bashrc
(BASH):


    PS1='$PWD $'
    export PS1
      

Previous Tip Top Next Tip
Running a script without changing file access permissions
Level: Beginner Submitted by: junkies_comp@hotmail.com URL: none
Usually a user has to change the file
permissions for a shell script to
make it executable, e.g.

	chmod +x script

Sometimes it's more convenient
to just invoke the shell with
the script as argument:

	sh script
      

Previous Tip Top Next Tip
How to set the title of a XTERM window
Level: Beginner Submitted by: ??? URL: none
[This tip was first published in the SHELLdorado Newsletter 1/99]

The title of a XTERM window can be set using the following
escape sequence:

    ESC ] 0 ; title ^G

Example:

    echo "^[]0;This is a title^G"

Enter the escape character (the first character of the
string) as CTRL-V ESC. On the screen you will see "^[". The
last character is entered as CTRL-V CTRL-G.

      

Previous Tip Top Next Tip
Show current directory in the XTerm window title bar
Level: Beginner Submitted by: ron.perrella@bellsouth.com URL: none
This combines two tips.  Take the code for showing the current directory
in the prompt, which looks like this:

export PS1='$PWD $ '

now, enhance it with the escape codes for putting a title on your XTerm. (You
can find it easily in the tips section of Shelldorado.  I used to do this at
work, which is great because you can have a bunch of Xterms open and still
get around ok.  If you want to get fancy, change the color of the window
based on which machine you are connected to. (for ex: red for production,
yellow for test, blue for development, etc.)
      

Previous Tip Top Next Tip
Show current directory in the XTerm window title bar (2)
Level: Beginner Submitted by: "Ian P. Springer" <ips@fpk.hp.com> URL: none
Add the following lines to your .profile or $ENV file to print the system
name and current directory in the title bars and icon descriptions of your
terminal windows.  Supports xterm, dtterm, and hpterm:

if [ "$TERM" = xterm ] || [ "$TERM" = dtterm ]; then
   alias cd=Xcd
   Xcd ()
   {
      if [ $# -ne 0 ]; then
         'cd' "$@"
      else
         'cd'
      fi
      NAME="$(uname -n):${PWD}"
      # reset name of xterm title bar & icon to $NAME
      echo "\033]0;${NAME}\007\c"  # set title bar & icon
   }
   Xcd .
elif [ "$TERM" = hpterm ]; then
   alias cd=Hcd
   Hcd ()
   {
      if [ $# -ne 0 ]; then
         'cd' "$@"
      else
         'cd'
      fi
      NAME="$(uname -n):${PWD}"
      LEN=`echo "$NAME\c" | wc -c`
      # reset name of hpterm title bar & icon to $NAME
      echo "\033&f0k${LEN}D${NAME}\c"   # set title bar
      echo "\033&f-1k${LEN}D${NAME}\c"  # set icon
   }
   Hcd .
fi

      

Previous Tip Top Next Tip
Welcome the user as they login to the system
Level: Beginner Submitted by: radhakrishnan_m@hotmail.com URL: none
tt=`date +"%T" | cut -c1-2`
NAME=`grep "^$LOGNAME" /etc/passwd | awk -F: ' {print $5}'`
echo "\n\n\n"
tput smso
if [ $tt -gt 0 -a $tt -lt 12 ]
then
	echo " $NAME !!!!!!    GOOD MORNING !!!!!!"
elif [ $tt -gt 12 -a $tt -le 16 ]
then
	echo " $NAME !!!!!!     GOOD AFTERNOON !!!!!!"
else
	echo " $NAME !!!!!!      GOOD EVENING !!!!!!"
fi
tput rmso

      

   
Copyright © 1998-2001 Heiner Steven (heiner.steven@shelldorado.com)