need help with my implementation of cat in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help with my implementation of cat in bash
# 1  
Old 10-17-2006
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
# 2  
Old 10-18-2006
I think I am giving that up. I started a C implementation of the cat and it is much easier to implement.
# 3  
Old 10-18-2006
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.
# 4  
Old 10-18-2006
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...
# 5  
Old 10-18-2006
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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - Trying to cat a file that is a variable

Thank you in advance for looking at this, I've scoured the internet and can't find the answer I'm looking for!! - I am new at bash script so please bare with me!! I have a script where I've identified individual files within a folder, the filename is then stored as a variable ($filename):... (3 Replies)
Discussion started by: paperbackwriter
3 Replies

2. Shell Programming and Scripting

Bash script: "mkdir -p" doesn't work with var(cat x)

Hello, :) I've an issue with the creation of a directory, All work without it :mad: So, below, my scripts with the debug output : #!/bin/bash # PATHS HOME_BACKUP="/home/backup" HOME_SCRIPT="/home/scripts/test/backup_server" TARGET="/var/www" # DATE DATE_Ymd=$(date +%Y-%m-%d) #... (1 Reply)
Discussion started by: Arnaudh78
1 Replies

3. Shell Programming and Scripting

OSX, bash, cat with <<MARKER executing commands

I have a script that writes another script with cat >/usr/local/bin/myscript.sh <<EOF #!/bin/sh VAR=`run a command here` EOF Problem is, after this is run, I get: $ cat /usr/local/bin/myscript.sh #!/bin/sh VAR=result of command How do I stop that from happening with Macs... (2 Replies)
Discussion started by: jnojr
2 Replies

4. UNIX for Advanced & Expert Users

Ipsec implementation

How can i implement Ipsec between two machines in linux_ ubuntu? any link?? suggestion?? (0 Replies)
Discussion started by: elinaz
0 Replies

5. Shell Programming and Scripting

bash: cat multiple files together except first line?

Hopefully the title summarized what I need help with. I have multiple files that I would like to concatenate in bash. ie: cat file1 file2 file3 > bigfile except I do not want to include the first line from each file (). Any help? Thanks. (6 Replies)
Discussion started by: sanimfj
6 Replies

6. Shell Programming and Scripting

Bash: Zeilen aus Datei mit cat und grep in dynamisches Array schreiben

Hallo, ich habe eine Datei "Kino.ini" die z.B. wie folgt aussieht * KINOFILM A bla bla KINOFILM B blubb blubb KINOFILM C Ich möchte nun die Datei "Kino.ini" per cat und grep auslesen und testen ob der String KINOFILM nur mit einem '*' am Anfang vorkommt. In dieser Beispieldatei... (3 Replies)
Discussion started by: ABE2202
3 Replies

7. UNIX for Advanced & Expert Users

Implementation of ls - i command

It is possible for me to obtain the Inode of the path name using ls -i <pathname> command Can anyone tell me how its implemented... (3 Replies)
Discussion started by: ganapathy.psgit
3 Replies

8. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

9. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies
Login or Register to Ask a Question