├── Contributors ├── LICENSE ├── README ├── awk ├── disk_space_usage.sh └── list_all_ip.sh ├── check_gmail.sh ├── create-project.sh ├── curl └── time_http_response.sh ├── ftp └── put_file.sh ├── git ├── git_all_add.sh └── git_create_branch.sh ├── kill_updatedb.sh ├── operations_on_data_types ├── join_string.sh └── split_string.sh ├── rss └── rss_reader.sh ├── sed └── tree.sh ├── take_screenshot.sh └── work_with_files ├── all_file_lower_case.sh ├── remove_empty_files.sh ├── remove_tmp_files.sh └── show_full_file_path.sh /Contributors: -------------------------------------------------------------------------------- 1 | if you want to add your usefull script in collection, 2 | fork https://github.com/onlyshk/bash-snippets and pull-request. 3 | 4 | Contributers: 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) <2010> 2 | 3 | This program is free software: you can redistribute it and/or modify 4 | it under the terms of the GNU General Public License as published by 5 | the Free Software Foundation, either version 3 of the License, or 6 | (at your option) any later version. 7 | 8 | This program is distributed in the hope that it will be useful, 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | GNU General Public License for more details. 12 | 13 | You should have received a copy of the GNU General Public License 14 | along with this program. If not, see . 15 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | bash-snippets - it is usefull snippets in shell. 2 | 3 | Wiki - https://github.com/onlyshk/bash-snippets/wiki 4 | Contribute - https://github.com/onlyshk/bash-snippets#fork_box 5 | -------------------------------------------------------------------------------- /awk/disk_space_usage.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | read -p "Enter disk space must be usage: " disk; 4 | 5 | usage=`df -h | awk '{print $5}' | sed -e N -e 's/\n/ /' | awk '{print $2}' | tr -d % -s "\n"` 6 | devnm=`df -h | awk '{print $1}' | sed -e N -e 's/\n/ /' | awk '{print $2}' | tr -s "\n"` 7 | str="=============================" 8 | 9 | if [ $usage -ge $disk ]; then 10 | info="Disk usage for $devnm is more than $disk , Current Disk usage is $usage % " 11 | echo -e "$str\n$info\n$str" |mail -s "Alert: Disk Usage for `hostname` on `date`" admin@example.com 12 | 13 | else 14 | info="Disk Usage is $usage% for $devnm" 15 | # echo -e "$str\n$info\n$str" | mail -s "Alert: Disk Usage for `hostname` on `date`" admin@example.com 16 | fi 17 | -------------------------------------------------------------------------------- /awk/list_all_ip.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | d=$1 3 | OUT=/tmp/spam.ip.$$ 4 | HTTPDLOG="/www/$d/var/log/httpd/access.log" 5 | [ $# -eq 0 ] && { echo "Usage: $0 domain-name"; exit 999; } 6 | if [ -f $HTTPDLOG ]; 7 | then 8 | awk '{print}' $HTTPDLOG >$OUT 9 | awk '{ print $1}' $OUT | sort -n | uniq -c | sort -n 10 | else 11 | echo "$HTTPDLOG not found. Make sure domain exists and setup correctly." 12 | fi 13 | /bin/rm -f $OUT 14 | -------------------------------------------------------------------------------- /check_gmail.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | read -p "Input your name in gmail: " username; 4 | read -p "Input your password in gmail: " password; 5 | 6 | echo -e "Checking for new messages... \c" 7 | 8 | atomlines=`wget -T 3 -t 1 -q --secure-protocol=TLSv1 \ 9 | --no-check-certificate \ 10 | --user=$username --password= $password \ 11 | https://mail.google.com/mail/feed/atom -O - \ 12 | | wc -l` 13 | 14 | echo -e "\r\c" 15 | 16 | [ $atomlines -gt "8" ] \ 17 | && echo -e " You have new gmail." \ 18 | || echo -e " No new gmail." 19 | -------------------------------------------------------------------------------- /create-project.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ -z `which git` ]; then 4 | echo 'You have to install git first.' 5 | return 6 | fi 7 | 8 | template_path="$1" 9 | echo "Now we are going to create a new project for you". 10 | if [ -z "$template_path" ]; then 11 | echo "Enter the desired path of your new project dir:" 12 | read template_path 13 | fi 14 | 15 | # switch to the new project dir 16 | cd "$template_path" 17 | # remove unnecessary files 18 | mv tmp/template/* . 19 | rm -rf tmp 20 | 21 | echo 22 | echo Initializing your new project... 23 | 24 | echo 'Enter the binary name of your program. This is the name of the binary flie installed to /usr/bin:' 25 | read bin_name 26 | if [ -z "$bin_name" ]; then 27 | echo 'Please enter a valid name.' 28 | return 29 | fi 30 | 31 | echo 'Enter a human readable name of your program ': 32 | read prog_name 33 | if [ -z "$prog_name" ]; then 34 | echo 'Please enter a valid name.' 35 | return 36 | fi 37 | 38 | echo 'Enter a short description for your program (such as "A tool to configure mouse and keyboard")': 39 | read prog_desc 40 | if [ -z "$prog_desc" ]; then 41 | echo 'Please enter a valid description.' 42 | return 43 | fi 44 | 45 | bin_name_=`echo $bin_name | sed s/-/_/`_ 46 | 47 | for f in `ls ./configure.ac ./src/*.c ./src/*.am ./data/*.am ./data/*.in ./data/ui/*.am ./data/ui/*.in`; do 48 | sed -e s/template_/"$bin_name_"/ -e s/template/"$bin_name"/ -e s/Template/"$prog_name"/ -e "s/Template Description/$prog_desc/" "$f" > "$f".tmp 49 | mv "$f".tmp "$f" 50 | done 51 | 52 | # rename files 53 | mv src/template.c src/"$bin_name".c 54 | mv data/template.desktop.in data/"$bin_name".desktop.in 55 | 56 | # remove git files. 57 | rm -rf .git 58 | 59 | echo Done! 60 | -------------------------------------------------------------------------------- /curl/time_http_response.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | start=$(date +%s) 4 | 5 | curl http://www.example.com 6 | 7 | stop=$(date +%s) 8 | 9 | let total_time=stop-start 10 | 11 | echo $total_time 12 | 13 | -------------------------------------------------------------------------------- /ftp/put_file.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | FILE_NAME="path_of_file" 4 | 5 | HOST='IP Addr. of ftp server' 6 | 7 | USER='user_name' 8 | 9 | PASSWD='user_passwd' 10 | 11 | ftp -nv <&2 Usage: $0 branch_name 5 | exit 127 6 | fi 7 | 8 | branch_name=$1 9 | git push origin master:refs/heads/$branch_name 10 | echo "git push origin master:refs/heads/$branch_name" 11 | git fetch origin 12 | git checkout --track -b $branch_name origin/$branch_name 13 | git pull 14 | -------------------------------------------------------------------------------- /kill_updatedb.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | PIDS=`ps ax | grep 'updatedb\|find' | grep -v grep | sort | awk 3 | '{print $1}' | perl -ne 'chomp;print "$_ "'` 4 | if [ "$PIDS" ] 5 | then kill $PIDS 6 | fi 7 | -------------------------------------------------------------------------------- /operations_on_data_types/join_string.sh: -------------------------------------------------------------------------------- 1 | #!usr/bin/shell 2 | 3 | function join () { 4 | local sep=$1 5 | shift 6 | builtin echo "$1$(shift ; for x do builtin echo -n "$sep$x"; done)"; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /operations_on_data_types/split_string.sh: -------------------------------------------------------------------------------- 1 | #!usr/bin/shell 2 | 3 | function split () { 4 | local IFS="$1" 5 | builtin echo $(for x in $2; do builtin echo -n "$x "; done); 6 | } 7 | 8 | -------------------------------------------------------------------------------- /rss/rss_reader.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | declare -i count 4 | ver=0.4 5 | choise=endless 6 | browser=none 7 | 8 | # 9 | # RDF/RSS reader 10 | # 11 | # Pretty lame actualy.. 12 | # Could have used some functions instead of c-and-p 13 | # Might make it to smallest rdf/rss reader 14 | # For updates check http://etv.cx/~the_jinx/rssread 15 | # 16 | # Anne Jan Brouwer (the_JinX) 17 | # last edited 2004-05-15 18 | 19 | echo "rss reader $ver" 20 | echo 21 | 22 | # usage 23 | if [[ -z $1 || $1 == "--help" ]]; then 24 | echo "usage: $0 url (browser)" 25 | echo "will use elinks, links, lynx or a fallback;" 26 | echo "if no browser set.." 27 | exit 28 | fi 29 | 30 | # make tempfile 31 | if [[ -e `which tempfile` ]]; then 32 | tempfile=$(tempfile -p rss) 33 | else 34 | tempfile="/tmp/rssread" 35 | touch $tempfile 36 | fi 37 | 38 | # download 39 | wget $1 -O $tempfile 40 | if [[ $? != 0 ]]; then 41 | echo "wget error or wget not installed" 42 | rm $tempfile 43 | exit 44 | fi 45 | 46 | # shave titles 47 | cat $tempfile | grep "<title>" | cut -d ">" -f 2 | cut -d "<" -f 1 > $tempfile"title" 48 | # shave url 49 | cat $tempfile | grep "<link>" | cut -d ">" -f 2 | cut -d "<" -f 1 > $tempfile"link" 50 | 51 | # count links and titles 52 | titles=$( wc -l $tempfile"title" | cut -d " " -f 1 ) 53 | links=$( wc -l $tempfile"link" | cut -d " " -f 1 ) 54 | if [[ $titles == 0 || $urls == 0 ]]; then 55 | echo "no good.." 56 | rm $tempfile* 57 | exit 58 | fi 59 | 60 | # check if titles match urls 61 | if [[ $titles -gt $links ]]; then 62 | echo "more titles then links" 63 | fi 64 | if [[ $titles -lt $links ]]; then 65 | echo "less titles then links" 66 | fi 67 | 68 | # The MAIN Loop 69 | 70 | while [[ $choice != "" || $choice != 0 ]]; do 71 | clear 72 | # show title 73 | echo "rss reader $ver" 74 | echo "reading $1" 75 | echo 76 | 77 | # list items 78 | count=1 79 | while [[ $count -lt $titles ]]; do 80 | # print titles 81 | line=$( cat $tempfile"title" | head -n $count | tail -n 1 ) 82 | printf "%s\t" $count 83 | echo $line 84 | count=$count+1 85 | done 86 | 87 | # ask 88 | echo 89 | echo "Which would you like to read ?" 90 | echo "0, q and x quit." 91 | read choice 92 | choice=$( echo $choice | tr A-Z a-z ) 93 | until [[ $choice != "" && $choice -gt 0 && $choice -le $titles ]]; do 94 | if [[ $choice == 0 || $choice == "q" || $choice == "x" ]]; then 95 | echo "bye.." 96 | rm $tempfile* 97 | exit 98 | else 99 | echo "BURP" 100 | fi 101 | read choice 102 | choice=$( echo $choice | tr A-Z a-z ) 103 | done 104 | 105 | # get link 106 | link=$( cat $tempfile"link" | head -n $choice | tail -n 1) 107 | link=${link//&/&} 108 | #link=${link//&/%26} 109 | 110 | # browser 111 | if [[ -e $( which lynx 2> /dev/null ) ]] 112 | then 113 | browser=lynx 114 | fi 115 | if [[ -e $( which links 2> /dev/null ) ]] 116 | then 117 | browser=links 118 | fi 119 | if [[ -e $( which elinks 2> /dev/null ) ]] 120 | then 121 | browser=elinks 122 | fi 123 | if [[ $2 != "" ]]; then 124 | if [[ -e $( which $2 2> /dev/null ) ]] 125 | then 126 | browser=$2 127 | else 128 | echo "$2 does not seem to be a browser :(" 129 | fi 130 | fi 131 | 132 | # showtime 133 | if [[ $browser == none ]]; then 134 | if [[ -e $( which sed ) ]]; then 135 | wget -q -O - $link | sed -e 's/<.*>//' | sed -e '/^$/d' | less 136 | else 137 | wget -q -O - $link | less 138 | fi 139 | else 140 | $browser "$link" 141 | if [[ $? != 0 ]]; then 142 | echo "browser error" 143 | read -s -n1 144 | fi 145 | fi 146 | done 147 | 148 | #remove tempfiles 149 | rm $tempfile* 150 | 151 | #EOF 152 | 153 | -------------------------------------------------------------------------------- /sed/tree.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' 4 | 5 | -------------------------------------------------------------------------------- /take_screenshot.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | prefix="$HOME/screenshot" 3 | date="$(date -I)" 4 | type='png' 5 | quality='90' 6 | wnd='-window root' 7 | 8 | function help(){ 9 | cat << EOF 10 | Usage: `basename $0` [options] [filename] 11 | Options: 12 | -f fullscreen shot (default) 13 | -r region shot 14 | -w single window shot 15 | EOF 16 | exit 1 17 | } 18 | 19 | while getopts fwr opt; do 20 | case "$opt" in 21 | f) wnd='-window root' ;; 22 | w) wnd="-window $(xwininfo | awk '/Window\ id:/{print $4}')" ;; 23 | r) wnd='' ;; 24 | ?) help ;; 25 | esac 26 | done 27 | 28 | shift $((OPTIND-1)) 29 | 30 | if [ "x$1" == "x" ] 31 | then 32 | i=0 33 | name=${prefix}_$date.$type 34 | while [ -f "$name" ]; do 35 | i=$(($i+1)) 36 | name=${prefix}_${date}_$i.$type 37 | done 38 | else 39 | name=$1 40 | fi 41 | 42 | import $wnd -quality $quality $name 43 | -------------------------------------------------------------------------------- /work_with_files/all_file_lower_case.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for i in * ; do mv $i `echo $i | tr [A-Z] [a-z]` ; done 4 | -------------------------------------------------------------------------------- /work_with_files/remove_empty_files.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | find . -empty -type f -delete 4 | 5 | -------------------------------------------------------------------------------- /work_with_files/remove_tmp_files.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Remove all tmp files in dir 4 | # which end in ~ 5 | 6 | echo "Input dir in which remove all temp files: " 7 | read DIR; 8 | find "$DIR" -name "*~" -exec rm -f {} \; 9 | echo "All temp files are removed" 10 | 11 | -------------------------------------------------------------------------------- /work_with_files/show_full_file_path.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Shows the full path of files 4 | # good for copy pasting and for when 5 | # listing the full paths is necessary. 6 | 7 | for file in $(ls "$@"); do 8 | echo -n $(pwd) 9 | [[ $(pwd) != "/" ]] && echo -n / 10 | echo $file 11 | done 12 | --------------------------------------------------------------------------------