[CLUG-tech] Scripting - creating a file

Andy Rabagliati andyr at wizzy.com
Fri Nov 22 15:23:55 SAST 2002


On Fri, 22 Nov 2002, Brian Shortridge wrote:

> Hi all
> 
> How would I go about creating a file from a bash script?

The time-honoured way is to use shar.

On redhat these are found in the sharutils RPM.

I use them extensively for %post scripts on kickstart
installs - one long shell script to tweak my install.

The result looks like this, though for my kickstart scripts I use
the following command line, which has less dependencies :-

shar --text-files --no-character-count --no-md5-digest \
	--no-timestamp --force-prefix --no-check-existing --quiet-unshar --no-i18n

Cheers,    Andy!

% shar /etc/exports

#!/bin/sh
# This is a shell archive (produced by GNU sharutils 4.2.1).
# To extract the files from this archive, save it to some FILE, remove
# everything before the `!/bin/sh' line above, then type `sh FILE'.
#
# Made on 2002-11-22 15:19 SAST by <andyr at armada>.
# Source directory was `/home/andyr'.
#
# Existing files will *not* be overwritten unless `-c' is specified.
#
# This shar contains:
# length mode       name
# ------ ---------- ------------------------------------------
#    610 -rw-r--r-- /etc/exports
#
save_IFS="${IFS}"
IFS="${IFS}:"
gettext_dir=FAILED
locale_dir=FAILED
first_param="$1"
for dir in $PATH
do
  if test "$gettext_dir" = FAILED && test -f $dir/gettext \
     && ($dir/gettext --version >/dev/null 2>&1)
  then
    set `$dir/gettext --version 2>&1`
    if test "$3" = GNU
    then
      gettext_dir=$dir
    fi
  fi
  if test "$locale_dir" = FAILED && test -f $dir/shar \
     && ($dir/shar --print-text-domain-dir >/dev/null 2>&1)
  then
    locale_dir=`$dir/shar --print-text-domain-dir`
  fi
done
IFS="$save_IFS"
if test "$locale_dir" = FAILED || test "$gettext_dir" = FAILED
then
  echo=echo
else
  TEXTDOMAINDIR=$locale_dir
  export TEXTDOMAINDIR
  TEXTDOMAIN=sharutils
  export TEXTDOMAIN
  echo="$gettext_dir/gettext -s"
fi
if touch -am -t 200112312359.59 $$.touch >/dev/null 2>&1 && test ! -f 200112312359.59 -a -f $$.touch; then
  shar_touch='touch -am -t $1$2$3$4$5$6.$7 "$8"'
elif touch -am 123123592001.59 $$.touch >/dev/null 2>&1 && test ! -f 123123592001.59 -a ! -f 123123592001.5 -a -f $$.touch; then
  shar_touch='touch -am $3$4$5$6$1$2.$7 "$8"'
elif touch -am 1231235901 $$.touch >/dev/null 2>&1 && test ! -f 1231235901 -a -f $$.touch; then
  shar_touch='touch -am $3$4$5$6$2 "$8"'
else
  shar_touch=:
  echo
  $echo 'WARNING: not restoring timestamps.  Consider getting and'
  $echo "installing GNU \`touch', distributed in GNU File Utilities..."
  echo
fi
rm -f 200112312359.59 123123592001.59 123123592001.5 1231235901 $$.touch
#
if mkdir _sh09352; then
  $echo 'x -' 'creating lock directory'
else
  $echo 'failed to create lock directory'
  exit 1
fi
# ============= /etc/exports ==============
if test ! -d '/etc'; then
  $echo 'x -' 'creating directory' '/etc'
  mkdir '/etc'
fi
if test -f '/etc/exports' && test "$first_param" != -c; then
  $echo 'x -' SKIPPING '/etc/exports' '(file already exists)'
else
shar: Saving /etc/exports (text)
  $echo 'x -' extracting '/etc/exports' '(text)'
  sed 's/^X//' << 'SHAR_EOF' > '/etc/exports' &&
#
/export           172.16.0.0/16(ro,no_subtree_check)
## LTS-begin ##
X
#
# The lines between the 'LTS-begin' and the 'LTS-end' were added
# on: Sat Oct 19 17:12:15 SAST 2002 by the ltsp installation script.
# For more information, visit the ltsp homepage
# at http://www.ltsp.org
#
X
/opt/ltsp/i386                  172.20.1.0/255.255.255.0(ro,no_root_squash)
/var/opt/ltsp/swapfiles         172.20.1.0/255.255.255.0(rw,no_root_squash)
X
#
# The following entries need to be uncommented if you want
# Local App support in ltsp
#
/home                  172.20.1.0/255.255.255.0(rw,no_root_squash)
X
## LTS-end ##
SHAR_EOF
  (set 20 02 10 19 17 16 09 '/etc/exports'; eval "$shar_touch") &&
  chmod 0644 '/etc/exports' ||
  $echo 'restore of' '/etc/exports' 'failed'
  if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
  && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
    md5sum -c << SHAR_EOF >/dev/null 2>&1 \
    || $echo '/etc/exports:' 'MD5 check failed'
582df3c4f5a83aabd8fd9add5caf0cb5  /etc/exports
SHAR_EOF
  else
    shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < '/etc/exports'`"
    test 610 -eq "$shar_count" ||
    $echo '/etc/exports:' 'original size' '610,' 'current size' "$shar_count!"
  fi
fi
rm -fr _sh09352
exit 0




More information about the Clug-tech mailing list