Sponsored Content
Top Forums Shell Programming and Scripting need help with my implementation of cat in bash Post 302093229 by jack1981 on Wednesday 18th of October 2006 06:28:18 AM
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

 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
asadmin-delete-domain(1AS)					   User Commands					asadmin-delete-domain(1AS)

NAME
asadmin-delete-domain, delete-domain - deletes the givendomain SYNOPSIS
delete-domain [--terse=false] [--echo=false] [--domaindir install_dir/domains] domain_name delete-domain deletes the specified domain. The domain must already exist and must be stopped. This command is supported in local mode only. OPTIONS
--domaindir directory where the domain is to be deleted. If specified, path must be accessible in the filesystem. If not speci- fied, the domain in the default install_dir/domains directory is deleted. --terse indicates that any output data must be very concise, typically avoiding human-friendly sentences and favoring well- formatted data for consumption by a script. Default is false. --echo setting to true will echo the command line statement on the standard output. Default is false. OPERANDS
domain_name name of the domain. Must be a unique name. Example 1: Using delete-domain asadmin> delete-domain sampleDomain deleted domain sampleDomain successfully Where: the sampleDomain domain is deleted. EXIT STATUS
0 command executed successfully 1 error in executing the command asadmin-create-domain(1AS), asadmin-start-domain(1AS), asadmin-stop-domain(1AS), asadmin-list-domains(1AS) J2EE 1.4 SDK March 2004 asadmin-delete-domain(1AS)
All times are GMT -4. The time now is 06:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy