LDP/LDP/guide/docbook/abs-guide/script-array.sh

30 lines
954 B
Bash

#!/bin/bash
# script-array.sh: Loads this script into an array.
# Inspired by an e-mail from Chris Martin (thanks!).
script_contents=( $(cat "$0") ) # Stores contents of this script ($0)
#+ in an array.
for element in $(seq 0 $((${#script_contents[@]} - 1)))
do # ${#script_contents[@]}
#+ gives number of elements in the array.
#
# Question:
# Why is seq 0 necessary?
# Try changing it to seq 1.
echo -n "${script_contents[$element]}"
# List each field of this script on a single line.
# echo -n "${script_contents[element]}" also works because of ${ ... }.
echo -n " -- " # Use " -- " as a field separator.
done
echo
exit 0
# Exercise:
# --------
# Modify this script so it lists itself
#+ in its original format,
#+ complete with whitespace, line breaks, etc.