Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-20 of 120 total

Morse decode using sed

This is a short Morse code decoder written as a shellscript using sed.

The Morse coded text should be in $text, and should be written with spaces between the letters.

echo $text\  | tr . 0 | sed -e {s/0----\ /1/g} -e {s/00---\ /2/g} -e {s/000--\ /3/g} -e {s/000-\ /4/g} -e {s/00000\ /5/g} -e {s/-0000\ /6/g} -e {s/--000\ /7/g} -e {s/---00\ /8/g} -e {s/----0\ /9/g} -e {s/-----\ /0/g} \
	| sed -e {s/-0-0\ /c/g} -e {s/-000\ /b/g} -e {s/00-0\ /f/g} -e {s/0000\ /h/g} -e {s/0---\ /j/g} -e {s/0-00\ /l/g} -e {s/0--0\ /p/g} -e {s/--0-\ /q/g} -e {s/000-\ /v/g} -e {s/-00-\ /x/g} -e {s/-0--\ /y/g} -e {s/--00\ /z/g} \
	| sed -e {s/0--\ /w/g} -e {s/-00\ /d/g} -e {s/--0\ /g/g} -e {s/-0-\ /k/g} -e {s/---\ /o/g} -e {s/0-0\ /r/g} -e {s/000\ /s/g} -e {s/00-\ /u/g} \
	| sed -e {s/0-\ /a/g} -e {s/00\ /i/g} -e {s/--\ /m/g} -e {s/-0\ /n/g} \
	| sed -e {s/0\ /e/g} -e {s/-\ /t/g}

Recursively Remove .DS_Store files in OS X (More optimized)

Recently, I saw a this snippet :
alias rmdsstores='find ./ -type f | grep .DS_Store | xargs rm'

But this one is not very optimized, because you have two pipes with command call.

You can do exactly the same, with only the 'find' command, here the code :
alias rmdsstores='find . -name *.DS_Store -type f -exec rm {} \;'

Recursively Remove .DS_Store files in OS X

This shell alias will recursively remove all .DS_Store files from the current directory up.

alias rmdsstores='find ./ -type f | grep .DS_Store | xargs rm'

Add many users in a Debian system v0.2

Create many users in a Debian system using a text file as argument

#!/bin/bash

#******************************************************************************#
# AddManyUsers-deb.sh - Create many users in a Debian system using a text file #
# 			as argument					       #
#   Copyright (C) 2008 - written by flynets - <flynets<at>autistici<dot>org>   # 
#   AddManyUsers-deb is free software: you can redistribute it and/or modify   #
#   it under the terms of the GNU General Public License as published by       #
#   the Free Software Foundation, either version 3 of the License, or          #
#   any later version.							       #
#									       #
#   AddManyUsers-deb is distributed in the hope that it will be useful,	       #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of             #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the	       #
#   GNU General Public License for more details.			       #
#									       #
#   You should have received a copy of the GNU General Public License	       #
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.      #
#******************************************************************************#

# Checks if you have the right privileges
if [ "$USER" = "root" ]
then

# CHANGE THIS PARAMETERS FOR A PARTICULAR USE
PERS_HOME="/home/"
PERS_SH="/bin/bash"

   # Checks if there is an argument
   [ $# -eq 0 ] && { echo >&2 ERROR: You may enter as an argument a text file containing users, one per line. ; exit 1; }
   # checks if there a regular file
   [ -f "$1" ] || { echo >&2 ERROR: The input file does not exists. ; exit 1; }
   TMPIN=$(mktemp)
   # Remove blank lines and delete duplicates 
   sed '/^$/d' "$1"| sort -g | uniq > "$TMPIN"
   
   NOW=$(date +"%Y-%m-%d-%X")
   LOGFILE="AMU-log-$NOW.log"
   
   for user in $(more "$TMPIN"); do
      # Checks if the user already exists.
      cut -d: -f1 /etc/passwd | grep "$user" > /dev/null
      OUT=$?
      if [ $OUT -eq 0 ];then
   	 echo >&2 "ERROR: User account: \"$user\" already exists."
	 echo >&2 "ERROR: User account: \"$user\" already exists." >> "$LOGFILE"
      else
	 # Create a new user
         /usr/sbin/useradd -d "$PERS_HOME""$user" -s "$PERS_SH" -m "$user"
	 # gpw must be installed on debian system
	 pass=$(gpw 1 8)
         echo $user:$pass | chpasswd
	 # save user and password in a file
	 echo -e $user"\t"$pass >> "$LOGFILE"
	 echo "The user \"$user\" has been created and has the password: $pass"
      fi
   done
   rm -f "$TMPIN"
   exit 0
else
   echo >&2 "ERROR: You must be a root user to execute this script."
   exit 1
fi

Add many users in a Red Hat system v0.2

Create many users in a RedHat system using a text file as argument
#!/bin/bash

#******************************************************************************#
# AddManyUsers-rh.sh - Create many users in a Red Hat system using a text file #
# 			as argument					       #
#   Copyright (C) 2008 - written by flynets - <flynets<at>autistici<dot>org>   # 
#   AddManyUsers-rh is free software: you can redistribute it and/or modify    #
#   it under the terms of the GNU General Public License as published by       #
#   the Free Software Foundation, either version 3 of the License, or          #
#   any later version.							       #
#									       #
#   AddManyUsers-rh is distributed in the hope that it will be useful,	       #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of             #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the	       #
#   GNU General Public License for more details.			       #
#									       #
#   You should have received a copy of the GNU General Public License	       #
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.      #
#******************************************************************************#

# Checks if you have the right privileges
if [ "$USER" = "root" ]
then

# CHANGE THIS PARAMETERS FOR A PARTICULAR USE
PERS_HOME="/home/"
PERS_SH="/bin/bash"

   # Checks if there is an argument
   [ $# -eq 0 ] && { echo >&2 ERROR: You may enter as an argument a text file containing users, one per line. ; exit 1; }
   # checks if there a regular file
   [ -f "$1" ] || { echo >&2 ERROR: The input file does not exists. ; exit 1; }
   TMPIN=$(mktemp)
   # Remove blank lines and delete duplicates 
   sed '/^$/d' "$1"| sort -g | uniq > "$TMPIN"
   
   NOW=$(date +"%Y-%m-%d-%X")
   LOGFILE="AMU-log-$NOW.log"
   
   for user in $(more "$TMPIN"); do
      # Checks if the user already exists.
      cut -d: -f1 /etc/passwd | grep "$user" > /dev/null
      OUT=$?
      if [ $OUT -eq 0 ];then
   	 echo >&2 "ERROR: User account: \"$user\" already exists."
	 echo >&2 "ERROR: User account: \"$user\" already exists." >> "$LOGFILE"
      else
	 # Create a new user
         /usr/sbin/useradd -d "$PERS_HOME""$user" -s "$PERS_SH" -m "$user"
	 # passwdgen must be installed
	 pass=$(passwdgen -paq --length 8)
         echo $pass | passwd --stdin $user
	 # save user and password in a file
	 echo -e $user"\t"$pass >> "$LOGFILE"
	 echo "The user \"$user\" has been created and has the password: $pass"
      fi
   done
   rm -f "$TMPIN"
   exit 0
else
   echo >&2 "ERROR: You must be a root user to execute this script."
   exit 1
fi

Reads log files

// Map directory structure and then read logs extract results.

!#/usr/bin/bash

for line in `ls /netapp/XXXXXX0/XXXXXX_logs`
	do 
	for line2 in `ls  /netapp/XXXXXX0/XXXXXX_logs/$line`
			do  echo /netapp/XXXXXX0/XXXXXX_logs/$line/$line2 
	done	
done | grep 2007-11-29 > results.dat

!#/usr/bin/bash
for res in `cat results.dat`
 do
 echo -n $res 
 echo -n " "
 grep -c ",i," $res/*
done

Sed substitution

// Sed used to replace a string

#!/bin/bash

for line in `ls`
do
        sed -e 's/Spring/Regular Season/g' line > tmp
        mv tmp line
        echo -n $line
        grep Regular $line
done

Email in Bash

//Bash email
#!/bin/bash

function email
{
echo "this is a test" | mail -s "test" "$1"
}

if [ "$1" = "" ]; then
echo "nothing in param"
else
echo "sending mail"

#emailing attachment
#uuencode statsNbaAllStarSim statssim | mail -s "test attachement" atayebali@foxsports.com
#uuencode <filename that will be send> <name for attached file that is sent> |

#passing the param to function
email $1
fi

add a folder to PATH, but not if it is already in PATH

Often times one uses a single .profile or .bashrc across several hosts. These hosts may be differently configured, with varying directory locations, and different initial PATH values from /etc/profile. In situations like this, setting PATH the traditional way -- "PATH=$PATH:/path/to/some/stuff" -- can result in a cumbersomely long PATH that includes inaccessible or duplicate folders.

The snippet below nicely handles this problem. It has been tested on Solaris and Linux.


AddPath ()
# Add argument to $PATH if:
# - it is not already present
# - it is a directory
# - we have execute permission on it
#
# This snippet is public domain; you may use it freely.  Death to copyright, patents, 
# and all other forms of intellectual monopoly.  
#
{
  _folder=$1
  echo " $PATH " | sed 's/:/ /g' | grep " $_folder " > /dev/null
  [ $? -ne 0 ] && [ -d $_folder ] && [ -x $_folder ] && PATH=$PATH:$_folder
  export PATH
}

# Add some common paths:
AddPath /usr/bin
AddPath /usr/local/bin
AddPath /opt/somepackage/bin

dynamic rails img headers

// description of your code here

find views -name [a-z]\*rhtml | xargs -n1 grep -H "@page_title" | grep -v "<%" | sed "s/\@page_title = //" | sed "s/rhtml/png/" | sed "s:views/::" | sed "s:/:_:g" | sed "s:^:public/images/beta/headers/hdr_:" | sed "s/  //g"
« Newer Snippets
Older Snippets »
Showing 11-20 of 120 total