Thursday 8 March 2012

Write a (Perl or Bash) script which takes one or more filenames as arguments. If any of the files does not exist in the current directory display prompt. For each file, remove all the comments lines in the file.


Write a (Perl or Bash) script which takes one or more filenames as arguments. If any of the files
does not exist in the current directory display prompt. For each file, remove all the comments
lines in the file.


#!/bin/bash
if [ $# -eq 0 ] #If no file is given as argument
then
echo 'Please provide name of file as argument' #Display message
else #If arguments are given
for file #For each argument
do
if [ ! -f $file ] #If file does not exist
then
echo "File $file does not exist" #Display message
else #If file exist
echo "Original contents of file $file are:" #Display message
cat $file #Display original file contents
sed -e '/^#[^!]/d' $file > temp #sed used to remove lines which start with #,
except those starting with #!. Result saved in file temp
echo "File contents after removing comments are:" #Display message
cat temp > $file #Output is written back on the file
cat $file #Display updated file
fi
done
fi

0 comments:

Post a Comment

 
Free Host | lasik surgery new york