Announce

PukiWiki contents have been moved into SONOTS Plugin (20070703)

bash

bashスクリプトのメモ

snippets

out=${file/\/expression\//_} #replace with regexp
out=${out##*/} #replace from head (last met)
out=${out%.jpg}.png #replace from tail (first met)
option="-c -l -o"
command $option -> (command "-c -l -o") Bad
eval `echo "command $option"` -> (command -c -l -o) Good
a=(11 22 27)
foreach i ($a)
  sh $1 $i
end
while [ $i -le 100 ]
do
  i=`expr $i + 1`
done
foreach file (foo/bar/*)
  if [ -f $file ]; then
  else
  fi
end
if [ "$2" = "debug" ]; then
fi
$ bash hoge.bsh a b c
$* => "a b c"
$@ => "a" "b" "c"
envmerge () {
    var=`eval echo '$'$1`
    if ! echo $var | /bin/egrep -q "(^|:)$2($|:)" ; then
        if [ "$3" = "after" ] ; then
            eval $1='$var:$2'
        else
            eval $1='$2:$var'
        fi
    fi
}

変数名を変数で指定

http://technique.sonots.com/?eval

programs

killps

動機:学校のシステムが killall を使わせてくれなかった。

#!/usr/local/bin/bash
# Copyright (C) Naotoshi Seo, s1080134, 2003

function printUsage(){
  cmd=${0##*/}
  echo "String specified kill. kill front end." 1>&2
  echo "Usage: $cmd [kill_options] [: [egrep options]] pattern" 1>&2
  echo "         ex) $cmd emacs" 1>&2
  echo "         ex) $cmd -9 emacs" 1>&2
  echo "         ex) $cmd '.*'" 1>&2 
  echo "         ex) $cmd 'emacs|kterm'" 1>&2 
  echo "         ex) $cmd : -v tcsh" 1>&2
  echo "         ex) $cmd -KILL : -v tcsh" 1>&2
  echo "         ex) yes | $cmd -9 : -v tcsh" 1>&2
  echo "       Note: Don't use kill's [-s signal] option. Use [-signal] option" 1>&2
}

if [ $# -lt 1 ]; then
  printUsage
  exit 0
fi

if [ "$1" = ":" ];then
  shift
else
  #Kill Options
  while [ $# -gt 0 ]
    do
    if [ $(expr "x$1" : 'x-') -eq 0 ];then
	if [ "$1" = ":" ];then
	    shift
	fi
	break
    fi
    options="$options$1 "
    shift
  done
  if [ $# -eq 0 ]; then
    printUsage
    exit 1
  fi
fi

#main
for ps in `/bin/ps -f -u $USER | grep -v $0 | egrep "$@" | sed 's/ /;/g'` 
  do
  ps=`echo "$ps" | sed 's/;/ /g'`
  echo "$ps"
  pid=`echo $ps | awk '{print \$2}'`
  [ $(expr "$pid" : '[0-9]*$') -eq 0 ] && continue
  echo -n "kill $options$pid ? (y/[n]) "
  read reply
  [ "$reply" == "y" ] && kill $options$pid
done

bconvert

一気に convert コマンド

#!/usr/local/bin/bash
# Copyright (C) Naotoshi Seo, s1080134, 2002

function printUsage(){
  cmd=${0##*/}
  echo "Batch convert. convert front end." 1>&2
  echo "Usage: $cmd [[convert options] :] files ... ext" 1>&2
  echo "          ex) $cmd *.png jpg" 1>&2
  echo "          ex) $cmd : *.png jpg" 1>&2
  echo "          ex) $cmd -size 1024x768 : *.png ../*.gif jpg" 1>&2
}
if [ $# -lt 2 ]; then
  printUsage
  exit 0
fi

if [ "$1" = ":" ];then
  shift
elif [ $(expr "x$1" : 'x-') -ne 0 ];then
  #Options
  while [ $# -gt 0 ]
    do
    if [ "$1" = ":" ];then
      shift
      break
    fi
    options="$options$1 "
    shift
  done
  if [ $# -eq 0 ]; then
    printUsage
    exit 1
  fi
fi
#$@ become files ... ext

while [ $# -gt 0 ]
  do
  case $# in
    1)
    ext=$1
    break
  esac
  files="$files $1"
  shift
done

#main
for file in $files
  do
  echo "convert $options$file ${file%.*}.$ext"
  convert $options$file ${file%.*}.$ext
  if [ "$?" != "0" ];then
    break
  fi
done