Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.