Code Academy

Code Academy

Code Academy | Learn to Code

Great intractable site for learning how to code, once you get into it it’s pretty good, and you can enjoy some of the tasks in learning as you go along, some tough ones along the way that gets you to think hard and remember everything you’ve learned from the beginning.

Advertisements

FoDel {Shell}

A while back I created a simple Bash Shell script for fixing an annoying occurrence that sometimes happens on a Mac, sometimes it would refuse to delete files properly from the Trash folder, and so you’d have to force delete it by hand in Terminal, so I decided to come up with the idea for creating an autonomous program to take care of the process for you so you don’t have to type everything out like so;

(ignore the appending $ sign, it’s just an example of the current shell you are in.)

$ rm ~/.Trash/
$ mkdir ~/.Trash/
$ killall Finder

It’s not really much to type out, but the script I created executes it in a matter of seconds with one command, by simply typing FoDel into the command line. I first wanted to create a sequence of menu commands, where you select your options from the menu to execute the script, but that wasn’t quite working out so well, still need to actually work on that. So I ended up scrapping that idea for now and replaced it with countdown timers instead. I put a short interval in between each command so it’s not trying to cram everything in all at once, because that would like trying to cram in 300 biscuits into your mouth all at once.

I called this script FoDEL for a very good reason, it’s short for Force Delete.

So…… anyway here’s the code.

#!/bin/sh

echo // @@ ======// === FoDEL script v0.2b ==== \\====== @@
echo // @@ ======// ====== by ShadWolf ======== \\====== @@
echo // @@ ======// ========= ©2011 =========== \\====== @@
echo // @@ ======// ++ Force Delete Trash ++ \\======= @@
echo // ————————————————————
echo // Force delete files from ~/.Trash/ folder, use with caution!
echo // improper use of the rm command could mess up your system.
echo // !! This script is designed to delete the ~/.Trash/ folder !!
echo // !! then Re-Create it, then kill Finder to relaunch it. !!
echo // ————————————————————
echo

echo “I will begin to run FoDEL, if at any time you want me to stop press ^C. Starting in 6 Seconds…”

echo

wait ## First initial countdown with 6 seconds, incase you wish to cancel it before it runs.
function countdown
{
local OLD_IFS=“${IFS}”
IFS=“:”
local ARR=( $1 )
local SECONDS=$(( (ARR[0] * 60 * 60) + (ARR[1] * 60) + ARR[2] ))
local START=$(date +%s)
local END=$((START + SECONDS))
local CUR=$START

while [[ $CUR -lt $END ]]
do
CUR=$(date +%s)
LEFT=$((END-CUR))

printf “\r%02d:%02d:%02d” \
$((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))

sleep 1
done
IFS=“${OLD_IFS}”
echo ” “
}

countdown “00:00:6” ## First step, 6 Second Countdown

echo

## This will delete the .Trash folder

`rm ~/.Trash/`
echo “\”~/.Trash/\” folder Deleted!”

echo

## Small time interval between each command ‘3 seconds’.

wait
function countdown
{
local OLD_IFS=“${IFS}”
IFS=“:”
local ARR=( $1 )
local SECONDS=$(( (ARR[0] * 60 * 60) + (ARR[1] * 60) + ARR[2] ))
local START=$(date +%s)
local END=$((START + SECONDS))
local CUR=$START

while [[ $CUR -lt $END ]]
do
CUR=$(date +%s)
LEFT=$((END-CUR))

printf “\r%02d:%02d:%02d” \
$((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))

sleep 1
done
IFS=“${OLD_IFS}”
echo ” “
}

countdown “00:00:03”

echo

echo “Creating new ~/.Trash/ folder…”

echo

wait
function countdown
{
local OLD_IFS=“${IFS}”
IFS=“:”
local ARR=( $1 )
local SECONDS=$(( (ARR[0] * 60 * 60) + (ARR[1] * 60) + ARR[2] ))
local START=$(date +%s)
local END=$((START + SECONDS))
local CUR=$START

while [[ $CUR -lt $END ]]
do
CUR=$(date +%s)
LEFT=$((END-CUR))

printf “\r%02d:%02d:%02d” \
$((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))

sleep 1
done
IFS=“${OLD_IFS}”
echo ” “
}

countdown “00:00:03”

echo

## This will make the .Trash folder (recreate)

`mkdir ~/.Trash`
echo “\”~/.Trash/\” folder Created!”

echo

wait
function countdown
{
local OLD_IFS=“${IFS}”
IFS=“:”
local ARR=( $1 )
local SECONDS=$(( (ARR[0] * 60 * 60) + (ARR[1] * 60) + ARR[2] ))
local START=$(date +%s)
local END=$((START + SECONDS))
local CUR=$START

while [[ $CUR -lt $END ]]
do
CUR=$(date +%s)
LEFT=$((END-CUR))

printf “\r%02d:%02d:%02d” \
$((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))

sleep 1
done
IFS=“${OLD_IFS}”
echo ” ”
}

countdown “00:00:03”

echo

## This will kill Finder which may take upto a minute or less

## If using this script on a different UNIX machine you probably won’t need this
## Just comment out the command line from what you see below.

`killall -v Finder`
echo “Killing \”Finder\” please wait…!”

echo

## Lastly this is not idiot-proof so if you somehow fuck this up while using it, I won’t be held responsible for it. You brake it. You deal with it!
## Oh and you better not cancel the task by the time it reaches the 1st, because you’ll have to execute these commands
## mkdir ~/.Trash/ && killall Finder

echo “I’m done here…. exiting.”

To run the script you would just have to do;

$ ./fodel.sh

If you wish to make it much easier to use, just stick it into your .Profile file, should be either called .profile, or .bash_profile  or something similar. Here’s an example of what I did for mine.

alias fodel=’~/scripts/FoDel-0.2b.sh’

so I can just run the command right off the bat without having to type ./fodel-0.2b.sh. It doesn’t really matter if the file name has capitols, it’ll still run either way when you call it from the command line. It may not be much of a script, but atleast it gets the job done that it’s suppose to do. Hopefully when I can be bothered I’ll probably revise this code a little more and maybe improve it to be better. I was gonna try implement the Secure Remove command, but it’s a little tricky and doesn’t like it’s command options bundled together. I’ll have to look into that more, and maybe if I do improve this code I’ll try get a Menu Option execution inserted, instead of having those countdown timers in place. Possibly might be revised and changed in v0.3b.

I would gladly like it for anyone to suggest how to improve this. You are free to edit and change it up to make it a better script. :3