Skip to main content

Posts

Showing posts from June, 2010

File Handling using Shell Scripts

File handling using Shell Scripts
to prepend a line to a file
to develop a temporary file name generator
to compare two files
Requirements:
    Suse Linux, BASH Scripting
Program
(a) Prepending a line to a file

#!/bin/bash
# prepend.sh: Add text at beginning of file.

E_NOSUCHFILE=65

read -p "File: " file # -p arg to 'read' displays prompt.
if [ ! -e "$file" ]
    then # Bail out if no such file.
    echo "File $file not found."
    exit $E_NOSUCHFILE
fi

read -p "Title: " title
cat - $file <<<$title > $file.new
echo "Modified file is $file.new"
exit 0

(b) to develop a temporary file name generator
#!/bin/bash
# tempfile-name.sh: temp filename generator
BASE_STR=`mcookie` # 32-character magic cookie.
POS=11 # Arbitrary position in magic cookie string.
LEN=5 # Get $LEN consecutive characters.
prefix=temp # This is, after all, a "temp" file.
# For more "uniqueness," generate the…