Bash Examples

edit a file, in-place, with vim

vi -e +"%s/rvm:x:502:root/rvm:x:502:root,<your-username>/" +"wq" /etc/group

iterate over a list of directories or files and do something

#!/bin/bash

function doSomething {
  for TARBALL in *.tar
  do
    DIR_NAME=${TARBALL#*.*}
    DIR_NAME=${DIR_NAME%*.*}
    echo $DIR_NAME 
    mkdir $DIR_NAME
    cd $DIR_NAME
    tar xvf ../$TARBALL
    cd ..
  done
}

for FILE in *
do
  if [ -d $FILE ]; then
    NODE=$FILE
    echo $NODE
    cd $NODE
    doSomething
    cd ..
  fi
done
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License