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

All kinds of code

Case Switch
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	echo "enter something>>" 
   5  4	read var 
   6  5	 
   7  6	case $var in 
   8  7	1 ) echo "1" ;; 
   9  8	2 ) echo "2" ;; 
  10  9	an* ) echo "not" ;; 
  11  10	*tom ) echo "wow" ;; 
  12  11	*) echo "end" 
  13  12	esac 
  14  	view plain | print | ?
  15  #!/bin/bash echo "enter something>>" read var case $var in 1 ) echo "1" ;; 2 ) echo "2" ;; an* ) echo "not" ;; *tom ) echo "wow" ;; *) echo "end" esac

Find Files
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	find . -name "*" -size 0k > file1.txt 
   5  4	sed -e 's/\.\///g' file1.txt > file2.txt 
   6  5	for line in `cat 0sizenuggets1.txt`; do ls -l $line; done > finalResult.txt 
   7  6	uuencode finalResult.txt finalresult.txt | mail -s "size 0 nuggets" atayebali@foxsports.com 
   8  7	rm file1.txt file2.txt 
   9  	view plain | print | ?
  10  #!/bin/bash find . -name "*" -size 0k > file1.txt sed -e 's/\.\///g' file1.txt > file2.txt for line in `cat 0sizenuggets1.txt`; do ls -l $line; done > finalResult.txt uuencode finalResult.txt finalresult.txt | mail -s "size 0 nuggets" atayebali@foxsports.com rm file1.txt file2.txt

Get Time
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	wget -O tf http://nist.time.gov/timezone.cgi?Pacific/d/-8 
   5  4	grep '>[0-9][0-9]:[0-9][0-9]'  tf | cut -c63-70 
   6  5	echo "PST" 
   7  6	rm tf 
   8  	view plain | print | ?
   9  #!/bin/bash wget -O tf http://nist.time.gov/timezone.cgi?Pacific/d/-8 grep '>[0-9][0-9]:[0-9][0-9]' tf | cut -c63-70 echo "PST" rm tf

If Statement Example
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	echo "$1" 
   5  4	 
   6  5	if [ "$1" == hello ]; then 
   7  6	        echo "hello there" 
   8  7	else 
   9  8	        echo "By nice" 
  10  9	fi 
  11  10	 
  12  11	checking for directories 
  13  12	 
  14  13	if [ -d "DB/" ] 
  15  14	 then 
  16  15	        rm -rf DB 
  17  16	        echo "killing DB folder" 
  18  17	 fi 
  19  18	 
  20  19	checking for files 
  21  20	if [ -f "DB/" ] 
  22  21	 then 
  23  22	        rm -rf DB 
  24  23	        echo "killing DB folder" 
  25  24	 fi 
  26  	view plain | print | ?
  27  #!/bin/bash echo "$1" if [ "$1" == hello ]; then echo "hello there" else echo "By nice" fi checking for directories if [ -d "DB/" ] then rm -rf DB echo "killing DB folder" fi checking for files if [ -f "DB/" ] then rm -rf DB echo "killing DB folder" fi

Sed Example
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	for line in `ls` 
   5  4	do 
   6  5	        sed -e 's/Spring/Regular Season/g' line > tmp 
   7  6	        mv tmp line 
   8  7	        echo -n $line 
   9  8	        grep Regular $line 
  10  9	done 
  11  	view plain | print | ?
  12  #!/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

While Example
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	echo "enter number: " 
   5  4	read var 
   6  5	 
   7  6	while [ $var -le 10 ]; do 
   8  7	echo "var is $var" 
   9  8	var=$((var + 1)) 
  10  9	done 
  11  	view plain | print | ?
  12  #!/bin/bash echo "enter number: " read var while [ $var -le 10 ]; do echo "var is $var" var=$((var + 1)) done

Email Example
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	function email 
   5  4	{ 
   6  5	echo "this is a test" | mail -s "test" "$1" 
   7  6	} 
   8  7	 
   9  8	if [ "$1" = "" ]; then 
  10  9	echo "nothing in param" 
  11  10	else 
  12  11	echo "sending mail" 
  13  12	 
  14  13	#emailing attachment 
  15  14	#uuencode statsNbaAllStarSim statssim | mail -s "test attachement" atayebali@foxsports.com 
  16  15	#uuencode <filename that will be send> <name for attached file that is sent> | 
  17  16	 
  18  17	#passing the param to function 
  19  18	email $1 
  20  19	fi 
  21  	view plain | print | ?
  22  #!/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

Function Example
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	 
   5  4	function show_uptime 
   6  5	{ 
   7  6	    # Temporary function stub 
   8  7	        uptime 
   9  8	} 
  10  9	 
  11  10	      echo $(show_uptime) 
  12  	view plain | print | ?
  13  #!/bin/bash function show_uptime { # Temporary function stub uptime } echo $(show_uptime)

Command Line Args
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	 
   5  4	echo "Positional Parameters" 
   6  5	echo '$0 = ' $0 
   7  6	echo '$1 = ' $1 
   8  7	echo '$2 = ' $2 
   9  8	echo '$3 = ' $3 
  10  9	 
  11  10	if [ $1==hello ] ; then 
  12  11	echo "YO MAN" 
  13  12	fi 
  14  	view plain | print | ?
  15  #!/bin/bash echo "Positional Parameters" echo '$0 = ' $0 echo '$1 = ' $1 echo '$2 = ' $2 echo '$3 = ' $3 if [ $1==hello ] ; then echo "YO MAN" fi
  16  <code>
  17  Read User Input
  18  
  19  1	#!/bin/bash 
  20  2	 
  21  3	echo "Hello guy what is your name?" 
  22  4	read text 
  23  5	echo "Hello $text" 
  24  6	 
  25  7	echo $1 
  26  8	 
  27  9	echo "How old are you?" 
  28  10	if  read -t 5 response ; then 
  29  11	        echo "sweet" 
  30  12	else 
  31  13	        echo "sour" 
  32  14	 
  33  15	fi 
  34  16	 
  35  17	echo -n "type in secret" 
  36  18	if  read -t 5 response ; then 
  37  19	        echo "sweet" 
  38  20	fi 
  39  	view plain | print | ?
  40  #!/bin/bash echo "Hello guy what is your name?" read text echo "Hello $text" echo $1 echo "How old are you?" if read -t 5 response ; then echo "sweet" else echo "sour" fi echo -n "type in secret" if read -t 5 response ; then echo "sweet" fi

Create Cookie Script
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	function set_value 
   5  4	{ 
   6  5	       domain=( fox hotmail gmail ) 
   7  6	        echo "${domain[0]}" 
   8  7	 
   9  8	} 
  10  9	 
  11  10	 
  12  11	 
  13  12	for line in `cat description.dat` 
  14  13	        do 
  15  14	 
  16  15	        echo $line 
  17  16	        String=$String$line=$(set_value)\& 
  18  17	 
  19  18	done 
  20  19	 
  21  20	echo -e  "\n\n" 
  22  21	 
  23  22	echo $String 
  24  	view plain | print | ?
  25  #!/bin/bash function set_value { domain=( fox hotmail gmail ) echo "${domain[0]}" } for line in `cat description.dat` do echo $line String=$String$line=$(set_value)\& done echo -e "\n\n" echo $String

Introduce a new line
   1  
   2  1	#!/bin/bash 
   3  2	 
   4  3	#Introduce a new line after every '&' in the test.txt file 
   5  4	 
   6  5	sed -e 's/&/\n/g' test.txt 
   7  	view plain | print | ?
   8  #!/bin/bash #Introduce a new line after every '&' in the test.txt file sed -e 's/&/\n/g' test.txt 

You need to create an account or log in to post comments to this site.


Click here to browse all 5545 code snippets