or die("$!")
business is commonplace in PERL when dealing with things which especially operate on files, which accounts for at least 52 functions:Code: Select all
accept() chown() dbmopen() getsockopt() mkdir() open() recv() semget() shmget() sysopen() truncate()
bind() close() exec() ioctl() msgctl() opendir() rename() semop() shmread() sysread() unlink()
binmode() closedir() fcntl() kill() msgget() pipe() rmdir() send() shutdown() sysseek()
chdir() connect() flock() link() msgrcv() read() seek() setsockopt() socketpair() system()
chmod() dbmclose() fork() listen() msgsnd() readlink() semctl() shmctl() symlink() syswrite()
autodie
pragma, which certainly cleans up the code, but IMO it's better to learn to do this manually to have a better understanding of the language; I didn't do this for a long time, then paid the price several times. Just because something will 'probably' work, doesn't mean it's wise to simply cross your fingers and hope things work out.My mindset with programming is similar to how I handle money: I don't save for what I expect, I save for what I don't.
Quite right, you didn't need the
declare
builtin to assign an array, unless it's an associative array.Regarding the
while read
loop, that's a good observation, but you weren't piping, as far as I could see. Redirecting into a while read
loop isn't piping, thankfully. You can forgo the processes substitution (<(...)
) for command substitution ($(...)
or `...`
) with a herestring (<<<
), like:Code: Select all
while read -a Line; do
if [[ ${Line[0]} == Mem: ]]; then
UsedRAM=${Line[2]}
break
fi
done <<< "$(free -h)"
printf '%s\n' "$UsedRAM"
readarray
or mapfile
builtin (synonymous), which dumps the contents of the file into an array (default: $MAPFILE
), where each line is an element in the array. Although, this can be tweaked with flags and stuff, such as to remove the included newline character (think of it like in PERL, without chomp()
). Note that this immediately stores the contents of the file into that array, which may be inefficient or otherwise undesired, depending on the task.Ah, I really do appreciate the honesty that you just want it to look cool, but here's the thing: it doesn't. When I first started programming, I genuinely thought that lots of busy looking complex and obnoxious code was SUPER COOL, but then I actually learned what complex code was and how ridiculous my old code looked. Never again. So, please don't worry about looking cool. IMO, what's cool is efficient, clean, and robust code.
Yeah, the HTML comments, as you demonstrated.
Your percentage thing is cracking me up.