Filter unique values from third column, separated by space

   1 cat datax.log |  awk -F ' ' '/IMEI/{print $3}' | sort | uniq | wc -l

Filter unique values from third column, separated by :

   1 cat datax.log |  awk -F ':' '/IMEI/{print $3}' | sort | uniq | wc -l

Search for text in files recursively

   1 cd startFolder
   2 grep -H "textToSearch" * -R

Copy files to other destination (Win32)

@echo off
REM Mount drive
echo Mounting remote drive
net use z: \\192.168.1.1\backs password /USER:userx

REM copy current day files
echo Copying current day files
forfiles -p "I:" -s -m *FULL*.sqb -d +0 -c "cmd /c copy @path Z:\BACKUPS\@file"

echo Deleting files older than 7 days
REM delete files older than 7 days
forfiles -p "Z:\BACKUPS" -m *.sqb -d 7 -c "cmd /c del @path"

Gzip files that where last modified in 30 or more days (Win32)

forfiles /p "c:\logs" /d -30 /m *.log /c "cmd /c gzip @path "

Run task every day at 01:00 (Win32)

at 01:00 /every:M,T,W,Th,F,S,Su c:\task.bat

Block responses to pings

   1 #block responses to pings
   2 iptables -A INPUT -p icmp -m icmp --icmp-type echo-request -j DROP
   3 iptables -L
   4 #restore responses to pings
   5 iptables -F
   6 iptables -L

Zip SVN modified Java files

   1 #!/bin/sh
   2 # chmod 755 /usr/local/bin/modifiedJavaSvn.sh # on CygWin
   3 svn status | awk '/.java/{ print( gensub( /\\/,"/","g",$2) ); }' | xargs -i zip modifiedJavaFiles.zip {}
   4 unzip -v modifiedJavaFiles.zip

Deploy EAR script

   1 #!/usr/bin/bash
   2 # deploy EAR in remote JBoss . 
   3 # Must have pub SSH keys (authorized_keys) deployed on remote server
   4 # Can be used with cygwin
   5 FILEX=sample.ear
   6 DEST_DIR=/opt/jboss/server/default/deploy/
   7 ECLIPSE_PROJECT=/home/userx/workspace/sample-ear/
   8 
   9 deploy()
  10 {
  11   SERVER=$1
  12   USER=$2
  13   echo "Deploying in $SERVER ..."
  14 
  15   cd $PROJECT
  16   scp target/$FILEX $USER@$SERVER:/tmp
  17   ssh $USER@$SERVER -C "mv /tmp/$FILEX $DEST_DIR/$FILEX"
  18   ssh $USER@$SERVER -C "ls -lah $DEST_DIR"
  19   ssh $USER@$SERVER -C "chmod 666  $DEST_DIR/$FILEX"
  20   ssh $USER@$SERVER -C "/sbin/service jboss abort"
  21   ssh $USER@$SERVER -C "sudo /sbin/service jboss start"
  22 }
  23 
  24 deploy "example-server" "userx"

List JAR contents to file

Size of each directory inside current directory

Backup folders in HOME

   1 #!/bin/bash
   2 # cd ~
   3 # ln -s scripts/backup.sh backup.sh
   4 # sh backup.sh
   5 DATE=`date '+%Y%m%d%H%M%S'`
   6 FILENAME=~/backups/backup_$DATE.tar.gz
   7 cd ~
   8 mkdir -p ~/backups
   9 rm $FILENAME
  10 tar cvzf $FILENAME Documents GNUstep ThunderbirdMail Desktop .Skype scripts moin-1.9.7
  11 # tar tvzf backup_20150108103242.tar.gz
  12 

Get installed updates on Windows

wmic qfe list brief /format:texttablewsys > updates.txt

Find files modified less than an hour ago

find . -mtime -1h -name "*.cfg"

Lock file to allow only one instance of a script to run

   1 #!/bin/sh
   2 LOCK_FILE=$(echo "/var/tmp/lockTest.lck")
   3 PID=$$
   4 LOCKED=0
   5 COUNT=0
   6 echo "Trying to get lock to $PID in $LOCK_FILE"
   7 
   8 while [ $LOCKED -eq "0" ] && [ $COUNT -le "3" ]; do
   9     if ( set -o noclobber; echo "$PID" > "$LOCK_FILE" ) 2> /dev/null; then
  10         LOCKED=1
  11     else        
  12         sleep 1
  13         COUNT=$(($COUNT+1))
  14         echo "Trying to get lock to $PID in $LOCK_FILE, retry $COUNT"
  15     fi    
  16 done
  17 
  18 if [ $LOCKED -eq "0" ]; then
  19     echo "Unable to get lock to $PID with $COUNT retries"
  20     exit 1
  21 else
  22     echo "Lock acquired to $PID"    
  23 fi   
  24 #####################################
  25 sleep 10
  26 #####################################
  27 rm -f "$LOCK_FILE"
  28 LOCKED=0
  29 echo "Released lock to $PID"
  30 exit 0

awk , load file into array

   1 #! /bin/bash
   2 INPUTFILE="inputfile.csv"
   3 DATAFILE="data.csv"
   4 OUTFILE="output.csv"
   5 
   6 awk 'BEGIN {
   7 while (getline < "'"$INPUTFILE"'")
   8 {
   9 split($0,ft,",");
  10 id=ft[1];
  11 name=ft[2];
  12 
  13 key=id;
  14 data=name;
  15 nameArr[key]=data;
  16 }
  17 close("'"$INPUTFILE"'");
  18 
  19 while (getline < "'"$DATAFILE"'")
  20 {
  21 split($0,ft,",");
  22 id=ft[1]; # Id is the first column
  23 phone=ft[2]; # Phonenumber is the second
  24 name=nameArr[id]; # Get the name from the nameArr, using "id" as key
  25 print id","name","phone > "'"$OUTFILE"'"; # Print directly to the outputfile
  26 }
  27 }'

ShellScript (last edited 2015-07-30 14:28:33 by static-wan-bl2-240-198-rev)