![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 looking forward to your advice. Kind regards, Valentin |
| Forum Sponsor | ||
|
|
|
|||
|
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
|
|||
| Google The UNIX and Linux Forums |