Register now and start sharing your code snippets.
-->

Check if a file or directory exists with bash

Shell Script (Bash) posted about 1 year ago by christian

This script tests if nginx exists and is executable. The script prints a warning and exits, if nginx doesn’t exists or isn’t executable:

   1  DAEMON=/usr/local/sbin/nbinx
   2  if [ ! -x $DAEMON ]
   3  then
   4     echo "Couldn't find $DAEMON. Please set path to DAEMON."
   5     exit 0
   6  fi

See man test for more information on how to use the test command.

Tagged nginx, daemon, bash, linux, debian

Appending after a pattern from a file in sed

Shell Script (Bash) posted about 1 year ago by marko

Useful when you want to append the contents of a whole file into something you are sed’ing. Be careful if you use -i, it will replace the working file.

   1  sed -i "/attr_accessor :config/r ../patch_for_application.rb.tmpl" app/controllers/application.rb

Tagged sed, linux, bash

Automatically giving predetermined answers to a program.

Shell Script (Bash) posted about 1 year ago by marko

Sometimes you want to automate the running of a script or program, but the darn thing keeps asking questions and expects interactive answers. Shell scripting makes it easy to give the answers automatically, using a stream redirection from a file.

   1  ./qurious_program < predetermined_answers.txt

Tagged script automation, bash, linux

Colorize grep

Shell Script (Bash) posted about 1 year ago by marko

Put this export in .bashrc to make grep colorize the text it found.

   1  export GREP_OPTIONS='--color=auto'

Tagged grep, bash, color, linux

Bulk renaming of files

Shell Script (Bash) posted about 1 year ago by marko

Rename the files in a directory by replacing a space with an underscore. The rename program comes with most modern Linux distros.

   1  rename 's/\ /_/g' *.*

Tagged rename, regexp, bulk, filename, bash, perl, linux