Public
snipts » bash
showing 21-40 of 501 snipts for bash
-
∞ Run Linux command for a designated time
#!/bin/bash # thread - Copyright (C) 2009 Radek Suski <info@sigsiu.net> # license see http://www.gnu.org/licenses/lgpl-2.1.html GNU/LGPL. # You can use, redistribute this file and/or modify it under the terms of the GNU Lesser General Public License version 2.1 function usage { echo echo "Starting command with time limit" echo " Usage: thread [COMMAND]... [SECONDS]...[OPTIONS]" echo echo "If the command to execute consist of more then 1 word, embbed it within quotes" echo "Example: " echo " thread "sleep 25" 5 " echo "will start the sleep process for 25 seconds and kill it after 5 seconds" echo echo "Options" echo " -q quiet no countdown output" } function Sleep { clear if [ $1 -gt 39 ]; then steps=39 step=$[$1/40] else steps=$[$1 + 1] step=1 fi for (( c=1; c<=$1; c++ )) do i=$( ps --ppid $$ | grep $3 ) if ! [ "$i" ]; then echo echo echo "$4 $3 has been already terminated - exiting" echo echo exit 1 fi if [ "$2" != 1 ]; then echo -ne "\r [" Done=$[$1-$c] Done=$[$Done/$step] progress=$[$steps-$Done] for (( i=1; i<$progress; i++ )) do echo -n "=" done todo=$[$steps-$progress] if [ $todo -gt $steps ] || [ $todo -lt 0 ]; then todo=$steps fi for (( i=1; i<=$todo; i++ )) do echo -n "." done s=$[$1-$c] p=$[$c*100] p=$[$p/$1] echo -n "] $p % done. $s seconds to forced termination of "$4 $3". " fi sleep 1 done } for p in "$@" do if [ "$p" == "-q" ]; then quiet=1 fi done case $2 in *[!0-9]* ) echo "Time has to be a numeric value. "$2" is not all numeric" usage exit 1; ;; esac if [ "$1" ] && [ "$2" ] && [ "$1" != "--help" ] && [ "$1" != "--usage" ]; then clear # start the process exec $1 & &>/dev/null clear # get program name run=`echo $1 | awk -F " " '{ print $1 }'` # get proces id pid=`ps --ppid $$ | grep "$run" | awk -F " " '{ print $1 }'` if [ "$quiet" != 1 ]; then echo "Started "$1" for $2 seconds. PID is $pid" echo fi # go sleep Sleep "$2" "$quiet" "$pid" "$run" # after sleep if the process still running i=$( ps --ppid $$ | grep $pid ) if [ "$i" ]; then # display message if [ "$quiet" != 1 ]; then echo echo echo "Process $pid is still running. Killing now" fi # try to kill it while [ "$i" ] do kill -9 $pid i=$( ps --ppid $$ | grep $pid ) # if killed, output messgae if ! [ "$i" ]; then # display message echo "Process $pid has been killed after $2 seconds" echo echo # exit exit 1 fi i=$( ps --ppid $$ | grep $pid ) done fi else echo Unsupported request usage fi
-
∞ Replace directory symlink
# Given symlink, target, replace it with a symlink the the directory, source # Tested only with directories, not sure how this would work for files # s - symbolic link # f - if the target exists, unlink it # h - do not follow target symlink, required for replacing a directory symlink ln -sfh source target
-
∞ Moving around in bash
Line Navigation: Ctrl-E == end of line Ctrl-A == beginning of line Ctrl-K == Delete from cursor to end of line or if you are wise, just use vi for command history: $ set -o vi Then you can just use all your friendly neighborhood vi commands on your command line. Command History: up arrow == previous command down arrow == next command (if already in history)
-
∞ remove .svn folders
find . -name ".svn" -type d -exec rm -rf {} \;
-
∞ detect linux distro
#!/bin/sh # Detects which OS and if it is Linux then it will detect # which Linux Distribution. OS=`uname -s` REV=`uname -r` MACH=`uname -m` GetVersionFromFile() { VERSION=`cat $1 | tr "\n" ' ' | sed s/.*VERSION.*=\ // ` } if [ "${OS}" = "SunOS" ] ; then OS=Solaris ARCH=`uname -p` OSSTR="${OS} ${REV}(${ARCH} `uname -v`)" elif [ "${OS}" = "AIX" ] ; then OSSTR="${OS} `oslevel` (`oslevel -r`)" elif [ "${OS}" = "Linux" ] ; then KERNEL=`uname -r` if [ -f /etc/redhat-release ] ; then DIST='RedHat' PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//` REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//` elif [ -f /etc/SUSE-release ] ; then DIST=`cat /etc/SUSE-release | tr "\n" ' '| sed s/VERSION.*//` REV=`cat /etc/SUSE-release | tr "\n" ' ' | sed s/.*=\ //` elif [ -f /etc/mandrake-release ] ; then DIST='Mandrake' PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//` REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//` elif [ -f /etc/debian_version ] ; then DIST="Debian `cat /etc/debian_version`" REV="" fi if [ -f /etc/UnitedLinux-release ] ; then DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]" fi OSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})" fi echo ${OSSTR}
-
∞ search & apply new perm for files in pattern
#!/bin/bash # Search for files with pattern name defined on name var, then # apply new user, group and other perms. located in path old_user='gin'; new_user='shakka'; old_group='guest'; new_group='geek'; name='*.ogg' path='/home/gin/video/'; perm='700'; chmod ${perm} `find ${path} -name ${name} -user ${old_user} -group ${old_group}`; chown ${new_user}:${new_group} `find ${path} -name ${name} -user ${old_user} -group ${old_group}`; exit 0;
-
∞ sort filezilla server log entries by session id
#!/bin/bash # # entry example # # (1579051) 10/16/2009 0:02:13 AM - (not logged in) (192.168.1.11)> USER chris # # sort -k1n fzs-2009-10-16.log # does not work properly because when the session id and timestamp are the same # it goes on and tries to sort based on the message # if the timestamp granularity was high enough it would work (and so much faster!) # [ $# -ne 1 ] && echo "number of args required is 1" && exit 1 [ ! -e $1 ] && echo "file $1 does not exist" && exit 2 FILEPATH=`dirname $1` LOGNAME=`basename $1` LOGNAME_SORTED=${FILEPATH}/${LOGNAME}".sorted" SID_SORTED_FILENAME=${FILEPATH}/"fzlog_sid_sorted.$$" rm -f $LOGNAME_SORTED awk '{ split( $1, L, "\\(|\\)" ); print L[ 2 ]; }' $1 | sort | uniq > ${SID_SORTED_FILENAME} cat ${SID_SORTED_FILENAME} | while read line do grep "^($line)" $1 >> ${LOGNAME_SORTED} done rm -f ${SID_SORTED_FILENAME} exit 0
-
∞ filter the SSAHA2 result
#to filter the SSAHA2 result like #=================================================== #Matches For Query 20489 (913 bases): b201.1e11.f.b1 #=================================================== #Score Q_Name S_Name Q_Start Q_End #S_Start S_End Direction #Bases identity #ALIGNMENT::00 730 b201.1e11.f.b1 SC02 15 816 288648 #289465 F 802 97.51 913 #ALIGNMENT::00 730 b201.1e11.f.b1 SC05 15 816 34214 #35031 F 802 97.51 913 cat '/megx/home/shuang/result/ANME_vs_ANME1_SSAHA2/ANME_vs_ANME1_Endseq.txt' | egrep "ALIGNMENT" | sed 's/::/:/g' | sed 's/ \+/:/g' | cut -d: -f4,12
-
∞ count how many pdf or chm files are there in endnote
ls -R -l /Users/sixinghuang/Documents/My\ EndNote\ Library.Data | egrep "\.pdf|\.chm" | wc -l
-
∞ gits from server backup per ssh
scp -r -P 1234 user@192.168.0.1:~/git/* /g/git
-
∞ outdated drupal module with svn
## In a nutshell, how to update a module from Drupal contrib, ## on a production site that is under revision control (subversion). # If you turn on a module (that just happens to already be # present in the list on admin/build/modules), then you MUST # 1) go to the update status page # 2) find out if the module you just turned on is up to date # 3) if it is, fine (your done! stop here), # 4) if there are updates... then go to the drupal.org site first # to see if there are any "gotchas" for the upgrade. As you'll # be turning the module on for the first time on a new site, # then you shouldnt have to worry too much. # 5) To run the updates: login via ssh ssh username@server.com # 6) go to the right folder and execute drush update cd domains/example.com/public_html/sites/all/modules drush --uri=example.com update modulename # 7) confirm that the update was successful on the site # 8) then check in the code svn commit -u username -m "updated modulename to the latest version"
-
∞ Locate Large Files on Linux
Finds all files over 20,000KB (roughly 20MB) in size and presents their names and size in a human readable format. Replace "find /" with desired directory: find / -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }' -
∞ svn bash shortcuts
alias .s='svn stat' alias .c='svn ci -m' alias .u='svn up' alias .a='svn stat | grep "^\?" | awk "{print \$2}" | xargs svn add' alias .d='svn stat | grep "^\!" | awk "{print \$2}" | xargs svn delete'
-
∞ divide and conquer svn repo
# The following structure exists: # # originalrepository/ # project_name/ # trunk/ # branches/ # tags/ # # I want to move it to its *own* repository and give the following structure: # # newrepositoryname/ # trunk/ # branches/ # tags/ # export LOCAL='file:/' export SVN_DIR='/home/bluespark/svn' export SVN_OLD_REPO='originalrepository' export SVN_PROJ='project_name' export SVN_REPO='newrepositoryname' cd $SVN_DIR ## Create dumps mkdir $SVN_DIR/dumps svnadmin dump $SVN_OLD_REPO > dumps/$SVN_OLD_REPO.dump ## Filter the specific project_name out of the dump svndumpfilter include $SVN_PROJ \ --drop-empty-revs \ --preserve-revprops \ --renumber-revs \ < dumps/$SVN_OLD_REPO.dump \ > dumps/$SVN_PROJ.dump ## Create the new repository... ## NOTE: You may need to do this step in the cpanel!! ## Here is how to do it on the comand line: # svnadmin create $SVN_DIR/$SVN_REPO ## Load the new repository with the contents of the ## filtered dump file. svnadmin load $SVN_REPO < dumps/$SVN_PROJ.dump ## Move the contens of the project_name to the repo root. svn mv $LOCAL/$SVN_DIR/$SVN_REPO/$SVN_PROJ/branches \ $LOCAL/$SVN_DIR/$SVN_REPO/$SVN_PROJ/tags \ $LOCAL/$SVN_DIR/$SVN_REPO/$SVN_PROJ/trunk \ $LOCAL/$SVN_DIR/$SVN_REPO/ \ -m "moved $SVN_PROJ contents to root" ## Delete the old (empty) project directory. ## Note: you need to make sure the directory ## is really empty before deleting! svn del $SVN_DIR/$SVN_REPO/$SVN_PROJ -m "removed old project directory"
-
∞ Small script to show the current branch when we are inside a git repo.
#!/bin/bash GITOUT=`git branch 2>&1 | grep \* | awk '{ print $2; }'` if [[ $GITOUT ]]; then echo -n "($GITOUT)" else echo -n '' fi
-
∞ update drupal from subversion tags
# Update a Drupal minor revision using a patch file. # Creating a patch file between cvs tags on the server proved to be # impossible. I've found a way to do this with a subversion mirror # of Drupal core. cd ~/path/to/local/drupal # What is current version of Drupal? cat CHANGELOG.txt | grep Drupal | head -1 ## Drupal 6.13, 2009-07-01 # I'm using a Subversion mirror of Drupal core to create the patch. # (Thanks to the guys at subversible.com!) svn diff http://subversible.com/svn/drupal/tags/DRUPAL-6-13 \ http://subversible.com/svn/drupal/tags/DRUPAL-6-14 > d6-14.patch # The local version of drupal was created from a tarball, and the date # format inside the $Id$ (from CVS) is: YYYY/mm/dd HH:MM:SS, but in the # subversion patch it was YYYY-mm-dd HH:MM:SS. So I had to this quick # find/replace in order to get the patch to apply cleanly. perl -pi -e 's/(\d{4})-(\d{2})-(\d{2})\s(\d{2})/\1\/\2\/\3 \4/g' d6-14.patch # Check to see if you have any outstanding uncommitted local changes. If # there are some changes then execute the commit (commented out below). svn status #svn commit -m "latest changes prior to drupal core update" # Apply the patch. The patch from svn diff command should be in # unified diff format (-u) and the paths should be taken as they are so # files in subfolders are matched (-p0). patch -p0 -u <d6-14.patch #dont forget to commit the updates... svn commit -m "updated core drupal" #optionally, tag the updated code in the repository
-
∞ txt2html sed
Snipt has been removed. Please contact nick@lionburger.com.
-
∞ txt2html sed
Snipt has been removed. Please contact nick@lionburger.com.
-
∞ remove spaces
#!/bin/bash # original value my_var="Fuck World!"; # trim function function trim() { local _tmp=$@; _tmp=${_tmp/ /}; echo $_tmp; } # passing one argument new_var=$(trim $my_var); # modified value, without spaces echo $new_var;
-
∞ man pages color active
# manpages coloridas -- coloque no ~/.bashrc export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking export LESS_TERMCAP_md=$'\E[01;38;5;74m' # begin bold export LESS_TERMCAP_me=$'\E[0m' # end mode export LESS_TERMCAP_se=$'\E[0m' # end standout-mode export LESS_TERMCAP_so=$'\E[38;5;246m' # begin standout-mode - info box export LESS_TERMCAP_ue=$'\E[0m' # end underline export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline



Mac for Linux Geeks