LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   My first bash at shell scripting (sorry...) (http://www.linuxquestions.org/questions/linux-newbie-8/my-first-bash-at-shell-scripting-sorry-539411/)

Daws 03-21-2007 02:42 PM

My first bash at shell scripting (sorry...)
 
Hello everybody, today I thought I'd have a go at some shell scripting. This was my first go at a simple script:

Code:

#!/bin/bash

if [ -f "$1" ]; then
    shred -zvf "$1"
    rm -fv "$1"
elif [ -d "$1" ]; then
    cd "$1"
    find -type f -exec shred -zvf {} \;
    cd ..
    rm -rfv "$1"
else
    echo "File Does Not Exist"
fi

Well it worked, for one file or directory only. So after a bit of searching and a labourious skim through a scripting guide, I was under the impression that replacing $1 with $@ would make it work for multiple files.

Alas no. I get "Binary operator expected" errors. I think I may be interpreting these guidelines wrong but $@ was what I thought I wanted. Clearly not.

So my question is, what do I replace $1 with to make this script work with multiple files? (Assuming that's what I need to replace)

PS. I know this is a silly script and rather dangerous at that, while I appreciate alternatives methods, I want the script to work because it's a script, not because I desire to shred multiple files, if you know what I mean.

rjwilmsi 03-21-2007 02:48 PM

Use $* instead of $@ or do a loop like:
for arg do rm "$arg" done

osor 03-21-2007 02:50 PM

Quote:

Originally Posted by Daws
Hello everybody, today I thought I'd have a go at some shell scripting. This was my first go at a simple script:

Code:

#!/bin/bash

if [ -f "$1" ]; then
    shred -zvf "$1"
    rm -fv "$1"
elif [ -d "$1" ]; then
    cd "$1"
    find -type f -exec shred -zvf {} \;
    cd ..
    rm -rfv "$1"
else
    echo "File Does Not Exist"
fi

Well it worked, for one file or directory only. So after a bit of searching and a labourious skim through a scripting guide, I was under the impression that replacing $1 with $@ would make it work for multiple files.

Alas no. I get "Binary operator expected" errors. I think I may be interpreting these guidelines wrong but $@ was what I thought I wanted. Clearly not.

So my question is, what do I replace $1 with to make this script work with multiple files? (Assuming that's what I need to replace)

PS. I know this is a silly script and rather dangerous at that, while I appreciate alternatives methods, I want the script to work because it's a script, not because I desire to shred multiple files, if you know what I mean.

If you take look at “man test” you’ll find that test will take only one argument with the file testing flags (i.e., -f, -d, etc.). I’m afraid the only way to do what you’re asking is within a loop like so:
Code:

#!/bin/bash

for file in $@; do
    if [ -f "$file" ]; then
        shred -zvf "$file"
        rm -fv "$file"
    elif [ -d "$file" ]; then
        cd "$file"
        find -type f -exec shred -zvf {} \;
        cd ..
        rm -rfv "$file"
    else
        echo "File $file Does Not Exist"
    fi
done


Daws 03-21-2007 03:03 PM

Thank you very much. Nice speedy replies, (maybe record).

I'll probably see you later in my much anticipated thread "Help: I hosed my system with a silly script"

Cheers.

johngreenwood 03-21-2007 05:42 PM

Since we're doing first bash scripts, here is mine (I was going to do "Hello world" script, but it was too simple so I did that one in my head).

It's pretty basic, it asks for a device, then a mountpoint, if you want to specify a filesystem type, and if you want to mount read only, then mounts it and asks if you want to mount another. I wrote it with the help of the Advanced Bash Scripting Guide

I never use it, but I am keeping it because it was my first one.

Code:

#! /bin/bash
#
#
# Mount or unmount specified drives to specified mountpoints
#

while getopts "VUh" Option
do
        case $Option in
                h) echo " "
                  echo "Mounting interactively mounts or umounts filesystems entered by user"
                  echo "Usage: amountscript [-VUh]"
                  echo "Options: -h print this help and exit"
                  echo "        -V print version and exit"
                  echo "        -U run script in unmount mode "
                  echo " "
                  exit;;
                V) echo " "
                  echo "Mounting v0.3"
                  echo "Author J Greenwood"
                  echo "05/01/2007"
                  echo " "
                  exit;;
                U) if
                        [ "$UID" -ne "0" ]
                    then
                        echo "Must be root to run this script"
                        exit
                  fi
                  echo "Running Mounting in unmount mode"
                  YN=y
                  while [ "$YN" = "y" ]       
                  do
                        echo -n "Enter device identifier (eg. /dev/hda1): "
                        read -e DEVICE
                        umount $DEVICE
                        echo -n "Unmount another? (y/n): "
                        read -e YN
                        if
                                [ "$YN" = "y" ]
                        then
                                echo ""
                        else
                                exit
                        fi
                    done
                exit;;
        esac
done


# Checks to see if you are root, if not you can't run the script

if
        [ "$UID" -ne "0" ]
 then
        echo "This script must be run as root"
        exit
fi

YN=y

# Keep looping until you say no to mount another
# Asks for information about the drive you want to mount
# And mounts it according to your answers

while [ "$YN" = "y" ]
do
        echo -n "Enter device identifier (eg. /dev/hda1): "
        read -e DEVICE
        echo -n "Enter mountpoint (eg. /mnt/hda1): "
        read -e MOUNTPOINT
        echo -n "Would you like to mount $DEVICE read-only? (y/n): "
        read -e RONLY
        echo -n "Would you like to specify a filesystem type? (y/n): "
        read -e FILETYPEYN
       
       
        if
                [ "$FILETYPEYN" = "y" ]
        then
          if
                [ "$RONLY" = "y" ]
          then
                echo -n "Which filesystem does your device use? (eg. ext3): "
                read -e FILETYPE
                mount -t $FILETYPE $DEVICE $MOUNTPOINT -o ro
        elif       
                [ "$FILETYPEYN" = "y" ]
          then
                echo -n "which filesystem does your device use? (eg. ext3): "
                read -e FILETYPE
                mount -t $FILETYPE $DEVICE $MOUNTPOINT
          fi       
        fi
       
        if
                [ "$FILETYPEYN" = "n" ]
        then
          if
                [ "$RONLY" = "y" ]
          then
                mount $DEVICE $MOUNTPOINT -o ro
        elif
                [ "$FILETYPEYN" = "n" ]
          then
                mount $DEVICE $MOUNTPOINT
          fi
        fi

# Asks if you want to mount another device

        echo -n "Mount another? (y/n): "
        read -e YN

        if
                [ "$YN" = "y" ]
        then        echo ""
        else
                exit
        fi
done

exit


Daws 03-21-2007 06:20 PM

Nice script you got there, I too was thinking about ye olde "Hello World" but thought I'd rather do something semi-useful. My script was born out of a minor annoyance with the shred command. There is no option to recursively shred an entire directory tree.

EDIT: Woohoo 200!


All times are GMT -5. The time now is 05:54 AM.