So I've been farting around after a long break from doing much of anything on Linux. I was prompted by my new hard drive arriving.
I spent some time migrating data from five separate hard drives and a stack of DVDs. One of my next goals was to get backups working, so that I can back up from my SSD to this new drive.
I have an old backup script, but I decided that I could spend this afternoon screwing around with it to optimize it a little.
Using the wonderful mcedit
which comes with Midnight Commander, I hacked away. Linux being what it is, there was a tangled horror of stuff I needed to try to remember, look up through my notes, get answers to and/or figure out.
- 1 Procedure/function
- 2 Pass parameters to a procedure/function
- 3 Pass an array to a function, iterate through an array
- 4 A case
- 5 if/not
- 6 Error Trapping (^c/BREAK)
- 7 Learning a partition's filesystem
- 8 Is a partition mounted?
- 9 How do I get fsck to just skip on already-mounted partitions? (Can't do it)
I've done enough scripting in the past that I've got notes on most of the common stuff. But because I wanted to "optimize" my backup code by replacing duplication with re-usable procedures, and I wanted to make it generally smarter, I really did need to do some research to get a bunch of other stuff working.
Keep in mind that all of this is quick-and-dirty. Oh, and WordPress doesn't preserve space-tabbing or give coloured markup. Meh.
Procedure/function ∞
example() { echo hi! } example
Pass parameters to a procedure/function ∞
example() { echo some text $1 } example "my text"
Pass an array to a function, iterate through an array ∞
example() { for element in ${@[@]}; do echo some text $element done } example "my text" two 3
A case ∞
var=2 case $var in 1) echo "one" 2) echo "two" ;; *) echo "everything else" ;; esac
if/not ∞
var=2 if [ $var -ne 2]; then echo "nope!" fi
Error Trapping (^c/BREAK) ∞
How do I trap error codes? (INT
, aka ^c
/BREAK
)
This can get much much more complex, but a simple example is:
trap breaktrap INT breaktrap() { echo "do this when ^c is used" }
I also leveraged the procedure and used it like one.. letting me do some pretty nifty stuff because I already had some other error-detection code. Yes, I harden my code.
Learning a partition's filesystem ∞
Ever notice how [s|c|]fdisk has partition types? Well here's how you can get at them!
\sfdisk --part-type /dev/sda 1
- 7 = NTFS
- 82 = Swap
-
83 = Linux (ext2/ext3/etc)
Is a partition mounted? ∞
2019-10-05 - I'm not really sure what I was trying to accomplish here..
How do I learn if a partition is mounted? /proc/mounts
? nope! That mysteriously doesn't show your present root filesystem.
Since fsck is retarded, I had to pipe other programs and wrap all that shit around fsck to build the fucking feature for it.
for line in `df | cut -d' ' -f1`; do if [ "x$1" -ne "x$line" ]; then # not mounted /sbin/fsck /dev/sda1 fi done
How do I get fsck to just skip on already-mounted partitions? (Can't do it) ∞
[edit 2012-09-03] I could probably script a workaround, but there's nothing provided.
2019-10-05 - Just leverage is-partition-mounted.sh
Last updated 2019-10-05 at 09:53:41
Absorbed it all into my git repository.
Also solved a nine year old question. Because why not.