You have... You want

You have a file
You want the file size in bytes
You have a command
You want to put the results in an array
You have a variable
You want to simulate the ord function
You have mutt
You want to send mail with an attachment"
You have a text file
You want a sorted list of all the words
You have a text file with a column of numbers
You want to sum the column
You have nothing
You want to create a variable and assign a value
You have a rc file structured like: color red, texture hard, size medium (three lines)
You want to parse the rc file
You have some code
You want to comment out some of it
You have a list (item1 item2 item3 ...)
You want to assign names to each list item
You have a list (item1 item2 item3 ...)
You want to print the list on a line with a separator
You have a string
You want to build an array with the words as elements
You have a string
You want to list the words
You have an array
You want to List the elements of the array
You have nothing
You want to Create a random integer
You have two dates
You want to calculate the number of elapsed seconds

Simulate the ord function

my_var='a'
printf '%d\n' "'$my_var"
97

If the leading character of a numeric argument is " or ' then its value is the numeric value of the immediately following character.


Put command results into an array

array=($(command))


Send mail with an attachment using mutt

cat "$message_body" | mutt -s $subject" -a "attachment_file" "$recipients"


Create a sorted list of words

tr -c '[:alnum:]' '[\n*]' |tr [:upper:] [:lower:]| sort -iu | grep -v '^[0-9]*$'


Sum a column

#!/bin/bash
# From the Advanced Bash Scripting Guide
# Another version of the "column totaler" script
#+ that adds up a specified column (of numbers) in the target file.
# This one uses indirect references.
ARGS=2
E_WRONGARGS=65
if [ $# -ne "$ARGS" ] # Check for proper no. of command line args.
then
echo "Usage: `basename $0` filename column-number"
exit $E_WRONGARGS
fi
filename=$1
column_number=$2
#===== Same as original script, up to this point =====#
# A multi-line awk script is invoked by awk ' ..... '
# Begin awk script.
# ------------------------------------------------
awk "
{ total += \$${column_number} # indirect reference
}
END {
print total
}
" "$filename"
# ------------------------------------------------
# End awk script.
# Indirect variable reference avoids the hassles
#+ of referencing a shell variable within the embedded awk script.
# Thanks, Stephane Chazelas.
exit 0

Create a variable and assign a value

var='color'
val='red'
eval $var=$val
echo $color --> red

Parse an rc file

#!/bin/bash -				# NOTE: #!/bin/sh - won't work here
filename='somerc'
while read var val; do
  [ -z $var ] && continue		# Ignore blank lines
  [ ${var:0:1} = "#" ] && continue	# Ignore comment lines
  eval $var=$val
  echo $var is ${!var}
done < <(cut -d' ' -f1-2 $filename)

Comment out some code

...
: <<'COMMENTBLOCK'
echo "this line won't echo"
*()^%%)(__+%		# This won't produce an error
$(rm -rf /tmp/foo)	# This won't execute
${var1?}		# This won't execute
COMMENTBLOCK
...

Assign variable names to list items

foo=(item1 item2 item3)
var1=${foo[0]}
var2=${foo[1]}
var3=${foo[2]}

Print a list on a line with a separator

for x in item1 item2 item3; do
  list=${list:+$list,}$x
done
echo $list
item1,item2,item3

Build an array from a string

foo="Now is the time"
bar=($foo)
echo ${bar[3]}	--> time

List the words in a string

foo="The quick brown fox"
for i in $(echo $foo)
do
   echo $i
done

List the elements of an array

foo=(aaa bbb ccc)
for i in $(seq 0 $(( ${#foo[@]}-1)))
do
	echo ${foo[$i]}
done

NOTE: There are two constructs here that may be unfamiliar:
The $( command ) for retreiving the results of a command and
the $(( arithmetic_expression )) for doing arithmetic.
An older form of $( ... ) for command substitution is back-ticks ` ... ` but it is depreciated.
An older form of $(( ... )) for doing arithmetic is $[ ... ] but it is depreciated


Create a random integer

echo $[ ( $RANDOM % 11 ) + 1 ]		# random integer between 1 and 11 inclusive

Calculate seconds between two dates

DATE1=1941/06/28			# Some date in the past at midnight
DATE2=$(date +%m/%d)			# Midnight this morning
TIME1=$(date --date="$DATE1" +%s)	# Epoch seconds for DATE1
TIME2=$(date --date="$DATE2" +%s)	# Epoch seconds for DATE2
ELTIME=$[ $TIME2 - $TIME1 ]
echo $ELTIME

Get the file size in bytes

/usr/bin/stat -c %s $file
1843


Send mail to the Webmaster

logo This site best viewed with a browser
Warning: This is a Debian centric site
Many thanks to Debra and Ian Murdock for making Debian possible
First created Apr 22, 2008 ~ Last revised April 18, 2010

Valid XHTML 1.0 Strict Valid CSS!