Posts Tagged by Shell

shell – all parameters – $* vs $@

Both $* and $@ expand to “all positional parameters”; in a nutshell – “$@” is often desired as it handles spaces correctly; see man bash “Special Parameters”.

% ls my*     
myfoo  my foo  myqux

----------------

% ./star_test my*
$*
myfoo
my     :-(
foo    :-(
myqux

$@
myfoo
my     :-(
foo    :-(
myqux

"$*"
myfoo my foo myqux    <-- probably not desired :-|

"$@"
myfoo
my foo  :-)
myqux

----------------

% cat star_test
#!/bin/bash
echo '$*'
./pargs $*
echo

echo '$@'
./pargs $@
echo

echo '"$*"'
./pargs "$*"
echo

echo '"$@"'
./pargs "$@"

----------------

% cat pargs
#!/bin/bash
for i ; do
    echo $i
done

ssh, sudo, tty, shell

A nice little script snippet I like to remember for automating quick stuff across multiple hosts. -t forces tty (so sudo will work); -q quiet – disable banners; -c script (“command”) to su. Presumes sudo doesn’t prompt for a password; otherwise, see Expect and Exploring Expect :-)

for h in $hosts ; do
  echo -e "nnhost: $hn------------------------------------"
  ssh -t -q $h "sudo su - -c "magic_stuff" "
done

cygwin: zsh: zsh compinit: insecure directories

Using cygwin under Windows, I’ve setup my shell as zsh by editing the launch script:

@echo off
C:
chdir C:cygwinbin
zsh -l -i

Unfortunately, this gives a nasty error everytime the shell is started up, due to Cygwin installing zsh incorrectly:

zsh compinit: insecure directories, run compaudit for list.
Ignore insecure directories and continue [y] or abort compinit [n]? y

Solution, thanks to wezm.net:

compaudit | xargs chmod g-w

vim modelines

I can never remember what those comments that change vim settings are called, and spend hours trawling through vim’s help. They’re called modelines. :help modeline brings up the documentation.

Some typical modelines for copying and pasting:

Go

//
// vim: tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab tw=72
// run 'go fmt' before checking in your code!
//

Text files

# vim:tw=75
# vim:textwidth=75

Shell/Bash

# vim: ai ts=2 sw=2 et sts=2 ft=sh
# vim: autoindent tabstop=2 shiftwidth=2 expandtab softtabstop=2 filetype=sh

Ruby

# vim: ai ts=2 sts=2 et sw=2 ft=ruby
# vim: autoindent tabstop=2 shiftwidth=2 expandtab softtabstop=2 filetype=ruby

Python

# vim: ai ts=4 sts=4 et sw=4 ft=python
# vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4 filetype=python

Or when using vi (eg on Solaris) INVESTIGATE FURTHER:

:set sw=4

Perl

# vim: ai ts=4 sts=4 et sw=4 ft=perl
# vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4 filetype=perl

LISP/Scheme

; vim: ft=lisp

bang history

There are three fields (separated by colons) that can be manipulated using the bang history command:

EventDesignator:WordDesignator:Modifier

(more…)