Every
so often I will stumble across a new command that is incredibly useful or just
really cool. It is in those moments I realize how little I actually know
about this operating system and how incredibly powerful it actually is. Linux is easily molded into whatever we need
it to be. If you break it down and learn
the tools used to model it you can literally get it to do pretty much
anything. Obviously there are going to
be computational limitations, but within those limits the capabilities are
still quite amazing.
So
let’s start with a simple command.
[myprompt]$
ls
The
ls command allows a user to list the contents of a directory. A simple command by itself, but when coupled
with other scripting commands you begin to create dynamic tools within scripts.
For
instance let’s say the linux machine is used for maintaining characters. Each character has configuration data
associated with it. We will give each
character configuration a name in the form of <CharacterName>.chcfg
We
have a program that creates deletes and stores these files in /Data/Characters
directory. Let’s say we have another
program that needs to list all the characters.
This is one method:
#!/bin/bash
#!/bin/bash
##Char
list script
CHAR_CONF_DIR=/Data/Characters
MY_CHARACTERS=(
`ls ${ CHAR_CONF_DIR}\*.chcfg` )
echo
${ MY_CHARACTERS[@]}
MY_CHARACTERS
is a bash array. The bash array just
stores each individual .chcfg file within one variable. The ls command allowed
us to easily access the .chcfg data and store it into the variable. So now each time we add or remove a .chcfg
file and run the script it will automatically update the MY_CHARACTERS variable
without any modifications to the script.
No comments:
Post a Comment