miércoles, 31 de diciembre de 2014

Other Comparison Operators

7.3. Other Comparison Operators

A binary comparison operator compares two variables or quantities. Note that integer and string comparison use a different set of operators.
integer comparison
-eq
is equal to
if [ "$a" -eq "$b" ]
-ne
is not equal to
if [ "$a" -ne "$b" ]
-gt
is greater than
if [ "$a" -gt "$b" ]
-ge
is greater than or equal to
if [ "$a" -ge "$b" ]
-lt
is less than
if [ "$a" -lt "$b" ]
-le
is less than or equal to
if [ "$a" -le "$b" ]
<
is less than (within double parentheses)
(("$a" < "$b"))
<=
is less than or equal to (within double parentheses)
(("$a" <= "$b"))
>
is greater than (within double parentheses)
(("$a" > "$b"))
>=
is greater than or equal to (within double parentheses)
(("$a" >= "$b"))
string comparison
=

is equal to
if [ "$a" = "$b" ]
CautionNote the whitespace framing the =.
if [ "$a"="$b" ] is not equivalent to the above.
==
is equal to
if [ "$a" == "$b" ]
This is a synonym for =.
Note The == comparison operator behaves differently within a double-brackets test than within single brackets.
[[ $a == z* ]]   # True if $a starts with an "z" (pattern matching).
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).

[ $a == z* ]     # File globbing and word splitting take place.
[ "$a" == "z*" ] # True if $a is equal to z* (literal matching).

# Thanks, Stéphane Chazelas
!=
is not equal to
if [ "$a" != "$b" ]
This operator uses pattern matching within a [[ ... ]] construct.
<
is less than, in ASCII alphabetical order
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]
Note that the "<" needs to be escaped within a [ ] construct.
>
is greater than, in ASCII alphabetical order
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
Note that the ">" needs to be escaped within a [ ] construct.
See Example 27-11 for an application of this comparison operator.
-z
string is null, that is, has zero length
 String=''   # Zero-length ("null") string variable.

if [ -z "$String" ]
then
  echo "\$String is null."
else
  echo "\$String is NOT null."
fi     # $String is null.
-n
string is not null.
CautionThe -n test requires that the string be quoted within the test brackets. Using an unquoted string with ! -z, or even just the unquoted string alone within test brackets (see Example 7-6) normally works, however, this is an unsafe practice. Always quote a tested string. [1]
Example 7-5. Arithmetic and string comparisons
#!/bin/bash

a=4
b=5

#  Here "a" and "b" can be treated either as integers or strings.
#  There is some blurring between the arithmetic and string comparisons,
#+ since Bash variables are not strongly typed.

#  Bash permits integer operations and comparisons on variables
#+ whose value consists of all-integer characters.
#  Caution advised, however.

echo

if [ "$a" -ne "$b" ]
then
  echo "$a is not equal to $b"
  echo "(arithmetic comparison)"
fi

echo

if [ "$a" != "$b" ]
then
  echo "$a is not equal to $b."
  echo "(string comparison)"
  #     "4"  != "5"
  # ASCII 52 != ASCII 53
fi

# In this particular instance, both "-ne" and "!=" work.

echo

exit 0
Example 7-6. Testing whether a string is null
#!/bin/bash
#  str-test.sh: Testing null strings and unquoted strings,
#+ but not strings and sealing wax, not to mention cabbages and kings . . .

# Using   if [ ... ]

# If a string has not been initialized, it has no defined value.
# This state is called "null" (not the same as zero!).

if [ -n $string1 ]    # string1 has not been declared or initialized.
then
  echo "String \"string1\" is not null."
else  
  echo "String \"string1\" is null."
fi                    # Wrong result.
# Shows $string1 as not null, although it was not initialized.

echo

# Let's try it again.

if [ -n "$string1" ]  # This time, $string1 is quoted.
then
  echo "String \"string1\" is not null."
else  
  echo "String \"string1\" is null."
fi                    # Quote strings within test brackets!

echo

if [ $string1 ]       # This time, $string1 stands naked.
then
  echo "String \"string1\" is not null."
else  
  echo "String \"string1\" is null."
fi                    # This works fine.
# The [ ... ] test operator alone detects whether the string is null.
# However it is good practice to quote it (if [ "$string1" ]).
#
# As Stephane Chazelas points out,
#    if [ $string1 ]    has one argument, "]"
#    if [ "$string1" ]  has two arguments, the empty "$string1" and "]" 


echo


string1=initialized

if [ $string1 ]       # Again, $string1 stands unquoted.
then
  echo "String \"string1\" is not null."
else  
  echo "String \"string1\" is null."
fi                    # Again, gives correct result.
# Still, it is better to quote it ("$string1"), because . . .


string1="a = b"

if [ $string1 ]       # Again, $string1 stands unquoted.
then
  echo "String \"string1\" is not null."
else  
  echo "String \"string1\" is null."
fi                    # Not quoting "$string1" now gives wrong result!

exit 0   # Thank you, also, Florian Wisser, for the "heads-up".
Example 7-7. zmore
#!/bin/bash
# zmore

# View gzipped files with 'more' filter.

E_NOARGS=85
E_NOTFOUND=86
E_NOTGZIP=87

if [ $# -eq 0 ] # same effect as:  if [ -z "$1" ]
# $1 can exist, but be empty:  zmore "" arg2 arg3
then
  echo "Usage: `basename $0` filename" >&2
  # Error message to stderr.
  exit $E_NOARGS
  # Returns 85 as exit status of script (error code).
fi  

filename=$1

if [ ! -f "$filename" ]   # Quoting $filename allows for possible spaces.
then
  echo "File $filename not found!" >&2   # Error message to stderr.
  exit $E_NOTFOUND
fi  

if [ ${filename##*.} != "gz" ]
# Using bracket in variable substitution.
then
  echo "File $1 is not a gzipped file!"
  exit $E_NOTGZIP
fi  

zcat $1 | more

# Uses the 'more' filter.
# May substitute 'less' if desired.

exit $?   # Script returns exit status of pipe.
#  Actually "exit $?" is unnecessary, as the script will, in any case,
#+ return the exit status of the last command executed.
compound comparison
-a
logical and
exp1 -a exp2 returns true if both exp1 and exp2 are true.
-o
logical or
exp1 -o exp2 returns true if either exp1 or exp2 is true.
These are similar to the Bash comparison operators && and ||, used within double brackets.
[[ condition1 && condition2 ]]
The -o and -a operators work with the test command or occur within single test brackets.
if [ "$expr1" -a "$expr2" ]
then
  echo "Both expr1 and expr2 are true."
else
  echo "Either expr1 or expr2 is false."
fi
CautionBut, as rihad points out:
[ 1 -eq 1 ] && [ -n "`echo true 1>&2`" ]   # true
[ 1 -eq 2 ] && [ -n "`echo true 1>&2`" ]   # (no output)
# ^^^^^^^ False condition. So far, everything as expected.

# However ...
[ 1 -eq 2 -a -n "`echo true 1>&2`" ]       # true
# ^^^^^^^ False condition. So, why "true" output?

# Is it because both condition clauses within brackets evaluate?
[[ 1 -eq 2 && -n "`echo true 1>&2`" ]]     # (no output)
# No, that's not it.

# Apparently && and || "short-circuit" while -a and -o do not.
Refer to Example 8-3, Example 27-17, and Example A-29 to see compound comparison operators in action.

Notes

[1]As S.C. points out, in a compound test, even quoting the string variable might not suffice. [ -n "$string" -o "$a" = "$b" ] may cause an error with some versions of Bash if $string is empty. The safe way is to append an extra character to possibly empty variables, [ "x$string" != x -o "x$a" = "x$b" ] (the "x's" cancel out).

martes, 30 de diciembre de 2014

Mysql query resultset in bash script

http://unix.stackexchange.com/questions/111545/mysql-query-resultset-in-bash-script



mysql seems to output the results to a shell variable in a single line. One way round this is to write the contents to a temporary file, then process in a while loop.
EDIT On my system IFS="\n" before the mysql command (when the results are assigned to a shell variable) gives the correct multi-line output. e.g.
 IFS="\n"
 Total_results=$(mysql.....)
=============== End of Edit ==========================
#!/bin/bash
mysql --silent -h server-name -P 3306 -u username-ppassword -D dbname<<<"select URL  from Experiment where URL_Exists = 1" > tmp_results

while read URL
do 
   echo $URL
   var=$(curl -s --head $URL | head -n 1 | grep "HTTP/1.[01] [23]..")
   echo "$var"
   if [ -z "$var" ]
   then
     echo "Ok we do not have a valid link and the value needs to be updated as -1 here"
   else
     echo "we will update the value as 1 from here"
   fi
done < tmp_results
 
 
 
-------------------------------------- 
You don't need a variable or a temp file for this, just parse the mysql command directly by piping to the while loop:
#!/bin/bash
mysql <<<"select URL from Experiment where URL_Exists = 1" |

while IFS='\n' read URL
do
  echo $URL
  var=$(curl -s --head $URL | head -n 1 | grep "HTTP/1.[01] [23]..")
  echo "$var"
  if [ -z "$var" ]
  then
    echo "Ok we do not have a valid link and the value needs to be updated as -1 here"
  else
    echo "we will update the value as 1 from here"
  fi
done
 
 
 
 
 

domingo, 28 de diciembre de 2014

Array variables

http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html

http://www.thegeekstuff.com/2010/06/bash-array-tutorial/

$ cat arraymanip.sh
#! /bin/bash
Unix[0]='Debian'
Unix[1]='Red hat'
Unix[2]='Ubuntu'
Unix[3]='Suse'

echo ${Unix[1]}

$./arraymanip.sh
Red hat
 
 
 
#Example file
$ cat logfile
Welcome
to
thegeekstuff
Linux
Unix

$ cat loadcontent.sh
#!/bin/bash
filecontent=( `cat "logfile" `)

for t in "${filecontent[@]}"
do
echo $t
done
echo "Read file content!"

$ ./loadcontent.sh
Welcome
to
thegeekstuff
Linux
Unix
Read file content! 

Bash Infinite Loop Examples

by on January 20, 2011 · 9 comments· LAST UPDATED January 20, 2011
How do I write an infinite loop in Bash script under Linux or UNIX like operating systems?

An infinite loop is nothing but a sequence of instructions which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over. The syntax is as follows using the while loop:
Leer más »

martes, 16 de diciembre de 2014

Bash Loop

Next Previous Contents

7. Loops for, while and until

In this section you'll find for, while and until loops.
The for loop is a little bit different from other programming languages. Basically, it let's you iterate over a series of 'words' within a string.
The while executes a piece of code if the control expression is true, and only stops when it is false (or a explicit break is found within the executed code.
The until loop is almost equal to the while loop, except that the code is executed while the control expression evaluates to false.
If you suspect that while and until are very similar you are right.
Leer más »

miércoles, 10 de diciembre de 2014

Shell Scripting Tip: Find The Length Of a String Under UNIX / Linux Oses


by on August 29, 2007 · 21 comments· LAST UPDATED December 17, 2013
While writing a shell script you may want to find out the length of a string. While reading GNU expr command man page I found an interesting option:

expr length STRING
For example display the length of "nixcraft" word/string, enter:
 
expr length "nixcraft"
 
Sample outputs:
8

expr and POSIX

Please note that the expr command is not concerned with POSIX (open system standards based on Unix). You can try old good KSH/SH/Bash command as follows which should work with any UNIX-likeo operating systems such as FreeBSD / Solaris/ AIX/HP-UX and so on:
 
myVar="nixcraft"
echo "${#myVar}"
 
Sample outputs:
8

4 Bash If Statement Examples ( If then fi, If then else fi, If elif else fi, Nested if )


by Sasikala on June 21, 2010
Bash conditional statements perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. These statements are used to execute different parts of your shell program depending on whether certain conditions are true. The ability to branch makes shell scripts powerful.

In Bash, we have the following conditional statements:
  1. if..then..fi statement (Simple If)
  2. if..then..else..fi statement (If-Else)
  3. if..elif..else..fi statement (Else If ladder)
  4. if..then..else..if..then..fi..fi..(Nested if)
These are similar to the awk if statements we discussed earlier.

1. Bash If..then..fi statement

if [ conditional expression ]
then
 statement1
 statement2
 .
fi
This if statement is also called as simple if statement. If the given conditional expression is true, it enters and executes the statements enclosed between the keywords “then” and “fi”. If the given expression returns zero, then consequent statement list is executed.
if then fi example:
#!/bin/bash
count=100
if [ $count -eq 100 ]
then
  echo "Count is 100"
fi

2. Bash If..then..else..fi statement

If [ conditional expression ]
then
 statement1
 statement2
 .
else
 statement3
 statement4
 .
fi
If the conditional expression is true, it executes the statement1 and 2. If the conditional expression returns zero, it jumps to else part, and executes the statement3 and 4. After the execution of if/else part, execution resume with the consequent statements.
if then else fi example:
#!/bin/bash
count=99
if [ $count -eq 100 ]
then
  echo "Count is 100"
else
  echo "Count is not 100"
fi
Note: This article is part of the ongoing Bash Tutorial series.

3. Bash If..elif..else..fi

If [ conditional expression1 ]
then
 statement1
 statement2
 .
elif [ conditional expression2 ]
then
 statement3
 statement4
 .
.
.
else
 statement5
fi
You can use this if .. elif.. if , if you want to select one of many blocks of code to execute. It checks expression 1, if it is true executes statement 1,2. If expression1 is false, it checks expression2, and if all the expression is false, then it enters into else block and executes the statements in the else block.
if then elif then else fi example:
#!/bin/bash
count=99
if [ $count -eq 100 ]
then
  echo "Count is 100"
elif [ $count -gt 100 ]
then
  echo "Count is greater than 100"
else
  echo "Count is less than 100"
fi

4. Bash If..then..else..if..then..fi..fi..

If [ conditional expression1 ]
then
 statement1
 statement2
 .
else
 if [ conditional expression2 ]
 then
  statement3
  .
 fi
fi
If statement and else statement could be nested in bash. The keyword “fi” indicates the end of the inner if statement and all if statement should end with the keyword “fi”.
The “if then elif then else fi” example mentioned in above can be converted to the nested if as shown below.
#!/bin/bash
count=99
if [ $count -eq 100 ]
then
  echo "Count is 100"
else
  if [ $count -gt 100 ]
  then
    echo "Count is greater than 100"
  else
  echo "Count is less than 100"
  fi
fi

Advanced Bash-Scripting Guide


An in-depth exploration of the art of shell scripting

Mendel Cooper

10
10 Mar 2014
Revision History
Revision 6.505 Apr 2012Revised by: mc
'TUNGSTENBERRY' release
Revision 6.627 Nov 2012Revised by: mc
'YTTERBIUMBERRY' release
Revision 1010 Mar 2014Revised by: mc
'PUBLICDOMAIN' release
This tutorial assumes no previous knowledge of scripting or programming, yet progresses rapidly toward an intermediate/advanced level of instruction . . . all the while sneaking in little nuggets of UNIX® wisdom and lore. It serves as a textbook, a manual for self-study, and as a reference and source of knowledge on shell scripting techniques. The exercises and heavily-commented examples invite active reader participation, under the premise that the only way to really learn scripting is to write scripts.
This book is suitable for classroom use as a general introduction to programming concepts.
This document is herewith granted to the Public Domain. No copyright!

Dedication

For Anita, the source of all the magic
Table of Contents
Part 1. Introduction
1. Shell Programming!
2. Starting Off With a Sha-Bang
Part 2. Basics
3. Special Characters
4. Introduction to Variables and Parameters
5. Quoting
6. Exit and Exit Status
7. Tests
8. Operations and Related Topics
Part 3. Beyond the Basics
9. Another Look at Variables
10. Manipulating Variables
11. Loops and Branches
12. Command Substitution
13. Arithmetic Expansion
14. Recess Time
Part 4. Commands
15. Internal Commands and Builtins
16. External Filters, Programs and Commands
17. System and Administrative Commands
Part 5. Advanced Topics
18. Regular Expressions
19. Here Documents
20. I/O Redirection
21. Subshells
22. Restricted Shells
23. Process Substitution
24. Functions
25. Aliases
26. List Constructs
27. Arrays
28. Indirect References
29. /dev and /proc
30. Network Programming
31. Of Zeros and Nulls
32. Debugging
33. Options
34. Gotchas
35. Scripting With Style
36. Miscellany
37. Bash, versions 2, 3, and 4
38. Endnotes
38.1. Author's Note
38.2. About the Author
38.3. Where to Go For Help
38.4. Tools Used to Produce This Book
38.5. Credits
38.6. Disclaimer
Bibliography
A. Contributed Scripts
B. Reference Cards
C. A Sed and Awk Micro-Primer
C.1. Sed
C.2. Awk
D. Parsing and Managing Pathnames
E. Exit Codes With Special Meanings
F. A Detailed Introduction to I/O and I/O Redirection
G. Command-Line Options
G.1. Standard Command-Line Options
G.2. Bash Command-Line Options
H. Important Files
I. Important System Directories
J. An Introduction to Programmable Completion
K. Localization
L. History Commands
M. Sample .bashrc and .bash_profile Files
N. Converting DOS Batch Files to Shell Scripts
O. Exercises
O.1. Analyzing Scripts
O.2. Writing Scripts
P. Revision History
Q. Download and Mirror Sites
R. To Do List
S. Copyright
T. ASCII Table
Index
List of Examples
2-1. cleanup: A script to clean up log files in /var/log
2-2. cleanup: An improved clean-up script
2-3. cleanup: An enhanced and generalized version of above scripts.
3-1. Code blocks and I/O redirection
3-2. Saving the output of a code block to a file
3-3. Running a loop in the background
3-4. Backup of all files changed in last day
4-1. Variable assignment and substitution
4-2. Plain Variable Assignment
4-3. Variable Assignment, plain and fancy
4-4. Integer or string?
4-5. Positional Parameters
4-6. wh, whois domain name lookup
4-7. Using shift
5-1. Echoing Weird Variables
5-2. Escaped Characters
5-3. Detecting key-presses
6-1. exit / exit status
6-2. Negating a condition using !
7-1. What is truth?
7-2. Equivalence of test, /usr/bin/test, [ ], and /usr/bin/[
7-3. Arithmetic Tests using (( ))
7-4. Testing for broken links
7-5. Arithmetic and string comparisons
7-6. Testing whether a string is null
7-7. zmore
8-1. Greatest common divisor
8-2. Using Arithmetic Operations
8-3. Compound Condition Tests Using && and ||
8-4. Representation of numerical constants
8-5. C-style manipulation of variables
9-1. $IFS and whitespace
9-2. Timed Input
9-3. Once more, timed input
9-4. Timed read
9-5. Am I root?
9-6. arglist: Listing arguments with $* and $@
9-7. Inconsistent $* and $@ behavior
9-8. $* and $@ when $IFS is empty
9-9. Underscore variable
9-10. Using declare to type variables
9-11. Generating random numbers
9-12. Picking a random card from a deck
9-13. Brownian Motion Simulation
9-14. Random between values
9-15. Rolling a single die with RANDOM
9-16. Reseeding RANDOM
9-17. Pseudorandom numbers, using awk
10-1. Inserting a blank line between paragraphs in a text file
10-2. Generating an 8-character "random" string
10-3. Converting graphic file formats, with filename change
10-4. Converting streaming audio files to ogg
10-5. Emulating getopt
10-6. Alternate ways of extracting and locating substrings
10-7. Using parameter substitution and error messages
10-8. Parameter substitution and "usage" messages
10-9. Length of a variable
10-10. Pattern matching in parameter substitution
10-11. Renaming file extensions:
10-12. Using pattern matching to parse arbitrary strings
10-13. Matching patterns at prefix or suffix of string
11-1. Simple for loops
11-2. for loop with two parameters in each [list] element
11-3. Fileinfo: operating on a file list contained in a variable
11-4. Operating on a parameterized file list
11-5. Operating on files with a for loop
11-6. Missing in [list] in a for loop
11-7. Generating the [list] in a for loop with command substitution
11-8. A grep replacement for binary files
11-9. Listing all users on the system
11-10. Checking all the binaries in a directory for authorship
11-11. Listing the symbolic links in a directory
11-12. Symbolic links in a directory, saved to a file
11-13. A C-style for loop
11-14. Using efax in batch mode
11-15. Simple while loop
11-16. Another while loop
11-17. while loop with multiple conditions
11-18. C-style syntax in a while loop
11-19. until loop
11-20. Nested Loop
11-21. Effects of break and continue in a loop
11-22. Breaking out of multiple loop levels
11-23. Continuing at a higher loop level
11-24. Using continue N in an actual task
11-25. Using case
11-26. Creating menus using case
11-27. Using command substitution to generate the case variable
11-28. Simple string matching
11-29. Checking for alphabetic input
11-30. Creating menus using select
11-31. Creating menus using select in a function
12-1. Stupid script tricks
12-2. Generating a variable from a loop
12-3. Finding anagrams
15-1. A script that spawns multiple instances of itself
15-2. printf in action
15-3. Variable assignment, using read
15-4. What happens when read has no variable
15-5. Multi-line input to read
15-6. Detecting the arrow keys
15-7. Using read with file redirection
15-8. Problems reading from a pipe
15-9. Changing the current working directory
15-10. Letting let do arithmetic.
15-11. Showing the effect of eval
15-12. Using eval to select among variables
15-13. Echoing the command-line parameters
15-14. Forcing a log-off
15-15. A version of rot13
15-16. Using set with positional parameters
15-17. Reversing the positional parameters
15-18. Reassigning the positional parameters
15-19. "Unsetting" a variable
15-20. Using export to pass a variable to an embedded awk script
15-21. Using getopts to read the options/arguments passed to a script
15-22. "Including" a data file
15-23. A (useless) script that sources itself
15-24. Effects of exec
15-25. A script that exec's itself
15-26. Waiting for a process to finish before proceeding
15-27. A script that kills itself
16-1. Using ls to create a table of contents for burning a CDR disk
16-2. Hello or Good-bye
16-3. Badname, eliminate file names in current directory containing bad characters and whitespace.
16-4. Deleting a file by its inode number
16-5. Logfile: Using xargs to monitor system log
16-6. Copying files in current directory to another
16-7. Killing processes by name
16-8. Word frequency analysis using xargs
16-9. Using expr
16-10. Using date
16-11. Date calculations
16-12. Word Frequency Analysis
16-13. Which files are scripts?
16-14. Generating 10-digit random numbers
16-15. Using tail to monitor the system log
16-16. Printing out the From lines in stored e-mail messages
16-17. Emulating grep in a script
16-18. Crossword puzzle solver
16-19. Looking up definitions in Webster's 1913 Dictionary
16-20. Checking words in a list for validity
16-21. toupper: Transforms a file to all uppercase.
16-22. lowercase: Changes all filenames in working directory to lowercase.
16-23. du: DOS to UNIX text file conversion.
16-24. rot13: ultra-weak encryption.
16-25. Generating "Crypto-Quote" Puzzles
16-26. Formatted file listing.
16-27. Using column to format a directory listing
16-28. nl: A self-numbering script.
16-29. manview: Viewing formatted manpages
16-30. Using cpio to move a directory tree
16-31. Unpacking an rpm archive
16-32. Stripping comments from C program files
16-33. Exploring /usr/X11R6/bin
16-34. An "improved" strings command
16-35. Using cmp to compare two files within a script.
16-36. basename and dirname
16-37. A script that copies itself in sections
16-38. Checking file integrity
16-39. Uudecoding encoded files
16-40. Finding out where to report a spammer
16-41. Analyzing a spam domain
16-42. Getting a stock quote
16-43. Updating FC4
16-44. Using ssh
16-45. A script that mails itself
16-46. Generating prime numbers
16-47. Monthly Payment on a Mortgage
16-48. Base Conversion
16-49. Invoking bc using a here document
16-50. Calculating PI
16-51. Converting a decimal number to hexadecimal
16-52. Factoring
16-53. Calculating the hypotenuse of a triangle
16-54. Using seq to generate loop arguments
16-55. Letter Count"
16-56. Using getopt to parse command-line options
16-57. A script that copies itself
16-58. Exercising dd
16-59. Capturing Keystrokes
16-60. Preparing a bootable SD card for the Raspberry Pi
16-61. Securely deleting a file
16-62. Filename generator
16-63. Converting meters to miles
16-64. Using m4
17-1. Setting a new password
17-2. Setting an erase character
17-3. secret password: Turning off terminal echoing
17-4. Keypress detection
17-5. Checking a remote server for identd
17-6. pidof helps kill a process
17-7. Checking a CD image
17-8. Creating a filesystem in a file
17-9. Adding a new hard drive
17-10. Using umask to hide an output file from prying eyes
17-11. Backlight: changes the brightness of the (laptop) screen backlight
17-12. killall, from /etc/rc.d/init.d
19-1. broadcast: Sends message to everyone logged in
19-2. dummyfile: Creates a 2-line dummy file
19-3. Multi-line message using cat
19-4. Multi-line message, with tabs suppressed
19-5. Here document with replaceable parameters
19-6. Upload a file pair to Sunsite incoming directory
19-7. Parameter substitution turned off
19-8. A script that generates another script
19-9. Here documents and functions
19-10. "Anonymous" Here Document
19-11. Commenting out a block of code
19-12. A self-documenting script
19-13. Prepending a line to a file
19-14. Parsing a mailbox
20-1. Redirecting stdin using exec
20-2. Redirecting stdout using exec
20-3. Redirecting both stdin and stdout in the same script with exec
20-4. Avoiding a subshell
20-5. Redirected while loop
20-6. Alternate form of redirected while loop
20-7. Redirected until loop
20-8. Redirected for loop
20-9. Redirected for loop (both stdin and stdout redirected)
20-10. Redirected if/then test
20-11. Data file names.data for above examples
20-12. Logging events
21-1. Variable scope in a subshell
21-2. List User Profiles
21-3. Running parallel processes in subshells
22-1. Running a script in restricted mode
23-1. Code block redirection without forking
23-2. Redirecting the output of process substitution into a loop.
24-1. Simple functions
24-2. Function Taking Parameters
24-3. Functions and command-line args passed to the script
24-4. Passing an indirect reference to a function
24-5. Dereferencing a parameter passed to a function
24-6. Again, dereferencing a parameter passed to a function
24-7. Maximum of two numbers
24-8. Converting numbers to Roman numerals
24-9. Testing large return values in a function
24-10. Comparing two large integers
24-11. Real name from username
24-12. Local variable visibility
24-13. Demonstration of a simple recursive function
24-14. Another simple demonstration
24-15. Recursion, using a local variable
24-16. The Fibonacci Sequence
24-17. The Towers of Hanoi
25-1. Aliases within a script
25-2. unalias: Setting and unsetting an alias
26-1. Using an and list to test for command-line arguments
26-2. Another command-line arg test using an and list
26-3. Using or lists in combination with an and list
27-1. Simple array usage
27-2. Formatting a poem
27-3. Various array operations
27-4. String operations on arrays
27-5. Loading the contents of a script into an array
27-6. Some special properties of arrays
27-7. Of empty arrays and empty elements
27-8. Initializing arrays
27-9. Copying and concatenating arrays
27-10. More on concatenating arrays
27-11. The Bubble Sort
27-12. Embedded arrays and indirect references
27-13. The Sieve of Eratosthenes
27-14. The Sieve of Eratosthenes, Optimized
27-15. Emulating a push-down stack
27-16. Complex array application: Exploring a weird mathematical series
27-17. Simulating a two-dimensional array, then tilting it
28-1. Indirect Variable References
28-2. Passing an indirect reference to awk
29-1. Using /dev/tcp for troubleshooting
29-2. Playing music
29-3. Finding the process associated with a PID
29-4. On-line connect status
30-1. Print the server environment
30-2. IP addresses
31-1. Hiding the cookie jar
31-2. Setting up a swapfile using /dev/zero
31-3. Creating a ramdisk
32-1. A buggy script
32-2. Missing keyword
32-3. test24: another buggy script
32-4. Testing a condition with an assert
32-5. Trapping at exit
32-6. Cleaning up after Control-C
32-7. A Simple Implementation of a Progress Bar
32-8. Tracing a variable
32-9. Running multiple processes (on an SMP box)
34-1. Numerical and string comparison are not equivalent
34-2. Subshell Pitfalls
34-3. Piping the output of echo to a read
36-1. shell wrapper
36-2. A slightly more complex shell wrapper
36-3. A generic shell wrapper that writes to a logfile
36-4. A shell wrapper around an awk script
36-5. A shell wrapper around another awk script
36-6. Perl embedded in a Bash script
36-7. Bash and Perl scripts combined
36-8. Python embedded in a Bash script
36-9. A script that speaks
36-10. A (useless) script that recursively calls itself
36-11. A (useful) script that recursively calls itself
36-12. Another (useful) script that recursively calls itself
36-13. A "colorized" address database
36-14. Drawing a box
36-15. Echoing colored text
36-16. A "horserace" game
36-17. A Progress Bar
36-18. Return value trickery
36-19. Even more return value trickery
36-20. Passing and returning arrays
36-21. Fun with anagrams
36-22. Widgets invoked from a shell script
36-23. Test Suite
37-1. String expansion
37-2. Indirect variable references - the new way
37-3. Simple database application, using indirect variable referencing
37-4. Using arrays and other miscellaneous trickery to deal four random hands from a deck of cards
37-5. A simple address database
37-6. A somewhat more elaborate address database
37-7. Testing characters
37-8. Reading N characters
37-9. Using a here document to set a variable
37-10. Piping input to a read
37-11. Negative array indices
37-12. Negative parameter in string-extraction construct
A-1. mailformat: Formatting an e-mail message
A-2. rn: A simple-minded file renaming utility
A-3. blank-rename: Renames filenames containing blanks
A-4. encryptedpw: Uploading to an ftp site, using a locally encrypted password
A-5. copy-cd: Copying a data CD
A-6. Collatz series
A-7. days-between: Days between two dates
A-8. Making a dictionary
A-9. Soundex conversion
A-10. Game of Life
A-11. Data file for Game of Life
A-12. behead: Removing mail and news message headers
A-13. password: Generating random 8-character passwords
A-14. fifo: Making daily backups, using named pipes
A-15. Generating prime numbers using the modulo operator
A-16. tree: Displaying a directory tree
A-17. tree2: Alternate directory tree script
A-18. string functions: C-style string functions
A-19. Directory information
A-20. Library of hash functions
A-21. Colorizing text using hash functions
A-22. More on hash functions
A-23. Mounting USB keychain storage devices
A-24. Converting to HTML
A-25. Preserving weblogs
A-26. Protecting literal strings
A-27. Unprotecting literal strings
A-28. Spammer Identification
A-29. Spammer Hunt
A-30. Making wget easier to use
A-31. A podcasting script
A-32. Nightly backup to a firewire HD
A-33. An expanded cd command
A-34. A soundcard setup script
A-35. Locating split paragraphs in a text file
A-36. Insertion sort
A-37. Standard Deviation
A-38. A pad file generator for shareware authors
A-39. A man page editor
A-40. Petals Around the Rose
A-41. Quacky: a Perquackey-type word game
A-42. Nim
A-43. A command-line stopwatch
A-44. An all-purpose shell scripting homework assignment solution
A-45. The Knight's Tour
A-46. Magic Squares
A-47. Fifteen Puzzle
A-48. The Towers of Hanoi, graphic version
A-49. The Towers of Hanoi, alternate graphic version
A-50. An alternate version of the getopt-simple.sh script
A-51. The version of the UseGetOpt.sh example used in the Tab Expansion appendix
A-52. Cycling through all the possible color backgrounds
A-53. Morse Code Practice
A-54. Base64 encoding/decoding
A-55. Inserting text in a file using sed
A-56. The Gronsfeld Cipher
A-57. Bingo Number Generator
A-58. Basics Reviewed
A-59. Testing execution times of various commands
A-60. Associative arrays vs. conventional arrays (execution times)
C-1. Counting Letter Occurrences
J-1. Completion script for UseGetOpt.sh
M-1. Sample .bashrc file
M-2. .bash_profile file
N-1. VIEWDATA.BAT: DOS Batch File
N-2. viewdata.sh: Shell Script Conversion of VIEWDATA.BAT
T-1. A script that generates an ASCII table
T-2. Another ASCII table script
T-3. A third ASCII table script, using awk

martes, 2 de diciembre de 2014

asignando el contenido de un archivo a una variable

#!/bin/bash
 var=$(< phonebook.txt)
echo $var





http://stackoverflow.com/questions/4749905/how-can-i-read-a-file-and-redirect-it-to-a-variable

lunes, 1 de diciembre de 2014

for each as



#!/bin/bash
for i in {100,100,134,182}
do
   echo "Welcome $i times"
done




UNIX / Linux Shell Script For Monitoring System network with ping command


by on April 10, 2008 · 38 comments
  1. #!/bin/bash
  2. # Simple SHELL script for Linux and UNIX system monitoring with
  3. # ping command
  4. # -------------------------------------------------------------------------
  5. # Copyright (c) 2006 nixCraft project <http://www.cyberciti.biz/fb/>
  6. # This script is licensed under GNU GPL version 2.0 or above
  7. # -------------------------------------------------------------------------
  8. # This script is part of nixCraft shell script collection (NSSC)
  9. # Visit http://bash.cyberciti.biz/ for more information.
  10. # -------------------------------------------------------------------------
  11. # Setup email ID below
  12. # See URL for more info:
  13. # http://www.cyberciti.biz/tips/simple-linux-and-unix-system-monitoring-with-ping-command-and-scripts.html
  14. # -------------------------------------------------------------------------
  15.  
  16. # add ip / hostname separated by white space
  17. HOSTS="cyberciti.biz theos.in router"
  18.  
  19. # no ping request
  20. COUNT=1
  21.  
  22. # email report when
  23. SUBJECT="Ping failed"
  24. EMAILID="me@mydomain.com"
  25. for myHost in $HOSTS
  26. do
  27. count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  28. if [ $count -eq 0 ]; then
  29. # 100% failed
  30. echo "Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID
  31. fi
  32. done