simple bash script to end in exec #home-made

In this script, we check if the login is rpmadmin. If not, su to rpmadmin and run the same script!

Useful technique.

if [ $(whoami) != "rpmadmin" ] ; then
  set -x
  exec su rpmadmin $0

# 5 other steps....
sudo /var/opt/rpmadmin/bin/ ${pn}-?.?.?-?.rhel6.3.x86_64.rpm

from within bash script1 get script1’s dirname full path

readlink -f $0 # requires more steps, but takes care of symlinks. Seldom needed.

echo BASH_SOURCE gives "${BASH_SOURCE[0]}"

dirname "${BASH_SOURCE[0]}"
echo .. above dirname is often a relative path

fullpath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo fullpath is $fullpath # fairly reliable one-liner, provided the script itself isn't a symlink

## vi (+less) cheatsheet

  • ~~~~ command mode #roughly ranked
  • [3] dt — “dta” delete until the next “a”
  • [2] 9s — wipe out 9 characters (including current) and enter insert-mode. Better than R when you know how many chars (9) to change
    • to delete 5 characters … there is NO simpler keystroke sequence
  • R — Overwrite each character one by one until end of line. Useful if the replacement content is similar to original?
  • Ctrl-R to re-do
  • cw — wipe out from cursor to end of word and puts you into insert mode
    • c2w or 2cw
  • :se list (or nolist) to reveal invisible chars
  • C — wipe out from cursor to END of line and puts you into insert-mode
  • capital O — open new line above cursor
  • A — to append at END of current line
  • from inside q(LESS), type a single “v” to launch vi

–paging commands in vi and less

  • jump to end of file: capital G == in both vi and LESS
  • jump to head of file: 1G == in both vi and LESS
  • page dn: Ctrl-f == in both; LESS also uses space
  • page up: Ctrl-b == in both; LESS also uses b

[3/4] means vi receives 3 keystrokes; we hit 4 keys including shift or ctrl …

vi on multiple files

[3/4] means vi receives 3 keystrokes; we hit 4 keys including shift or ctrl …

–“split” solution by Deepak M

vi file1 # load 1st file

  • :sp file2 # to show 2nd file upstairs
  • :vsp file3 # to show 2nd file side by side
  • You end up with  — file2 and file3 side by side upstairs, and file1 downstairs!
  • [2/3] ctrl-ww # To move cursor to the “next” file, until it cycles back

–the q( :e ) solution

vi file1 # load 1st file

  • :e file2 # to put 2nd file on foreground
  • [1/3] ctrl-^ — to switch to “the other file”
  • This solution is non-ideal for moving data between files, since you must save active file before switching and you can’t see both files

–editing 3 or more files

  1. vi file1 file2 file3
  2. q(:n) to switch to next, q(:N) for previous…
  3. q(:args) shows all files
  • –Suppose now you are at file2.
  • q(:e file4) works. q(^) will toggle between file2 and file4
  • However, q(:n :N  :args) only work on the original list, not the new files from q(:e)

q(:n :N ^) always shows the current filename in status bar:)