The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 12:00 PM
isascii & isextendedascii implementation anjan_kumar_k High Level Programming 2 07-21-2006 10:48 AM
Solaris Timer Implementation ulisses0205 High Level Programming 2 04-06-2004 01:03 PM
Implementation of chat program hufs375 IP Networking 2 06-24-2002 11:25 AM
Shell Implementation clickonline1 UNIX for Dummies Questions & Answers 3 10-02-2001 04:52 PM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 10-17-2006
sanchopansa sanchopansa is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 38
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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0