The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
VietPad 2.0 (.NET implementation branch) iBot Software Releases - RSS News 0 04-04-2008 09:00 AM
isascii & isextendedascii implementation anjan_kumar_k High Level Programming 2 07-21-2006 07:48 AM
Solaris Timer Implementation ulisses0205 High Level Programming 2 04-06-2004 10:03 AM
Implementation of chat program hufs375 IP Networking 2 06-24-2002 08:25 AM
Shell Implementation clickonline1 UNIX for Dummies Questions & Answers 3 10-02-2001 01:52 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-17-2006
Registered User
 

Join Date: Oct 2006
Posts: 38
Stumble this Post!
need help with my implementation of cat in bash

Hello,

A few days ago I created my amateur version of the cat function in bash. Here is what I've done:

Code:
#!/bin/bash
#This is mycat. Similar to cat.
#For detailed information use path/to/mycat.sh -h option

arguments=$#

if [[ "$arguments" -eq 0 ]] #in case of standard input
then
while [[ "$input" != "EndInput" ]]
do
read input
echo $input
done
else
case $1 in #take action according to specified option
  	"-n")
		nl < $2 #number lines
  		;;

  	"-E")
   	sed 's/$/$/' < $2 #redirect file and add $ at the end of each line
  		;;

        "-A"
        sed 's/   /^I/g' < $2 > newfile.list
        sed l < newfile.list
                ;;

        "-T"
        sed 's/   /^I/g'
                ;;

  	"-h")
		echo
		echo "mycat. Concatenates and/or displays files from standard input to standard output for free."
		echo
		echo "SYNOPSIS"
		echo "   /path/to/mycat.sh [OPTION] [FILE]"
		echo
		echo "OPTIONS"
		echo "   -n                         number all output lines"
		echo "   -E                         displays $ at the end of each line"
		echo "   -h                         to display help"
		echo "   -T                         display tabs as ^I"
                echo "   -A                         show -all"
                echo
		echo "EndInput to end"
		;;

  *) #case with more than one file.
  while [ "$arguments" -ne 0 ]
  do
  line_number=1
  last_line=$(tail -1 < $1) 
  while [[ "$last_line" != "$line_to_display" ]] #go through the whole file
  do
  last_line=$(tail -1 < $1)
  line_to_display=$(head -$line_number < $1 | tail -1) #display current line
  echo "$line_to_display"
  ((line_number++))
  done
  shift #go to next file and decrease $# with 1
  arguments=$# 
  done;;
 esac
fi
exit 0
I am turning to you for help because I want to enhance it a bit and make it look more like the real cat utility. The first thing I want to do is replace the EndInput with CTRL+D as the terminating character. And the second one is to make it possible to use more than one option and more than one file, like cat -n -e file1.list file2.list. The latter could be done with the getopt function in C but I would appreciate if anyone could tell how to accomplish it in bash.

I am looking forward to your advice.

Kind regards,
Valentin
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-18-2006
Registered User
 

Join Date: Oct 2006
Posts: 38
Stumble this Post!
I think I am giving that up. I started a C implementation of the cat and it is much easier to implement.
Reply With Quote
  #3 (permalink)  
Old 10-18-2006
Registered User
 

Join Date: Sep 2006
Posts: 1,540
Stumble this Post!
Quote:
Originally Posted by sanchopansa
I think I am giving that up. I started a C implementation of the cat and it is much easier to implement.
why do you need to "reinvent the wheel"? cat is already there for you to use.
Reply With Quote
  #4 (permalink)  
Old 10-18-2006
Registered User
 

Join Date: Oct 2006
Posts: 38
Stumble this Post!
No particular reason. I just thought it would be a useful thing for me to do because I'm a beginner and I can learn something interesting like this.
Whatever...
Reply With Quote
  #5 (permalink)  
Old 10-18-2006
Registered User
 

Join Date: Jul 2006
Posts: 42
Stumble this Post!
Might be of some use

Hi,

I created a program that emualted the rm command in UNIX but stored the data in a seperate directory after deletion, a bit like the recycle bin in windows. This isn't the finished version but isn't to far off and it uses getopts which might be of some use.

Code:
#/bin/bash
# A program to emulate the "rm" command in UNIX.


# INITIALIZE VARIABLES
NO_ARGS=0
FLAG_R=""
FLAG_F_I=""
FLAG_V=""
TRASH=$HOME/deleted

# FUNCTIONS

function errors() {
if [ "$#" -eq  "$NO_ARGS" ] ; then
      echo "rm: too few arguments"
      echo "Try \`rm --help' for more information."
      exit 0
elif [[  ! -f "$1"  && ! -d "$1"   ]] ; then
      echo "rm: cannot remove $ARG : no such file or directory"
      exit 0
elif [[ -d $ARG  &&  "$FLAG_R" = ""  ]] ; then
      echo "rm: \`$ARG' is a directory"
      exit 0
else
     checkExisting $1
fi
}


function checkExisting (){
if [ -d $TRASH/$1 ]; then

     find "$TRASH/$1" -type f -exec shred -fu {} \; 2>/dev/null
     find "$TRASH/$1" -type d -exec rmdir -p  {} \; 2>/dev/null
     directoryDelete $1
else
     directoryDelete $1

fi

}

function directoryDelete () {
if [[ -d "$1"  &&  "$FLAG_R" == "R" && "$FLAG_F_I" == "i" ]]; then
echo -n "rm: descend into directory \`$1'?"
     read A
     if [[ "$A" = [Yy] ]] ; then
echo  "removing all entries of directory \`$1'?"

for FILE in $1/*
do
       if [ -d $FILE ] ; then
       directoryDelete $FILE
   else
       writePro $FILE
fi
done
     echo -n "rm: remove directory \`$1'?"
     read A
     if [[ "$A" = [Yy] ]] ; then
     mv  $1 $TRASH 2>/dev/null
echo "rm: removing directory itself: \`$1'"
fi
fi
else
    writePro $1
fi
}

function writePro () {
if ! [ -w  "$1" ] ; then
echo -n "rm: remove write-protected file \`$*'?"
     read A
     if [[ "$A" = [Yy] ]] ; then
     delete $1
     fi
else
     delete $1
fi

}

function delete() {
if [ "$FLAG_F_I" = "i" ] &&  [ -w  "$1" ] ; then
   interactive $1
elif [ "$FLAG_F_I" = "f"  ] ; then
   force $1
elif [ "$FLAG_R" = "R"  ] ; then
   remove $1
else
   remove $1
fi

}

function force () {

mv -f $1 $TRASH 2>/dev/null
verbose $1
}


function remove () {
mv  $1 $TRASH 2>/dev/null
verbose $1
}

function interactive () {
echo -n "rm: remove $1 ?"
     read A
     if [[ "$A" = [Yy] ]] ; then
     remove $1
     else
     exit 0
fi
}
function verbose () {
if [ "$FLAG_V" = "v" ] ; then
echo "removing \`$1'"
fi
}


# PARSE OPTIONS WITH GETOPTS

while getopts :rRfvi o
do    case $o in
           r|R) FLAG_R=R
             ;;
             f) FLAG_F_I=f
             ;;
             v) FLAG_V=v
             ;;
             i) FLAG_F_I=i
             ;;
             *) echo "rm: invalid option -$1"
                echo "try \`rm --help' for more information"
                exit 0
      esac
done
shift `expr $OPTIND - 1`

# START OF FLOW

if ! [ -d "$HOME/deleted" ] ; then
     mkdir $HOME/delete
elif  [ $# -eq $NO_ARGS ] ; then
errors
else
   for ARG in $@
   do
   errors $ARG
   done
fi
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:02 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0