LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-21-2007, 02:42 PM   #1
Daws
Member
 
Registered: May 2006
Location: UK
Distribution: Debian
Posts: 447

Rep: Reputation: 39
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.
 
Old 03-21-2007, 02:48 PM   #2
rjwilmsi
Member
 
Registered: Mar 2005
Location: UK
Distribution: opensuse 12.2 x86_64
Posts: 563

Rep: Reputation: 38
Use $* instead of $@ or do a loop like:
for arg do rm "$arg" done
 
Old 03-21-2007, 02:50 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 77
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
 
Old 03-21-2007, 03:03 PM   #4
Daws
Member
 
Registered: May 2006
Location: UK
Distribution: Debian
Posts: 447

Original Poster
Rep: Reputation: 39
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.
 
Old 03-21-2007, 05:42 PM   #5
johngreenwood
Member
 
Registered: Nov 2006
Location: Lancashire, United Kingdom
Distribution: Slackware 13
Posts: 243

Rep: Reputation: 31
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
 
Old 03-21-2007, 06:20 PM   #6
Daws
Member
 
Registered: May 2006
Location: UK
Distribution: Debian
Posts: 447

Original Poster
Rep: Reputation: 39
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Bash shell scripting HLM01 Linux - Newbie 7 01-31-2008 04:23 PM
BASH Shell Scripting tekmann33 Linux - General 4 02-08-2007 05:03 PM
Bash shell scripting Sco Linux - Newbie 1 11-09-2004 11:58 AM
Help with I/O on bash shell scripting Dave6383 Programming 1 06-03-2004 05:24 PM
BASH Shell scripting help ewarmour Linux - General 8 05-25-2002 07:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration