Skip to content

Commit

Permalink
updating usefull shell scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
rkramesh authored Aug 20, 2016
1 parent d3934bd commit c5a26ae
Show file tree
Hide file tree
Showing 33 changed files with 733 additions and 0 deletions.
94 changes: 94 additions & 0 deletions shell/VariableAssignmentAndSubstitution.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/bin/bash

# Variables: assignment and substitution

a=375
hello=$a

#-------------------------------------------------------------------------
# No space permitted on either side of = sign when initializing variables.
# What happens if there is a space?

# If "VARIABLE =value",
# ^
#+ script tries to run "VARIABLE" command with one argument, "=value".

# If "VARIABLE= value",
# ^
#+ script tries to run "value" command with
#+ the environmental variable "VARIABLE" set to "".
#-------------------------------------------------------------------------


echo hello # Not a variable reference, just the string "hello".

echo $hello
echo ${hello} # Identical to above.

echo "$hello"
echo "${hello}"

echo

hello="A B C D"
echo $hello # A B C D
echo "$hello" # A B C D
# As you see, echo $hello and echo "$hello" give different results.
# ^ ^
# Quoting a variable preserves whitespace.

echo

echo '$hello' # $hello
# ^ ^
# Variable referencing disabled by single quotes,
#+ which causes the "$" to be interpreted literally.

# Notice the effect of different types of quoting.


hello= # Setting it to a null value.
echo "\$hello (null value) = $hello"
# Note that setting a variable to a null value is not the same as
#+ unsetting it, although the end result is the same (see below).

# --------------------------------------------------------------

# It is permissible to set multiple variables on the same line,
#+ if separated by white space.
# Caution, this may reduce legibility, and may not be portable.

var1=21 var2=22 var3=$V3
echo
echo "var1=$var1 var2=$var2 var3=$var3"

# May cause problems with older versions of "sh".

# --------------------------------------------------------------

echo; echo

numbers="one two three"
# ^ ^
other_numbers="1 2 3"
# ^ ^
# If there is whitespace embedded within a variable,
#+ then quotes are necessary.
echo "numbers = $numbers"
echo "other_numbers = $other_numbers" # other_numbers = 1 2 3
echo

echo "uninitialized_variable = $uninitialized_variable"
# Uninitialized variable has null value (no value at all).
uninitialized_variable= # Declaring, but not initializing it --
#+ same as setting it to a null value, as above.
echo "uninitialized_variable = $uninitialized_variable"
# It still has a null value.

uninitialized_variable=23 # Set it.
unset uninitialized_variable # Unset it.
echo "uninitialized_variable = $uninitialized_variable"
# It still has a null value.
echo

exit 0
13 changes: 13 additions & 0 deletions shell/characterEncoding.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
# rot13.sh: Classic rot13 algorithm,
# encryption that might fool a 3-year old.

# Usage: ./rot13.sh filename
# or ./rot13.sh <filename
# or ./rot13.sh and supply keyboard input (stdin)

cat "$@" | tr 'a-zA-Z' 'n-za-mN-ZA-M' # "a" goes to "n", "b" to "o", etc.
# The 'cat "$@"' construction
#+ permits getting input either from stdin or from files.

exit 0
4 changes: 4 additions & 0 deletions shell/conditionalRexExmatch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if echo "$VAR" | grep -q txt # if [[ $VAR = *txt* ]]
then
echo "$VAR contains the substring sequence \"txt\""
fi
30 changes: 30 additions & 0 deletions shell/copyFiles.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash
# copydir.sh

# Copy (verbose) all files in current directory ($PWD)
#+ to directory specified on command line.

E_NOARGS=65

if [ -z "$1" ] # Exit if no argument given.
then
echo "Usage: `basename $0` directory-to-copy-to"
exit $E_NOARGS
fi

ls . | xargs -i -t cp ./{} $1
# ^^ ^^ ^^
# -t is "verbose" (output command line to stderr) option.
# -i is "replace strings" option.
# {} is a placeholder for output text.
# This is similar to the use of a curly bracket pair in "find."
#
# List the files in current directory (ls .),
#+ pass the output of "ls" as arguments to "xargs" (-i -t options),
#+ then copy (cp) these arguments ({}) to new directory ($1).
#
# The net result is the exact equivalent of
#+ cp * $1
#+ unless any of the filenames has embedded "whitespace" characters.

exit 0
46 changes: 46 additions & 0 deletions shell/deleteFileWithInodeNumber.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash
# idelete.sh: Deleting a file by its inode number.

# This is useful when a filename starts with an illegal character,
#+ such as ? or -.

ARGCOUNT=1 # Filename arg must be passed to script.
E_WRONGARGS=70
E_FILE_NOT_EXIST=71
E_CHANGED_MIND=72

if [ $# -ne "$ARGCOUNT" ]
then
echo "Usage: `basename $0` filename"
exit $E_WRONGARGS
fi

if [ ! -e "$1" ]
then
echo "File \""$1"\" does not exist."
exit $E_FILE_NOT_EXIST
fi

inum=`ls -i | grep "$1" | awk '{print $1}'`
# inum = inode (index node) number of file
# -----------------------------------------------------------------------
# Every file has an inode, a record that holds its physical address info.
# -----------------------------------------------------------------------

echo; echo -n "Are you absolutely sure you want to delete \"$1\" (y/n)? "
# The '-v' option to 'rm' also asks this.
read answer
case "$answer" in
[nN]) echo "Changed your mind, huh?"
exit $E_CHANGED_MIND
;;
*) echo "Deleting file \"$1\".";;
esac

find . -inum $inum -exec rm {} \;
# ^^
# Curly brackets are placeholder
#+ for text output by "find."
echo "File "\"$1"\" deleted!"

exit 0
25 changes: 25 additions & 0 deletions shell/dos2UnixConversion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
# Du.sh: DOS to UNIX text file converter.

E_WRONGARGS=65

if [ -z "$1" ]
then
echo "Usage: `basename $0` filename-to-convert"
exit $E_WRONGARGS
fi

NEWFILENAME=$1.unx

CR='\015' # Carriage return.
# 015 is octal ASCII code for CR.
# Lines in a DOS text file end in CR-LF.
# Lines in a UNIX text file end in LF only.

tr -d $CR < $1 > $NEWFILENAME
# Delete CR's and write to new file.

echo "Original DOS text file is \"$1\"."
echo "Converted UNIX text file is \"$NEWFILENAME\"."

exit 0
6 changes: 6 additions & 0 deletions shell/exprIndex.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
stringZ=abcABC123ABCabc
echo `expr index "$stringZ" C12` # 6
# C position.

echo `expr index "$stringZ" 1c` # 3
# 'c' (in #3 position) matches before '1'.
6 changes: 6 additions & 0 deletions shell/exprLengthOfBeginingStr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

stringZ=abcABC123ABCabc
# |------|

echo `expr match "$stringZ" 'abc[A-Z]*.2'` # 8
echo `expr "$stringZ" : 'abc[A-Z]*.2'` # 8
21 changes: 21 additions & 0 deletions shell/exprMathOperation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
expr 3 + 5
#returns 8

expr 5 % 3
#returns 2

expr 1 / 0
#returns the error message, expr: division by zero

#Illegal arithmetic operations not allowed.

expr 5 \* 3
#returns 15

#The multiplication operator must be escaped when used in an arithmetic expression with expr.

y=`expr $y + 1`
#Increment a variable, with the same effect as let y=y+1 and y=$(($y+1)). This is an example of arithmetic expansion.

z=`expr substr $string $position $length`
#Extract substring of $length characters, starting at $position.
5 changes: 5 additions & 0 deletions shell/exprStringLength.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
stringZ=abcABC123ABCabc

echo ${#stringZ} # 15
echo `expr length $stringZ` # 15
echo `expr "$stringZ" : '.*'` # 15
6 changes: 6 additions & 0 deletions shell/exprSubstrExtractionIndexBased.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
stringZ=abcABC123ABCabc
# 123456789......
# 1-based indexing.

echo `expr substr $stringZ 1 2` # ab
echo `expr substr $stringZ 4 3` # ABC
105 changes: 105 additions & 0 deletions shell/exprWithbuiltinsWithMatch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@

#!/bin/bash

# Demonstrating some of the uses of 'expr'
# =======================================

echo

# Arithmetic Operators
# ---------- ---------

echo "Arithmetic Operators"
echo
a=`expr 5 + 3`
echo "5 + 3 = $a"

a=`expr $a + 1`
echo
echo "a + 1 = $a"
echo "(incrementing a variable)"

a=`expr 5 % 3`
# modulo
echo
echo "5 mod 3 = $a"

echo
echo

# Logical Operators
# ------- ---------

# Returns 1 if true, 0 if false,
#+ opposite of normal Bash convention.

echo "Logical Operators"
echo

x=24
y=25
b=`expr $x = $y` # Test equality.
echo "b = $b" # 0 ( $x -ne $y )
echo

a=3
b=`expr $a \> 10`
echo 'b=`expr $a \> 10`, therefore...'
echo "If a > 10, b = 0 (false)"
echo "b = $b" # 0 ( 3 ! -gt 10 )
echo

b=`expr $a \< 10`
echo "If a < 10, b = 1 (true)"
echo "b = $b" # 1 ( 3 -lt 10 )
echo
# Note escaping of operators.

b=`expr $a \<= 3`
echo "If a <= 3, b = 1 (true)"
echo "b = $b" # 1 ( 3 -le 3 )
# There is also a "\>=" operator (greater than or equal to).


echo
echo



# String Operators
# ------ ---------

echo "String Operators"
echo

a=1234zipper43231
echo "The string being operated upon is \"$a\"."

# length: length of string
b=`expr length $a`
echo "Length of \"$a\" is $b."

# index: position of first character in substring
# that matches a character in string
b=`expr index $a 23`
echo "Numerical position of first \"2\" in \"$a\" is \"$b\"."

# substr: extract substring, starting position & length specified
b=`expr substr $a 2 6`
echo "Substring of \"$a\", starting at position 2,\
and 6 chars long is \"$b\"."


# The default behavior of the 'match' operations is to
#+ search for the specified match at the ***beginning*** of the string.
#
# uses Regular Expressions
b=`expr match "$a" '[0-9]*'` # Numerical count.
echo Number of digits at the beginning of \"$a\" is $b.
b=`expr match "$a" '\([0-9]*\)'` # Note that escaped parentheses
# == == + trigger substring match.
echo "The digits at the beginning of \"$a\" are \"$b\"."

echo

exit 0
Loading

0 comments on commit c5a26ae

Please sign in to comment.